SpringBoot2中如何整合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整合
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.xml type-aliases-package: com.boot.mybatis.entity # mapper映射文件 mapper-locations: classpath:mapper/*.xml
4、逆向工程產生的檔案
##這裡就不貼程式碼了。
5、寫基礎測試介面
// 增加 int insert(ImgInfo record); // 组合查询 List<img info alt="SpringBoot2中如何整合Mybatis框架" > selectByExample(ImgInfoExample example); // 修改 int updateByPrimaryKeySelective(ImgInfo record); // 删除 int deleteByPrimaryKey(Integer imgId);</imginfo>
@Service public class ImgInfoServiceImpl implements ImgInfoService { @Resource private ImgInfoMapper imgInfoMapper ; @Override public int insert(ImgInfo record) { return imgInfoMapper.insert(record); } @Override public List<img info alt="SpringBoot2中如何整合Mybatis框架" > selectByExample(ImgInfoExample example) { return imgInfoMapper.selectByExample(example); } @Override public int updateByPrimaryKeySelective(ImgInfo record) { return imgInfoMapper.updateByPrimaryKeySelective(record); } @Override public int deleteByPrimaryKey(Integer imgId) { return imgInfoMapper.deleteByPrimaryKey(imgId); } }</imginfo>
@RestController public class ImgInfoController { @Resource private 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<img info alt="SpringBoot2中如何整合Mybatis框架" > 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); } }</imginfo>
http://localhost:8010/insert http://localhost:8010/selectByExample http://localhost:8010/updateByPrimaryKeySelective http://localhost:8010/deleteByPrimaryKey
<?xml version="1.0" encoding="UTF-8" ?> nbsp;configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <plugins> <!--mybatis分页插件--> <plugin> <property></property> </plugin> </plugins> </configuration>
@Override public PageInfo<img info alt="SpringBoot2中如何整合Mybatis框架" > 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<img info alt="SpringBoot2中如何整合Mybatis框架" > imgInfoList = imgInfoMapper.selectByExample(example) ; PageInfo<img info alt="SpringBoot2中如何整合Mybatis框架" > pageInfo = new PageInfo(imgInfoList) ; return pageInfo ; }</imginfo></imginfo></imginfo>
http://localhost:8010/queryPage
以上是SpringBoot2中如何整合Mybatis框架的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

iBatis與MyBatis:你該選擇哪一個?簡介:隨著Java語言的快速發展,許多持久化框架也應運而生。 iBatis和MyBatis是兩個備受歡迎的持久化框架,它們都提供了一個簡單而高效的資料存取解決方案。本文將介紹iBatis和MyBatis的特點和優勢,並給出一些具體的程式碼範例,幫助你選擇合適的框架。 iBatis簡介:iBatis是一個開源的持久化框架

MyBatis動態SQL標籤解讀:Set標籤用法詳解MyBatis是一個優秀的持久層框架,它提供了豐富的動態SQL標籤,可以靈活地建構資料庫操作語句。其中,Set標籤是用來產生UPDATE語句中SET子句的標籤,在更新作業中非常常用。本文將詳細解讀MyBatis中Set標籤的用法,以及透過具體的程式碼範例來示範其功能。什麼是Set標籤Set標籤用於MyBati

JPA和MyBatis:功能與效能比較分析引言:在Java開發中,持久化框架扮演著非常重要的角色。常見的持久化框架包括JPA(JavaPersistenceAPI)和MyBatis。本文將對這兩個框架的功能和效能進行比較分析,並提供具體的程式碼範例。一、功能對比:JPA:JPA是JavaEE的一部分,提供了一個物件導向的資料持久化解決方案。它透過註解或X

MyBatis中實現批量刪除語句的幾種方式,需要具體程式碼範例近年來,由於資料量的不斷增加,批量操作成為了資料庫操作的一個重要環節之一。在實際開發中,我們經常需要批量刪除資料庫中的記錄。本文將重點介紹在MyBatis中實作批量刪除語句的幾種方式,並提供相應的程式碼範例。使用foreach標籤實作批量刪除MyBatis提供了foreach標籤,可以方便地遍歷一個集

MyBatis批量刪除語句的使用方法詳解,需要具體程式碼範例引言:MyBatis是一款優秀的持久層框架,提供了豐富的SQL操作功能。在實際專案開發中,經常會遇到需要大量刪除資料的情況。本文將詳細介紹MyBatis批量刪除語句的使用方法,並附上具體的程式碼範例。使用場景:在資料庫中刪除大量資料時,逐條執行刪除語句效率低。此時,可以使用MyBatis的批次刪除功能

MyBatis快取機制詳解:一文讀懂快取儲存原理引言在使用MyBatis進行資料庫存取時,快取是一個非常重要的機制,能夠有效減少對資料庫的訪問,提高系統效能。本文將詳細介紹MyBatis的快取機制,包括快取的分類、儲存原理和具體的程式碼範例。一、快取的分類MyBatis的快取主要分為一級快取和二級快取兩種。一級緩存一級緩存是SqlSession級別的緩存,當在

MyBatis一級快取詳解:如何提升資料存取效率?在開發過程中,高效率的資料存取一直是程式設計師關注的焦點之一。而對於MyBatis這樣的持久層框架而言,快取是提升資料存取效率的關鍵方法之一。 MyBatis提供了一級快取和二級快取兩種快取機制,其中一級快取是預設開啟的。本文將詳細介紹MyBatis一級快取的機制,並提供具體的程式碼範例,幫助讀者更好地理

MyBatis是一款流行的Java持久層框架,廣泛應用於各種Java專案。其中,批次插入是常見的操作,可以有效提升資料庫操作的效能。本文將深入探討MyBatis中批量的Insert實作原理,並結合具體的程式碼範例進行詳細解析。 MyBatis中的批次Insert在MyBatis中,批量Insert操作通常使用動態SQL來實作。透過建構一條包含多個插入值的S
