Mybatis 初期使用比较麻烦,需要各种配置文件、实体类、Dao 层映射关联、还有一大推其它配置。mybatis-spring-boot-starter
就是 Spring Boot+ Mybatis 可以完全注解不用配置文件,也可以简单配置轻松上手。
mybatis-spring-boot-starter 官方说明:MyBatis Spring-Boot-Starter will help you use MyBatis with Spring Boot
其实就是 Mybatis 看 Spring Boot 这么火热也开发出一套解决方案来凑凑热闹,但这一凑确实解决了很多问题,使用起来确实顺畅了许多。mybatis-spring-boot-starter
主要有两种解决方案,一种是使用注解解决一切问题,一种是简化后的老传统。
当然任何模式都需要首先引入mybatis-spring-boot-starter
的 Pom 文件,现在最新版本是 2.0.1
<dependency > <groupId > org.mybatis.spring.boot</groupId > <artifactId > mybatis-spring-boot-starter</artifactId > <version > 2.0.1</version > </dependency >
我一般使用的是XML极简模式,可能是由于之前用的hibernate用习惯了
极简 xml 版本 极简 xml 版本保持映射文件的老传统,接口层只需要定义空方法,系统会自动根据方法名在映射文件中找对应的 Sql .
1 添加相关 Maven 文件 <dependencies > <dependency > <groupId > org.mybatis.spring.boot</groupId > <artifactId > mybatis-spring-boot-starter</artifactId > <version > 2.0.1</version > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <scope > runtime</scope > </dependency > <dependency > <groupId > org.projectlombok</groupId > <artifactId > lombok</artifactId > <optional > true</optional > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-test</artifactId > <scope > test</scope > </dependency > </dependencies >
完整的 Pom 包这里就不贴了,大家直接看源码
2、application.yml
相关配置 推荐使用application.yml
进行配置,其实使用application.yml
或者application.properties
都是一样的效果,application.yml
最终是转换为application.properties
进行生效的,只不过application.yml
视觉效果看起来更加明了。新建项目默认为application.properties
,直接改为application.yml
,另外新增一个application-test.yml
用户不同环境使用不同的配置文件用。
application.yml
配置:
spring : profiles : active : test mybatis : type-aliases-package : com.niaobulashi.entity mapper-locations : classpath:mapper/*.xml configuration : map-underscore-to-camel-case : true logging : level : com.niaobulashi.dao : DEBUG
application-test.yml
配置:
spring : datasource : url : jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true username : root password : root driver-class-name : com.mysql.cj.jdbc.Driver
Spring Boot 会自动加载 spring.datasource.*
相关配置,数据源就会自动注入到 sqlSessionFactory 中,sqlSessionFactory 会自动注入到 Mapper 中,对了,你一切都不用管了,直接拿起来使用就行了。
在启动类中添加对 mapper 包扫描@MapperScan
@SpringBootApplication @MapperScan("com.niaobulashi.dao") public class MybatisApplication { public static void main (String[] args) { SpringApplication.run(MybatisApplication.class, args); } }
或者直接在 Mapper 类上面添加注解@Mapper
,建议使用上面那种,不然每个 mapper 加个注解也挺麻烦的
3、添加 User 的实例 @Data public class SysUserEntity implements Serializable { private static final long serialVersionUID = 1 L; private Long userId; private String username; private String password; private String salt; private String email; private String mobile; private Integer status; private Date createTime; }
4、添加 User 的映射文件 <mapper namespace ="com.niaobulashi.dao.SysUserDao" > <select id ="queryUserInfo" resultType ="com.niaobulashi.entity.SysUserEntity" > SELECT ur.* FROM sys_user ur WHERE 1 = 1 AND ur.user_id = #{userId} </select > </mapper >
其实就是把上个版本中 Mapper 的 Sql 搬到了这里的 xml 中了
5、编写 Mapper 层的代码 public interface SysUserDao { List<SysUserEntity> queryUserInfo (Long userId) ; }
6、编写Service层的代码 SysUserService
接口类:
public interface SysUserService { List<SysUserEntity> queryUserInfo(Long userId); }
SysUserServiceImpl
实现类:
@Service("sysUserService") public class SysUserServiceImpl implements SysUserService { @Resource private SysUserDao sysUserDao; @Override public List<SysUserEntity> queryUserInfo (Long userId) { return sysUserDao.queryUserInfo(userId); } }
7、测试 经过上面5个步骤就可以完成基本的接口开发,省去了Controller层的开发
@RunWith(SpringRunner.class) @SpringBootTest public class MabatisTest { private final static Logger logger = LoggerFactory.getLogger(MabatisTest.class); @Autowired private SysUserService sysUserService; @Test public void queryUserInfo () throws Exception { SysUserEntity userEntity = new SysUserEntity(); userEntity.setUserId(1L ); List<SysUserEntity> list = sysUserService.queryUserInfo(userEntity.getUserId()); logger.info("list:" + list); } }
总结 SpringBoot和Mybatis这对CP,完美
示例代码-github