文章插圖

文章插圖
在《第一個MyBatis應用:Spring Boot整合MyBatis框架》文章中,SqlSessionFactory仍然是由MyBatis來構建的,通過SqlSessionFactory創建SqlSession,然后通過SqlSession來進行增刪查改操作,并沒有由Spring容器托管 。
實際上,在Spring Boot中使用MyBatis本質就是在Spring框架中集成MyBatis,并沒有其他任何高級的東西 。只不過在Spring Boot中使用時因為插件封裝的關系使得相關的配置可以更簡潔一些,但是這種封裝對于不熟悉MyBatis的人來講反而增加了理解的難度 。
因此,本文將把如何在Spring Boot中使用MyBatis進行詳細介紹,希望對你有幫助 。
一、創建Maven示例項目
創建Maven示例項目mybatis-second,如圖所示:
<dependencies>二、配置數據源
<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.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
通過與 Spring Boot的整合,SqlSessionFactory 交由 Spring 來構建 。構建時需要在Spring的配置文件中添加數據庫連接的四大屬性來配置數據源 。
在 Spring Boot的application.yml配置文件中添加如下配置 。
# 配置數據源從上述 MyBatis的運行時參數中可以看到,可以通過參數mybatis.mapper-locations指定XML映射器所在位置 。
spring:
datasource:
url: jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: xxx
# 整合mybatis
mybatis:
mapper-locations: classpath:mapper/*.xml
通過插件MyBatis-Spring-Boot-Starter在Spring Boot中集成MyBatis時,可以不用再去關心原生配置方式里的細節,直接使用默認配置就能實現最基本的功能 。當然,同樣可以針對MyBatis的核心組件進行定制 。
前面已經在項目的pom文件中添加了插件MyBatis-Spring-Boot-Starter的依賴配置 。
默認情況下,插件MyBatis-Spring-Boot-Starter將進行如下配置:
自動檢查Spring Boot的數據源配置并構建DataSource對象通過SqlSessionFactoryBean使用數據源構建并注冊SqlSessionFactory對象從SqlSessionFactory中創建并注冊一個SqlSessionTemplate實例,其實就是構建一個SqlSession對象自動掃描接口映射器,并將這些映射器與SqlSessionTemplate實例進行關聯,同時將它們注冊到Spring容器中
這些在Spring Boot中通過插件MyBatis-Spring-Boot-Starter自動完成了 。
四、定義接口映射器
在示例項目中,定義接口映射器UserMapper,代碼如下所示:
public interface UserMapper {[email protected]apper的接口映射器并將其注冊到Spring容器中,也可以在Spring [email protected],示例項目采用這種方式 。
MyUser selectUserById(int id);
List<MyUser> selectAllUser();
void addUser(MyUser myUser);
void updateUser(MyUser myUser);
void deleteUser(int id);
}
我們在示例項目中創建了一個測試類UserMapperTest,在該類中直接注入接口映射器Bean進行使用 。
@SpringBootTest運行單元測試,驗證輸出結果,如圖所示:
public class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
void selectUserById() {
MyUser myUser = userMapper.selectUserById(5);
System.out.println(myUser);
}
@Test
void selectAllUser() {
List<MyUser> listUser = userMapper.selectAllUser();
for (MyUser myUser:listUser) {
System.out.println(myUser);
}
}
@Test
void addUser() {
MyUser myUser = new MyUser();
myUser.setUname(“Spring Cloud”);
myUser.setUsex(“男”);
userMapper.addUser(myUser);
}
@Test
void updateUser() {
MyUser myUser = new MyUser();
myUser.setUid(5);
myUser.setUname(“MyBatis”);
myUser.setUsex(“男”);
userMapper.updateUser(myUser);
}
@Test
void deleteUser() {
userMapper.deleteUser(6);
}
}
六、定制MyBatis運行時參數
在Spring Boot中對MyBatis進行定制主要是指在Spring Boot的配置文件中(如:application.yaml)對MyBatis運行參數進行自定義配置(使用mybatis作為配置參數前綴):
mybatis:上述配置參數最終是通過mybatis-spring-boot-autoconfigure.jar加載和配置的 。
check-config-location: true # 是否檢測MyBatis運行參數配置文件
config-location:classpath:/mybatis-config.xml # 指定MyBatis運行參數配置文件位置
mapper-locations: classpath:/mapper/**/*.xml # 注冊XML映射器
type-aliases-package: test.springboot.model # 配置Java類型包名
type-handlers-package: test.springboot.handlers # 配置類型處理器包名
executor-type: SIMPLE # 指定執行器類型
configuration:
default-fetch-size: 20
default-statement-timeout: 30
另外,上述配置參數只是一個配置示例,詳細的配置參數列表請參考MyBatis配置官網:http://www.mybatis.org/mybatis-3/zh/configuration.html 。
總結起來,在Spring Boot中使用MyBatis可以使用2種方式:
(1)使用在Spring框架中集成MyBatis的原生集成方式
(2)使用插件MyBatis-Spring-Boot-Starter集成MyBatis
【mybatis和spring集成原理 springboot集成mybatis原理】上述兩種方式都可以實現對MyBatis的定制化配置,可以根據個人喜好進行選擇 。無論如何,要想在Spring Boot中靈活使用好MyBatis,最基礎的還是MyBatis和Spring框架本身 。
- 怎樣做小炒牛肉
- 大專和中專有什么區別,五年大專和三年中??忌洗髮5挠惺裁磪^別?
- 和生疏妹子聊天套路對話
- 手機四核處理器和八核處理器哪個好 四核手機好不好
- 虎啦和云小孩
- 企業服務和saas
- 和平分手后多久適合挽回 和平分手后多久適合挽回
- 如何和婆婆友好相處 和婆婆吵架后如何相處
- 出現婆媳關系不和的四大原因 婆媳吵架的主要原因有哪些
- 迪麗娜爾和迪麗熱巴是什么關系 兩人認識嗎
