SpringBoot開發(fā)之整合Mybatis詳解
1 整合Mybatis
Spring Boot官方的依賴包:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>1 配置模式
- 全局配置文件
- SqlSessionFactory: 自動配置好了
- SqlSession:自動配置了 SqlSessionTemplate 組合了SqlSession
- @Import(AutoConfiguredMapperScannerRegistrar.class)
- Mapper層: 只要寫的操作MyBatis的接口帶著了 @Mapper 就會被自動掃描進來
// MyBatis配置項綁定類。
@EnableConfigurationProperties(MybatisProperties.class)
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class })
public class MybatisAutoConfiguration{}
@ConfigurationProperties(prefix = "mybatis")
public class MybatisProperties配置修改mybatis:
# 配置mybatis規(guī)則
mybatis:
config-location: classpath:mybatis/mybatis-config.xml # 全局配置文件位置
mapper-locations: classpath:mybatis/mapper/*.xml # sql映射文件位置
# Mapper接口--->綁定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.cf.admin.mapper.AccountMapper">
<!-- public Account getAcct(Long id); -->
<select id="getAcct" resultType="com.cf.admin.bean.Account">
select * from t_user where id=#{id}
</select>
</mapper>配置 private Configuration configuration; mybatis.configuration下面的所有,就是相當于改mybatis全局配置文件中的值
# 配置mybatis規(guī)則
mybatis:
# config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
configuration:
map-underscore-to-camel-case: true
# 可以不寫全局;配置文件,所有全局配置文件的配置都放在configuration配置項中即可使用流程:
- 導入mybatis官方starter
- 編寫mapper接口。標準@Mapper注解
- 編寫sql映射文件并綁定mapper接口
- 在application.yaml中指定Mapper配置文件的位置,以及指定全局配置文件的信息
2 注解模式
@Mapper
public interface CityMapper {
@Select("select * from t_user where id=#{id}")
public User getById(Long id);
}3 混合模式
@Mapper
public interface CityMapper {
@Select("select * from t_user where id=#{id}")
public User getById(Long id);
}使用流程:
- 引入mybatis-starter
- 配置application.yaml中,指定mapper-location位置即可
- 編寫Mapper接口并標注@Mapper注解
- 簡單方法直接注解方式 (如模式二)
- 復雜方法編寫mapper.xml進行綁定映射
- @MapperScan(“com.cf.admin.mapper”) 簡化,其他的接口就可以不用標注@Mapper注解
2 整合MyBatis-Plus
概述
MyBatis-Plus(簡稱 MP)是一個 MyBatis 的增強工具,在 MyBatis 的基礎(chǔ)上只做增強不做改變,為簡化開發(fā)、提高效率而生。
推薦安裝 MybatisX 插件
使用
引入依賴
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>說明:
- MybatisPlusAutoConfiguration 配置類,MybatisPlusProperties 配置項綁定。
- SqlSessionFactory 自動配置好。底層是容器中默認的數(shù)據(jù)源
- mapperLocations 自動配置好。有默認值。
- **classpath*:/mapper/**/*.xml;任意包的類路徑下的所有mapper文件夾下任意路徑下的所有xml都是sql映射文件。 **
- 容器中也自動配置好了 SqlSessionTemplate
- @Mapper 標注的接口也會被自動掃描 也可直接 @MapperScan(“com.cf.admin.mapper”) 批量掃描就行
優(yōu)點:
讓Mapper繼承 BaseMapper 就可以擁有簡單crud的能力
到此這篇關(guān)于SpringBoot開發(fā)之整合Mybatis詳解的文章就介紹到這了,更多相關(guān)SpringBoot整合Mybatis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringCloud-Alibaba之OSS對象存儲服務(wù)使用詳解
這篇文章主要介紹了SpringCloud-Alibaba之OSS對象存儲服務(wù)使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2026-03-03
maven 在執(zhí)行package,install,deploy時使用clean與不使用clean的不同之處
有時候用mvn install后,新改的內(nèi)容不生效,一定要后來使用mvn clean install 才生效,由于之前沒有做記錄,以及記不清是什么情況下才會出現(xiàn)的問題,于是想看看clean和不clean的區(qū)別,感興趣的朋友跟隨小編一起看看吧2021-08-08
SpringBoot3.2新特性之JdbcClient的使用
本文主要介紹了SpringBoot3.2新特性之JdbcClient的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-08-08
java同步器AQS架構(gòu)AbstractQueuedSynchronizer原理解析下
這篇文章主要為大家介紹了java同步器AQS架構(gòu)AbstractQueuedSynchronizer原理解析下,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-03-03
Servlet連接數(shù)據(jù)庫實現(xiàn)用戶登錄的實現(xiàn)示例
本文主要介紹了Servlet連接數(shù)據(jù)庫實現(xiàn)用戶登錄的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06

