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

您的位置:首頁技術文章
文章詳情頁

Spring Boot 整合 TKMybatis 二次簡化持久層代碼的實現

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

經常用 MyBatis 的的都知道,使用這個框架存在一個非常不友善的問題就是,就是每操作一個單表就需要自己手寫一個 xml 文件,雖然說可以用工具生成 xml 和實體類可以解決這個問題,但是二次開發的時候對某個表字段進行修改的時候,生成 xml 文件就不現實啦。最近發現 tk.mybatis 就非常好的解決了這個問題。tk.mybatis 整合了 MyBatis 框架,在其基礎上提供了很多工具,封裝了常用的增刪改查 SQL 語句,可以讓我們的開發效率更高。在這里和大家分享一下。

引入依賴

在 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><!-- 數據庫連接依賴 --><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>相關配置

在 application.yml 中添加相關配置

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: # 實體類的存放路徑,如:com.antoniopeng.hello.spring.boot.entity mapper-locations: classpath:mapper/*.xml # mapper.xml 文件存放路徑,這里存放在配置文件目錄 resources 下 logging: level: com.antoniopeng.hello.springboot.mybatis: debug # 配置監聽日志

在 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 分頁插件

引入依賴

在 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>

分頁查詢示例

@RunWith(SpringRunner.class)@SpringBootTest(classes = Application.class)@Transactional@Rollbackpublic class MyBatisTests { @Autowired UserService userService; /** * 測試分頁插件 */ @Test public void testPageHelper() { Example example = new Example(User.class); // 查詢條件 example.createCriteria().andEqualTo('userId', '1') // 分頁參數 PageHelper.startPage(1, 10, 'create_time desc'); // 獲取分頁列表數據 List<User> userList = userService.selectByExample(example); PageInfo pageInfo = new PageInfo(userList); // 獲取列表總數 int userCount = (int) pageInfo.getTotal(); }}

到此這篇關于Spring Boot 整合 TKMybatis 二次簡化持久層代碼的實現的文章就介紹到這了,更多相關Spring Boot 整合 TKMybatis 內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 国产一区二区三区在线观看精品 | 国产在线欧美日韩精品一区二区 | 一本一本久久a久久精品综合麻豆 | 香蕉手机网 | 婷婷六月天在线 | 中文字幕日韩一区 | 秀人网福利视频在线观看 | 久久精品全国免费观看国产 | 亚洲巨乳自拍在线视频 | 91视频最新网址 | 久久电影精品久久99久久 | 永久免费观看视频 | 成人中文字幕在线观看 | 诱人的女邻居bd中文字幕 | 久久精品视频在线观看 | 大香蕉毛片 | 天天躁日日躁狠狠躁中文字幕老牛 | 亚洲黄色片在线观看 | 亚洲 欧美 自拍 卡通 综合 | 国产欧美日韩在线视频 | 深夜在线看片 | 免费人成网站线观看合集 | 美女三级黄 | 欧美成人一区二区三区不卡视频 | 曰本女人一级毛片看一级毛 | 97精品国产自在现线免费 | 亚洲精品综合一二三区在线 | 尤物精品国产第一福利三区 | 亚洲日韩精品欧美一区二区一 | 成人精品一区二区户外勾搭野战 | pr社萌汁福利视频在线观看 | 韩国一级黄色录像 | 九九九热精品 | 日本亚洲黄色片 | 久久综合狠狠综合久久综合88 | 欧美3d人妖交 | 国产精品a在线观看香蕉 | 欧美日韩免费播放一区二区 | 天天拍夜夜添久久精品中文 | 国产真实乱人视频 | 国产视频 每日更新 |