官方文档:html
Mybatis开发团队为Spring Boot 提供了 MyBatis-Spring-Boot-Starter 方便使用。spring
要使用MyBatis-Spring-Boot-Starter模块,只须要在类路径中包含 mybatis
mybatis-spring-boot-autoconfigure.jar文件及其依赖项(mybatis.jar,mybatis -spring.jar等) 。app
下面介绍的是我用到的一部分功能,所有功能还请查阅官方文档dom
若是您正在使用Maven,只需将如下依赖项添加到您的pom.xml中:spring-boot
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0-SNAPSHOT</version> </dependency>
版本本身可选:fetch
须要注意的是:在Spring 中使用mybatis 最少须要 一个SqlSessionFactory 和 mapper 接口spa
引入MyBatis-Spring-Boot-Starter 模块后 将自动提供以.net
下功能code
就是说,使用了该Starter以后,只须要定义一个DataSource便可,它会自动建立使用该DataSource的SqlSessionFactoryBean以及SqlSessionTemplate,会自动扫描你的Mappers,链接到SqlSessionTemplate,并注册到Spring上下文中。
与其余Spring Boot应用程序同样,MyBatis-Spring-Boot-Application配置参数存储在application.properties(或application.yml)内部。 使用前缀 mybatis
可用的属性有:
红色标注为经常使用属性
例:
# application.properties mybatis.type-aliases-package=com.example.domain.model mybatis.type-handlers-package=com.example.typehandler mybatis.configuration.map-underscore-to-camel-case=true mybatis.configuration.default-fetch-size=100 mybatis.configuration.default-statement-timeout=30
一、pom中引入 MyBatis-Spring-Boot-Starter
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0-SNAPSHOT</version> </dependency>
二、配置数据源
直接在application.properties 中
三、在application.properties 中配置Mapper.xml 的扫描
四、编写Mapper接口,和Mapper.xml
Mapper 接口要加@Mapper 注解
MyBatis-Spring-Boot-Starter将默认搜索标记有@Mapper注解的映射器。
您可能须要指定自定义注解或标记界面进行扫描。 若是是这样,你必须使用@MapperScan注解。
默认不用加这个注解
参考 : https://www.cnblogs.com/larryzeal/p/5874107.html