亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Spring Boot 整合 TKMybatis 二次簡(jiǎn)化持久層代碼的實(shí)現(xiàn)

瀏覽:86日期:2023-07-24 17:24:12

經(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')@SpringBootApplicationpublic 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@Rollbackpublic 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)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 亚洲精品久久久久综合网 | 国产成人国产在线观看入口 | 国产麻豆精品入口在线观看 | 国内精品亚洲 | 国产成人深夜福利在线观看 | 人做人爱全免费视频 | 免费黄在线观看 | xxxxx日本护士xxxhd | xxxx鞠婧祎xxxx网站视频观看 | 一级做a爱过程免费视频时看 | 免费国产一区二区三区四区 | 欧美日韩精品一区二区 | 中文字幕乱码一区三区免费 | 欧美国产永久免费看片 | 亚洲精品欧美精品日韩精品 | 快射视频在线观看 | 国产99在线视频 | 国产成人精品无缓存在线播放 | 国产免费怕怕免费视频观看 | 久久久久久久综合狠狠综合 | 黄色成人在线网站 | 国产高清视频在线 | 色综合久久手机在线 | 在线看国产视频 | 久久精品国产福利 | 中文一区二区在线观看 | 在线观看国产一区二区三区 | 国产日本亚洲欧美 | 国产拍拍1000部ww | 青青伊人影院 | 亚洲欧美日韩国产色另类 | 免费黄色毛片视频 | 久久国产精品免费专区 | 久久精品国产半推半就 | 成人自拍网站 | 精品理论片一区二区三区 | 国产裸舞福利在线视频合集 | 久久国产精品2020免费m3u8 | 日韩一级片 | 久久精品亚洲综合一品 | 久久久精品影院 |