1. 程式人生 > >最簡單的SpringBoot整合MyBatis教程

最簡單的SpringBoot整合MyBatis教程

temp mailto urn 部分 工作 prop throw source test

本文就來看看在Spring Boot中MyBatis要如何使用。

工程創建

首先創建一個基本的Spring Boot工程,添加Web依賴,MyBatis依賴以及MySQL驅動依賴,如下:
技術分享圖片
最簡單的SpringBoot整合MyBatis教程
創建成功後,添加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({
br/>@Results({
@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.sang.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-3-mapper.dtd">
<mapper namespace="org.sang.mybatis.mapper.UserMapper">
<select id="getAllUser" resultType="org.sang.mybatis.model.User">
select * from t_user;
</select>
<insert id="addUser" parameterType="org.sang.mybatis.model.User">
insert into user (username,address) values (#{username},#{address});
</insert>
<update id="updateUserById" parameterType="org.sang.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所在的包下面:
技術分享圖片
最簡單的SpringBoot整合MyBatis教程
放在這裏的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目錄下,又不能自動被掃描到,需要添加額外配置。例如我在resources目錄下創建mapper目錄用來放mapper文件,如下:
技術分享圖片
最簡單的SpringBoot整合MyBatis教程
此時在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)
br/>@ConditionalOnSingleCandidate(DataSource.class)
br/>@AutoConfigureAfter(DataSourceAutoConfiguration.class)
br/>@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource);
return factory.getObject();}
@Bean
br/>}
@Bean
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
br/>}
}
@org.springframework.context.annotation.Configuration
br/>@ConditionalOnMissingBean(MapperFactoryBean.class)
br/>@Override
logger.debug("No {} found.", MapperFactoryBean.class.getName());
}
}
}
從類上的註解可以看出,當當前類路徑下存在SqlSessionFactory、 SqlSessionFactoryBean以及DataSource時,這裏的配置才會生效,SqlSessionFactory和SqlTemplate都被提供了。為什麽要看這段代碼呢?下篇文章,松哥和大夥分享Spring Boot中MyBatis多數據源的配置時,這裏將是一個重要的參考。

歡迎工作一到五年的Java工程師朋友們加入Java架構師:697558955

群內提供免費的Java架構學習資料(裏面有高可用、高並發、高性能及分布式、Jvm性能調優、Spring源碼,MyBatis,Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginx等多個知識點的架構資料)合理利用自己每一分每一秒的時間來學習提升自己,不要再用"沒有時間“來掩飾自己思想上的懶惰!趁年輕,使勁拼,給未來的自己一個交代!

最簡單的SpringBoot整合MyBatis教程