Spring Boot 整合 TKMybatis 二次簡(jiǎn)化持久層代碼的實(shí)現(xiàn)
經(jīng)常用 MyBatis 的的都知道,使用這個(gè)框架存在一個(gè)非常不友善的問題就是,就是每操作一個(gè)單表就需要自己手寫一個(gè) xml 文件,雖然說可以用工具生成 xml 和實(shí)體類可以解決這個(gè)問題,但是二次開發(fā)的時(shí)候?qū)δ硞€(gè)表字段進(jìn)行修改的時(shí)候,生成 xml 文件就不現(xiàn)實(shí)啦。最近發(fā)現(xiàn) tk.mybatis 就非常好的解決了這個(gè)問題。tk.mybatis 整合了 MyBatis 框架,在其基礎(chǔ)上提供了很多工具,封裝了常用的增刪改查 SQL 語(yǔ)句,可以讓我們的開發(fā)效率更高。在這里和大家分享一下。
引入依賴
在 pom.xml 中引入 mapper-spring-boot-starter 依賴
<!-- druid-spring-boot-starter --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <!-- 數(shù)據(jù)庫(kù)連接依賴 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> <scope>runtime</scope> </dependency> <!-- mapper-spring-boot-starter --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.0.2</version> </dependency>
相關(guān)配置
在 application.yml 中添加相關(guān)配置
spring: datasource: druid: url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123456 initial-size: 1 min-idle: 1 max-active: 20 test-on-borrow: true driver-class-name: com.mysql.jdbc.Driver # MySQL 8.x: com.mysql.cj.jdbc.Driver mybatis: type-aliases-package: # 實(shí)體類的存放路徑,如:com.antoniopeng.hello.spring.boot.entity mapper-locations: classpath:mapper/*.xml # mapper.xml 文件存放路徑,這里存放在配置文件目錄 resources 下 logging: level: com.antoniopeng.hello.springboot.mybatis: debug # 配置監(jiān)聽日志
在 Application 入口類中使用 tk.mybatis.spring.annotation 包下的 @MapperScan 注解指定 Mapper 接口的掃描路徑
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@MapperScan(value = "com.antoniopeng.springboot.mybatis.mapper")
@SpringBootApplication
public class HelloSpringBootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(HelloSpringBootMybatisApplication.class, args);
}
}
整合 PageHelper 分頁(yè)插件
引入依賴
在 pom.xml 中引入 pagehelper-spring-boot-starter 依賴
<!-- pagehelper-spring-boot-starter --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.5</version> </dependency>
分頁(yè)查詢示例
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@Transactional
@Rollback
public class MyBatisTests {
@Autowired
UserService userService;
/**
* 測(cè)試分頁(yè)插件
*/
@Test
public void testPageHelper() {
Example example = new Example(User.class);
// 查詢條件
example.createCriteria().andEqualTo("userId", "1")
// 分頁(yè)參數(shù)
PageHelper.startPage(1, 10, "create_time desc");
// 獲取分頁(yè)列表數(shù)據(jù)
List<User> userList = userService.selectByExample(example);
PageInfo pageInfo = new PageInfo(userList);
// 獲取列表總數(shù)
int userCount = (int) pageInfo.getTotal();
}
}
到此這篇關(guān)于Spring Boot 整合 TKMybatis 二次簡(jiǎn)化持久層代碼的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Spring Boot 整合 TKMybatis 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot統(tǒng)一異常處理最佳實(shí)踐(拓展篇)
這篇文章主要給大家介紹了關(guān)于Spring Boot統(tǒng)一異常處理最佳實(shí)踐(拓展篇)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
SpringBoot通過JSON傳遞請(qǐng)求參數(shù)的實(shí)例詳解
這篇文章主要介紹了SpringBoot通過JSON傳遞請(qǐng)求參數(shù),示例介紹SpringMVC如何通過JSON格式傳遞入?yún)?,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-11-11
java 獲取日期的幾天前,幾個(gè)月前和幾年前的實(shí)例
下面小編就為大家?guī)?lái)一篇java 獲取日期的幾天前,幾個(gè)月前和幾年前的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-10-10
servlet實(shí)現(xiàn)簡(jiǎn)單的權(quán)限管理和敏感詞過濾功能
JavaEE課要求用servlet和過濾器實(shí)現(xiàn)權(quán)限管理和敏感詞過濾功能,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
IDEA創(chuàng)建Maven工程Servlet的詳細(xì)教程
這篇文章主要介紹了IDEA創(chuàng)建Maven工程Servlet的詳細(xì)教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Spring?Cache抽象-使用SpEL表達(dá)式解析
這篇文章主要介紹了Spring?Cache抽象-使用SpEL表達(dá)式解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringBoot調(diào)用第三方接口的幾種方式小結(jié)
在項(xiàng)目中調(diào)用第三方接口時(shí),確實(shí)需要根據(jù)項(xiàng)目的技術(shù)棧、架構(gòu)規(guī)范以及具體的業(yè)務(wù)需求來(lái)選擇最適合的調(diào)用方式,下面我們就介紹幾種調(diào)用第三方接口的實(shí)現(xiàn)方式以及代碼示例,需要的朋友可以參考下2024-07-07
Jenkins系統(tǒng)如何進(jìn)行數(shù)據(jù)備份
隨著我們的長(zhǎng)期使用,Jenkins系統(tǒng)中的內(nèi)容會(huì)越來(lái)越多,特別是一些配置相關(guān)的東西,不能有任何丟失。這個(gè)時(shí)候我們就需要定期備份我們的Jenkins系統(tǒng),避免一些誤操作不小心刪除了某些重要文件,本文就將介紹下Jenkins系統(tǒng)如何進(jìn)行數(shù)據(jù)備份2021-06-06

