使用 Mybatis 时,需要用到 Mybatis 中几个核心的类,比如 SqlSessionFactory,SqlSession 等。但是,如果在 SpringBoot 中使用的话,只需要在 maven 中使用 MyBatis-Spring-Boot-Starter 即可,可以不再和 SqlSessionFactory,SqlSession 这些类打交道。这个 Start 并不是 SpringBoot 官方写的,而是 Mybatis 为 SpringBoot 写的。
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
mybatis-spring-boot-starter 启动器包含了 mybatis-spring-boot-autoconfigure.jar,该 jar 包中,已经自动配置了这些核心类。因此,简化了开发者的使用。
原生的 Mybatis 的配置是写在 Mybatis.xml 文件中,和 SpringBoot 整合后,就可以写在 SpringBoot 的 application.properties 或者 application.yml 中。
配置的细节在 mybatis-spring-boot-autoconfigure-1.3.2-sources.jar 包中的 META-INF/spring-configuration-metadata.json 中查看。
SpringBoot 中使用 Mybatis,可以使用注解方式,也可以使用 xml 配置方式,各有优缺点。个人喜欢使用 xml 方式,因为,这样就把 SQL 语句统一存放在 xml 文件中管理,代码中不再出现 SQL 语句。
配置数据源和 mybatis 的 map 路径之后,就可以了。
application.properties
server.port=8085
spring.datasource.url=jdbc:mysql://localhost:3306/test?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# Mybatis
mybatis.mapper-locations=classpath:mapper/*.xml
上面的配置中,数据源 springboot 是自带的数据源,并且指定了 mapper 文件所在的路径,Mybatis 会在这个路径下找所以的匹配的文件。类路径默认就是 resource 文件夹下。
MyBatis 会检测到这个 DataSource,并且会用这个 DataSource 绑定到 SqlSessionFactory ,SqlSessionTemplate ,装配到 spring 容器中。
剩下需要配置的是 mapper 文件,mapper 文件中,通过 xml 的方式定义了 sql 语句。当然,你也可以选择用注解的方式,而不使用 xml 文件。
示例代码:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper
namespace="com.example.weeklyreport.mapper.ReportMapper">
<!-- ReportEntity getReportById(int id); -->
<select id="getReportById"
resultType="com.example.weeklyreport.entity.ReportEntity">
SELECT * FROM wr_report WHERE id = #{id}
</select>
<!-- public int insert(ReportEntity entity); -->
<insert id="insert"
parameterType="com.example.weeklyreport.entity.ReportEntity" useGeneratedKeys="true" keyProperty="id">
INSERT INTO `wr_report` (`username`,`title`,`report`,`createtime`,`modifiedtime`)
VALUES
(#{username},#{title},#{report},#{createtime},#{modifiedtime});
</insert>
</mapper>
接着需要在代码中定义一个 Mapper 接口,该接口的类型必须和上述文件中的 mapper 的 namespace 一致。这个接口的方法,就是 xml 中的 select,insert,update,delete 节点中的 id,名字必须是相同的。
Mapper接口
public interface ReportMapper {
public ReportEntity getReportById(int i) ;
public void insert(ReportEntity entity);
}
注意,这个 Mapper 接口的作用就类似 Dao 层代码,但是,在 Spring 中使用,可以通过@Mapper 注解,将其装配到容器中去,@Mapper 注解并不是 Spring 中的注解,也可以使用@MapperScan 注解,定义所有的 Mapper 接口所在的 jar 包,例如@MapperScan(“com.example.weeklyreport.mapper”)。
有了 Mapper 和 XML 后,通过 Spring 注入的方式,获取 Mapper 的实例,就可以操作数据库了。
@Autowired
ReportMapper reportMapper;
@RequestMapping("/")
public String index(Model model) {
Object o=reportMapper.getReportById(0);
model.addAttribute("r",o);
return "index";
}
SpringBoot 和 Mybatis 整合流程就这么几步,但是 MyBatis 的细节远不止这么多,比如动态的生成 sql,多表查询的映射等等,但这些也并不会很难,大多数情况下可通过查询操作手册就会用,学习曲线很平稳。因此,MyBatis 越来越受到欢迎。