我是靠谱客的博主 高挑大雁,最近开发中收集的这篇文章主要介绍spring boot 2.x 集成 mybatis,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

引入关于mybatis的starter

compile 'org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2'

spring boot 配置文件中的mybatis配置项

#######mybatis配置######
###配置xml映射文件通用位置
mybatis.mapper-locations=classpath:mybatis/mappers/*.xml
###扫描别名包和@alias联用
mybatis.type-aliases-package=com.primerxiao.springboot.chapter5.pojo
###配置typehandler的扫描包
mybatis.type-handlers-package=com.primerxiao.springboot.chapter5.typehandler
#######mybatis配置######

创建实体类以及操作接口

import com.primerxiao.springboot.chapter5.enumeration.SexEnum;
import lombok.Data;
import org.apache.ibatis.type.Alias;
@Data
@Alias(value = "user")
public class User {
private long id;
private String userName;
private SexEnum sex;
private String note;
}
import com.primerxiao.springboot.chapter5.pojo.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserDao {
public User getUser(long id);
}

创建typehandler转换性别

import com.primerxiao.springboot.chapter5.enumeration.SexEnum;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@MappedJdbcTypes(JdbcType.INTEGER)
@MappedTypes(value = SexEnum.class)
public class SexTypeHandler extends BaseTypeHandler<SexEnum> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, SexEnum parameter, JdbcType jdbcType) throws SQLException {
ps.setInt(i,parameter.getId());
}
@Override
public SexEnum getNullableResult(ResultSet rs, String columnName) throws SQLException {
int sexId = rs.getInt(columnName);
if (sexId!=1&&sexId!=2) {
return null;
}
return SexEnum.getEnumById(sexId);
}
@Override
public SexEnum getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
int sexId = rs.getInt(columnIndex);
if (sexId!=1&&sexId!=2) {
return null;
}
return SexEnum.getEnumById(sexId);
}
@Override
public SexEnum getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
int sexId = cs.getInt(columnIndex);
if (sexId!=1&&sexId!=2) {
return null;
}
return SexEnum.getEnumById(sexId);
}
}

创建用户映射文件(userMapper.xml)

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD MAPPER 3.0//EN"
"http://mybatis.org/dtd/schema/mybatis-3-mapper.dtd">
<mapper namespace="com.primerxiao.springboot.chapter5.dao.UserDao">
<select id="getUser" parameterType="long" resultType="user">
select id,user_name as userName,sex,note from t_user where id=#{id}
</select>
</mapper>

定义mybatis操作接口(dao)

import com.primerxiao.springboot.chapter5.pojo.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserDao {
public User getUser(long id);
}

使用控制器测试mybatis接口

import com.primerxiao.springboot.chapter5.dao.UserDao;
import com.primerxiao.springboot.chapter5.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserDao userDao;
@RequestMapping("/getUser")
public User getUser(Long id) {
User user = userDao.getUser(id);
return user;
}
}

装配mybatis接口(用@MapperScan定义扫描)

@SpringBootApplication(scanBasePackages = {"com.primerxiao.springboot.chapter5"})
@EnableJpaRepositories(basePackages = "com.primerxiao.springboot.chapter5.dao")
@EntityScan(basePackages = "com.primerxiao.springboot.chapter5.pojo")
@MapperScan(
basePackages = "com.primerxiao.springboot.chapter5.*",
sqlSessionFactoryRef = "sqlSessionFactory",
sqlSessionTemplateRef = "sqlSessionTemplate",
annotationClass = Repository.class
)
public class Chapter5Application implements ApplicationContextAware
{
@Autowired
public static
ApplicationContext context;
public static void main(String[] args) {
SpringApplication.run(Chapter5Application.class, args);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Chapter5Application.context=applicationContext;
}
}

启动项目后通过intellij idea自带的httpclient工具测试,附上测试结果

HTTP/1.1 200
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 23 Jan 2019 03:59:56 GMT
{
"id": 1,
"userName": "1",
"sex": "MAIL",
"note": "1"
}
Response code: 200; Time: 327ms; Content length: 47 bytes

 

最后

以上就是高挑大雁为你收集整理的spring boot 2.x 集成 mybatis的全部内容,希望文章能够帮你解决spring boot 2.x 集成 mybatis所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部