日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot2.0 基础案例(10):整合Mybatis框架,集成分页助手插件

發布時間:2025/3/17 javascript 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot2.0 基础案例(10):整合Mybatis框架,集成分页助手插件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、Mybatis框架

1、mybatis簡介

MyBatis 是一款優秀的持久層框架,它支持定制化 SQL、存儲過程以及高級映射。MyBatis 避免了幾乎所有的 JDBC 代碼和手動設置參數以及獲取結果集。MyBatis 可以使用簡單的 XML 或注解來配置和映射原生類型、接口和 Java 的 POJO(Plain Old Java Objects,普通老式 Java 對象)為數據庫中的記錄。

2、mybatis特點

1)sql語句與代碼分離,存放于xml配置文件中,方便管理 2)用邏輯標簽控制動態SQL的拼接,靈活方便 3)查詢的結果集與java對象自動映射 4)編寫原生態SQL,接近JDBC 5)簡單的持久化框架,框架不臃腫簡單易學

3、適用場景

MyBatis專注于SQL本身,是一個足夠靈活的DAO層解決方案。
對性能的要求很高,或者需求變化較多的項目,MyBatis將是不錯的選擇。

二、與SpringBoot2.0整合

1、項目結構圖


采用druid連接池,該連接池。

2、核心依賴

<!-- mybatis依賴 --> <dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version> </dependency> <!-- mybatis的分頁插件 --> <dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>4.1.6</version> </dependency>

3、核心配置

mybatis:# mybatis配置文件所在路徑config-location: classpath:mybatis.cfg.xmltype-aliases-package: com.boot.mybatis.entity# mapper映射文件mapper-locations: classpath:mapper/*.xml

4、逆向工程生成的文件


這里就不貼代碼了。

5、編寫基礎測試接口

// 增加 int insert(ImgInfo record); // 組合查詢 List<ImgInfo> selectByExample(ImgInfoExample example); // 修改 int updateByPrimaryKeySelective(ImgInfo record); // 刪除 int deleteByPrimaryKey(Integer imgId);

6、編寫接口實現

@Service public class ImgInfoServiceImpl implements ImgInfoService {@Resourceprivate ImgInfoMapper imgInfoMapper ;@Overridepublic int insert(ImgInfo record) {return imgInfoMapper.insert(record);}@Overridepublic List<ImgInfo> selectByExample(ImgInfoExample example) {return imgInfoMapper.selectByExample(example);}@Overridepublic int updateByPrimaryKeySelective(ImgInfo record) {return imgInfoMapper.updateByPrimaryKeySelective(record);}@Overridepublic int deleteByPrimaryKey(Integer imgId) {return imgInfoMapper.deleteByPrimaryKey(imgId);} }

7、控制層測試類

@RestController public class ImgInfoController {@Resourceprivate ImgInfoService imgInfoService ;// 增加@RequestMapping("/insert")public int insert(){ImgInfo record = new ImgInfo() ;record.setUploadUserId("A123");record.setImgTitle("博文圖片");record.setSystemType(1) ;record.setImgType(2);record.setImgUrl("https://avatars0.githubusercontent.com/u/50793885?s=460&v=4");record.setLinkUrl("https://avatars0.githubusercontent.com/u/50793885?s=460&v=4");record.setShowState(1);record.setCreateDate(new Date());record.setUpdateDate(record.getCreateDate());record.setRemark("知了");record.setbEnable("1");return imgInfoService.insert(record) ;}// 組合查詢@RequestMapping("/selectByExample")public List<ImgInfo> selectByExample(){ImgInfoExample example = new ImgInfoExample() ;example.createCriteria().andRemarkEqualTo("知了") ;return imgInfoService.selectByExample(example);}// 修改@RequestMapping("/updateByPrimaryKeySelective")public int updateByPrimaryKeySelective(){ImgInfo record = new ImgInfo() ;record.setImgId(11);record.setRemark("知了一笑");return imgInfoService.updateByPrimaryKeySelective(record);}// 刪除@RequestMapping("/deleteByPrimaryKey")public int deleteByPrimaryKey() {Integer imgId = 11 ;return imgInfoService.deleteByPrimaryKey(imgId);} }

8、測試順序

http://localhost:8010/insert http://localhost:8010/selectByExample http://localhost:8010/updateByPrimaryKeySelective http://localhost:8010/deleteByPrimaryKey

三、集成分頁插件

1、mybatis配置文件

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration><plugins><!--mybatis分頁插件--><plugin interceptor="com.github.pagehelper.PageHelper"><property name="dialect" value="mysql"/></plugin></plugins> </configuration>

2、分頁實現代碼

@Override public PageInfo<ImgInfo> queryPage(int page,int pageSize) {PageHelper.startPage(page,pageSize) ;ImgInfoExample example = new ImgInfoExample() ;// 查詢條件example.createCriteria().andBEnableEqualTo("1").andShowStateEqualTo(1);// 排序條件example.setOrderByClause("create_date DESC,img_id ASC");List<ImgInfo> imgInfoList = imgInfoMapper.selectByExample(example) ;PageInfo<ImgInfo> pageInfo = new PageInfo<>(imgInfoList) ;return pageInfo ; }

3、測試接口

http://localhost:8010/queryPage

四、源代碼地址

GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 碼云地址:知了一笑 https://gitee.com/cicadasmile/spring-boot-base


總結

以上是生活随笔為你收集整理的SpringBoot2.0 基础案例(10):整合Mybatis框架,集成分页助手插件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。