前面兩篇文章和讀者聊了 Spring Boot 中最簡單的數據持久化方案 JdbcTemplate,JdbcTemplate 雖然簡單,但是用的並不多,因為它沒有 MyBatis 方便,在 Spring+SpringMVC 中整合 MyBatis 步驟還是有點復雜的,要配置多個 Bean,Spring Boot 中對此做了進一步的簡化,使 MyBatis 基本上可以做到開箱即用,本文就來看看在 Spring Boot 中 MyBatis 要如何使用。
工程創建
首先創建一個基本的 Spring Boot 工程,添加 Web 依賴,MyBatis 依賴以及 MySQL 驅動依賴,如下:
創建成功后,添加Druid依賴,並且鎖定MySQL驅動版本,完整的依賴如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.28</version>
<scope>runtime</scope>
</dependency>
如此,工程就算是創建成功了。小伙伴們注意,MyBatis 和 Druid 依賴的命名和其他庫的命名不太一樣,是屬於 xxx-spring-boot-stater 模式的,這表示該 starter 是由第三方提供的。
基本用法
MyBatis 的使用和 JdbcTemplate 基本一致,首先也是在 application.properties 中配置數據庫的基本信息:
spring.datasource.url=jdbc:mysql:///test01?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
配置完成后,MyBatis 就可以創建 Mapper 來使用了,例如我這里直接創建一個 UserMapper2,如下:
public interface UserMapper2 {
@Select("select * from user")
List<User> getAllUsers();
@Results({
@Result(property = "id", column = "id"),
@Result(property = "username", column = "u"),
@Result(property = "address", column = "a")
})
@Select("select username as u,address as a,id as id from user where id=#{id}")
User getUserById(Long id);
@Select("select * from user where username like concat('%',#{name},'%')")
List<User> getUsersByName(String name);
@Insert({"insert into user(username,address) values(#{username},#{address})"})
@SelectKey(statement = "select last_insert_id()", keyProperty = "id", before = false, resultType = Integer.class)
Integer addUser(User user);
@Update("update user set username=#{username},address=#{address} where id=#{id}")
Integer updateUserById(User user);
@Delete("delete from user where id=#{id}")
Integer deleteUserById(Integer id);
}
這里是通過全注解的方式來寫 SQL,不寫 XML 文件。
@Select、@Insert、@Update 以及 @Delete 四個注解分別對應 XML 中的 select、insert、update 以及 delete 標簽,@Results 注解類似於 XML 中的 ResultMap 映射文件(getUserById 方法給查詢結果的字段取別名主要是向小伙伴們演示下 @Results
注解的用法)。
另外使用 @SelectKey 注解可以實現主鍵回填的功能,即當數據插入成功后,插入成功的數據 id 會賦值到 user 對象的id 屬性上。
UserMapper2 創建好之后,還要配置 mapper 掃描,有兩種方式,一種是直接在 UserMapper2 上面添加 @Mapper
注解,這種方式有一個弊端就是所有的 Mapper 都要手動添加,要是落下一個就會報錯,還有一個一勞永逸的辦法就是直接在啟動類上添加 Mapper 掃描,如下:
@SpringBootApplication
@MapperScan(basePackages = "org.javaboy.mybatis.mapper")
public class MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class, args);
}
}
好了,做完這些工作就可以去測試 Mapper 的使用了。
mapper 映射
當然,開發者也可以在 XML 中寫 SQL,例如創建一個 UserMapper,如下:
public interface UserMapper {
List<User> getAllUser();
Integer addUser(User user);
Integer updateUserById(User user);
Integer deleteUserById(Integer id);
}
然后創建 UserMapper.xml 文件,如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-21-mapper.dtd">
<mapper namespace="org.javaboy.mybatis.mapper.UserMapper">
<select id="getAllUser" resultType="org.javaboy.mybatis.model.User">
select * from t_user;
</select>
<insert id="addUser" parameterType="org.javaboy.mybatis.model.User">
insert into user (username,address) values (#{username},#{address});
</insert>
<update id="updateUserById" parameterType="org.javaboy.mybatis.model.User">
update user set username=#{username},address=#{address} where id=#{id}
</update>
<delete id="deleteUserById">
delete from user where id=#{id}
</delete>
</mapper>
將接口中方法對應的 SQL 直接寫在 XML 文件中。
那么這個 UserMapper.xml 到底放在哪里呢?有兩個位置可以放,第一個是直接放在 UserMapper 所在的包下面:
放在這里的 UserMapper.xml 會被自動掃描到,但是有另外一個 Maven 帶來的問題,就是 java 目錄下的 xml 資源在項目打包時會被忽略掉,所以,如果 UserMapper.xml 放在包下,需要在 pom.xml 文件中再添加如下配置,避免打包時 java 目錄下的 XML 文件被自動忽略掉:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
當然,UserMapper.xml 也可以直接放在 resources 目錄下,這樣就不用擔心打包時被忽略了,但是放在 resources 目錄下,必須創建和 Mapper 接口包目錄相同的目錄層級,這樣才能確保打包后 XML 和 Mapper 接口又處於在一起,否則 XML 文件將不能被自動掃描,這個時候就需要添加額外配置。例如我在 resources 目錄下創建 mapper 目錄用來放 mapper 文件,如下:
此時在 application.properties 中告訴 mybatis 去哪里掃描 mapper:
mybatis.mapper-locations=classpath:mapper/*.xml
如此配置之后,mapper 就可以正常使用了。注意這種方式不需要在 pom.xml 文件中配置文件過濾。
原理分析
在 SSM 整合中,開發者需要自己提供兩個 Bean,一個SqlSessionFactoryBean ,還有一個是 MapperScannerConfigurer,在 Spring Boot 中,這兩個東西雖然不用開發者自己提供了,但是並不意味着這兩個 Bean 不需要了,在 org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
類中,我們可以看到 Spring Boot 提供了這兩個 Bean,部分源碼如下:
@org.springframework.context.annotation.Configuration
@ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class })
@ConditionalOnSingleCandidate(DataSource.class)
@EnableConfigurationProperties(MybatisProperties.class)
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
public class MybatisAutoConfiguration implements InitializingBean {
@Bean
@ConditionalOnMissingBean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource);
return factory.getObject();
}
@Bean
@ConditionalOnMissingBean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
ExecutorType executorType = this.properties.getExecutorType();
if (executorType != null) {
return new SqlSessionTemplate(sqlSessionFactory, executorType);
} else {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@org.springframework.context.annotation.Configuration
@Import({ AutoConfiguredMapperScannerRegistrar.class })
@ConditionalOnMissingBean(MapperFactoryBean.class)
public static class MapperScannerRegistrarNotFoundConfiguration implements InitializingBean {
@Override
public void afterPropertiesSet() {
logger.debug("No {} found.", MapperFactoryBean.class.getName());
}
}
}
從類上的注解可以看出,當當前類路徑下存在 SqlSessionFactory、 SqlSessionFactoryBean 以及 DataSource 時,這里的配置才會生效,SqlSessionFactory 和 SqlTemplate 都被提供了。為什么要看這段代碼呢?下篇文章,松哥和大伙分享 Spring Boot 中 MyBatis 多數據源的配置時,這里將是一個重要的參考。
好了,本文就先說到這里,本文相關案例,大家可以在 GitHub 上下載:https://github.com/lenve/javaboy-code-samples
關注公眾號【江南一點雨】,專注於 Spring Boot+微服務以及前后端分離等全棧技術,定期視頻教程分享,關注后回復 Java ,領取松哥為你精心准備的 Java 干貨!