Spring|6、springboot 集成mybatis

http://www.mybatis.org/mybatis-3/
https://github.com/mybatis/spring-boot-starter
1、添加mybatis 相关依赖Spring|6、springboot 集成mybatis
文章图片


2、在application.properties 设置 mybatis 参数
mybatis 配置参数

#########mybatis###########
## 设置mybatis的mapper和model扫描路径
mybatis.mapperLocations=classpath:mapping/*.xml,classpath:mapping/**/*.xml
mybatis.typeAliasesPackage=com.chn.frameweb.core.model
mybatis.config-location=classpath:mybatis/mybatis.cfg.xml

#mapper: 配置mappers路径后,则在接口中不需要添加@Mapper 注解了
#mappers 多个接口时逗号隔开
mapper.mappers=com.chn.frameweb.core.mapper.*
mapper.not-empty=false
#mapper.identity=MYSQL
mapper.identity=ORACLE


3、声明Mapper接口: 接口添加@Mapper注解
Spring|6、springboot 集成mybatis
文章图片


4、service 实现调用mapper
Spring|6、springboot 集成mybatis
文章图片

5、在springboot启动类添加 Mapper扫描组件 @MapperScan("com.chn.frameweb.core.mapper") (重要)
Spring|6、springboot 集成mybatis
文章图片

6、 如果不采用@MapperScan 的话,在application.properties中声明 mappers的位置也可以

Spring|6、springboot 集成mybatis
文章图片

注意(重要):mybatis 需要配置数据源DataSource,(SqlSessionFactory 工厂类中需要注入 数据库连接池实例 datasource ),在本集成搭建过程中并没有发现配置datasource,
这是因为 mybatis-spring-boot-starter 包含了mybatis-spring-boot-autoconfigure.jar 做了自动配置 ,会自动获取服务里的datasource ,并且自动加载了事务管理包
【Spring|6、springboot 集成mybatis】

    推荐阅读