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 就會被自動掃描進(jìn)來
// MyBatis配置項(xiàng)綁定類。 @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下面的所有,就是相當(dāng)于改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配置項(xiàng)中即可
使用流程:
- 導(dǎo)入mybatis官方starter
- 編寫mapper接口。標(biāo)準(zhǔn)@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接口并標(biāo)注@Mapper注解
- 簡單方法直接注解方式 (如模式二)
- 復(fù)雜方法編寫mapper.xml進(jìn)行綁定映射
- @MapperScan(“com.cf.admin.mapper”) 簡化,其他的接口就可以不用標(biāo)注@Mapper注解
2 整合MyBatis-Plus
概述
MyBatis-Plus(簡稱 MP)是一個 MyBatis 的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡化開發(fā)、提高效率而生。
推薦安裝 MybatisX 插件
使用
引入依賴
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> </dependency>
說明:
- MybatisPlusAutoConfiguration 配置類,MybatisPlusProperties 配置項(xiàng)綁定。
- SqlSessionFactory 自動配置好。底層是容器中默認(rèn)的數(shù)據(jù)源
- mapperLocations 自動配置好。有默認(rèn)值。
- **classpath*:/mapper/**/*.xml;任意包的類路徑下的所有mapper文件夾下任意路徑下的所有xml都是sql映射文件。 **
- 容器中也自動配置好了 SqlSessionTemplate
- @Mapper 標(biāo)注的接口也會被自動掃描 也可直接 @MapperScan(“com.cf.admin.mapper”) 批量掃描就行
優(yōu)點(diǎn):
讓Mapper繼承 BaseMapper 就可以擁有簡單crud的能力
到此這篇關(guān)于SpringBoot開發(fā)之整合Mybatis詳解的文章就介紹到這了,更多相關(guān)SpringBoot整合Mybatis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java?Stream?流中?Collectors.toMap?的用法詳解
這篇文章主要介紹了Stream?流中?Collectors.toMap?的用法,Collectors.toMap()方法是把List轉(zhuǎn)Map的操作,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-01-01Java編程實(shí)現(xiàn)服務(wù)器端支持?jǐn)帱c(diǎn)續(xù)傳的方法(可支持快車、迅雷)
這篇文章主要介紹了Java編程實(shí)現(xiàn)服務(wù)器端支持?jǐn)帱c(diǎn)續(xù)傳的方法,涉及Java文件傳輸?shù)南嚓P(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11mybatis中映射文件include標(biāo)簽的應(yīng)用
這篇文章主要介紹了mybatis中映射文件include標(biāo)簽的應(yīng)用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11Java NIO 文件通道 FileChannel 用法及原理
這篇文章主要介紹了Java NIO 文件通道 FileChannel 用法和原理,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01