SpringBoot圖文教程11—今後不寫mapper文件「SpringBoot集成MybatisPlus」

有天上飛的概念,就要有落地的實現html

  • 概念十遍不如代碼一遍,朋友,但願你把文中全部的代碼案例都敲一遍
  • 先贊後看,養成習慣

SpringBoot 圖文教程系列文章目錄java

  1. SpringBoot圖文教程1「概念+案例 思惟導圖」「基礎篇上」
  2. SpringBoot圖文教程2—日誌的使用「logback」「log4j」
  3. SpringBoot圖文教程3—「‘初戀’情結」集成Jsp
  4. SpringBoot圖文教程4—SpringBoot 實現文件上傳下載
  5. SpringBoot圖文教程5—SpringBoot 中使用Aop
  6. SpringBoot圖文教程6—SpringBoot中過濾器的使用
  7. SpringBoot圖文教程7—SpringBoot攔截器的使用姿式這都有
  8. SpringBoot圖文教程8 — SpringBoot集成MBG「代碼生成器」
  9. SpringBoot圖文教程9—SpringBoot 導入導出 Excel 「Apache Poi」
  10. SpringBoot圖文教程10—模板導出|百萬數據Excel導出|圖片導出「easypoi」

前言

在使用Mybatis進行項目開發的時候,最繁瑣的事情就是實體類,dao接口,mapper.xml文件的編寫,幾乎每一個表都須要對應寫一套,而且大部分的工做量都在最基本的增刪改查上。若是表中的字段進行了修改,那麼實體類,mapper文件甚至dao接口都要進行修改。git

在以前的文章中介紹了 MBG(Mybatis 代碼生成器) 的使用,今天帶來更進一步的簡化Mybatis開發的工具 MybatisPlus,後續還會有 通用Mapper,總有一款適合你。github

MybatisPlus

MybatisPlus 能夠認爲一個Mybatis的外掛,用了這個技術以後 能夠不寫mapper文件 能夠不寫dao接口中的方法 而後實現增刪改查 分頁查詢 條件查詢 等等

什麼是Mybatis Plus

MyBatis-Plus(簡稱 MP)是一個 MyBatis 的加強工具,在 MyBatis 的基礎上只作加強不作改變,爲簡化開發、提升效率而生。sql

在 MyBatis 的基礎上只作加強不作改變

意味着 若是你導入了Mybatisplus的依賴 可是不想用Mybatisplus的方法 ,那麼 不會影響到Mybatis的正常使用數據庫

官方文檔:https://mp.baomidou.com/guide...segmentfault

MybatisPlus的使用

集成項目

  1. 導入Mybatisplus的依賴mybatis

    注意:要首先刪除Mybatis的依賴,由於Mybatisplus中包含有Mybatis的依賴 不須要獨立導入 容易jar包衝突

<dependency>
       <groupId>com.baomidou</groupId>
       <artifactId>mybatis-plus-boot-starter</artifactId>
       <version>3.3.1.tmp</version>
   </dependency>
  1. 修改配置文件

經過MybatisPlus實現增刪改查

教程新手向,只講解Mybatis最經常使用,最基本的API,更多的操做請參考官方文檔
demo使用到的數據庫庫表以下
  1. 給要數據庫操做的實體類加Mybatisplus的註解app

    由於Mybatisplus不須要寫mapper文件 不須要寫sql 那麼Mybatisplus怎麼知道實體類和數據庫表映射關係(ORM) 經過註解代表這種關係
/**
    * @TableName("cmfz_admin") 將當前的實體類和數據庫的表創建聯繫
    * 註解參數:表名
    */
   @TableName("cmfz_admin")
   @Data
   public class CmfzAdmin implements Serializable {
       /**
        * 主鍵屬性  @TableId
        *
        * value 該屬性對應的數據庫表中的字段名
        * type 主鍵自增的類型 AUTO 表明自動遞增
        */
       @TableId(value = "id",type = IdType.AUTO)
       private Integer id;
       /**
        * 非主鍵屬性  @TableField
        *  @TableField("username")  參數爲該屬性對應的數據庫表中的字段名
        *
        */
       @TableField("username")
       private String username;
   
       @TableField("password")
       private String password;
   
   }
  1. 建立dao接口ide

    import com.baizhi.entity.CmfzAdmin;
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    
    /**
     * 注意:
     * 1.接口中不須要寫方法
*/

public interface CmfzAdminDao extends BaseMapper<CmfzAdmin> {
}

3. 直接使用Mybatisplus的方法 開始增刪改查
@Test
   public void contextLoads() {

// 根據id查詢

CmfzAdmin cmfzAdmin = cmfzAdminDao.selectById(1);

// System.out.println(cmfzAdmin);

// 查詢全部 selectList(null);

List<CmfzAdmin> cmfzAdmins = cmfzAdminDao.selectList(null);
       for (CmfzAdmin admin : cmfzAdmins) {
           System.out.println(admin);
       }
   }

   /**
    * 添加
    */
   @Test
   public void test1(){

// 建立一個對象

CmfzAdmin cmfzAdmin = new CmfzAdmin();
       cmfzAdmin.setUsername("hhhh");
       cmfzAdmin.setPassword("123456");

// 添加 返回值是成功的條數

int insert = cmfzAdminDao.insert(cmfzAdmin);
       System.out.println(insert);
   }

   /**
    * 將id爲5的管理員名字修改成lisi
    */
   @Test
   public void test2(){
       CmfzAdmin cmfzAdmin = new CmfzAdmin();
       cmfzAdmin.setId(5);
       cmfzAdmin.setUsername("lisi");

// 根據id更新數據

int i = cmfzAdminDao.updateById(cmfzAdmin);

// 刪除

/**
        * deleteById() 根據id刪除
        * deleteBatchIds() 批量刪除 參數是要刪除的id的集合
        */
       
   }
#### 實現條件查詢 (條件構造器的使用)

/**

* 要查詢名字有zhangsan的管理員的信息
 */
@Test
public void test4(){
    /**
     * selectOne 查詢一個方法
     *
     * selectOne 須要一個Wrapper對象 其實是須要一個條件
     *
     * 條件是 查詢名字爲zhangsan管理員
     * sql where username = zhangsan
     * 須要 把 sql的條件 封裝到 Wrapper對象 對象中
     * 將封裝了條件的對象 給 selectOne
     *
     * Wrapper對象 條件構造器  將sql的條件 寫入到Java對象中
     * QueryWrapper 查詢條件構造器
     * UpdateWrapper 更新的條件構造器
     */

// 1.建立一個條件構造器 泛型 爲要查詢數據的實體類類型

QueryWrapper<CmfzAdmin> queryWrapper = new QueryWrapper<>();

// 2.將 where username = zhangsan 等值查詢 這個條件封裝到 條件構造器

// username = zhangsan ---》 eq("username","zhangsan") 參數1 字段名 參數2 要查詢的值
    queryWrapper.eq("username","zhangsan");

// 3.經過條件構造器 查詢

CmfzAdmin cmfzAdmin = cmfzAdminDao.selectOne(queryWrapper);

    System.out.println(cmfzAdmin);
}

/**
 * 查詢 名字爲 lisi 或者 密碼爲 123456 的管理員
 *
 * sql username = lisi or password = 123456
 *
 * or 和 and 怎麼構建?
 * eq("id",1).or().eq("name","老王")--->id = 1 or name = '老王'
 */
@Test
public void test5(){

// 1.建立條件構造器

QueryWrapper<CmfzAdmin> queryWrapper = new QueryWrapper<>();

// 2.構造條件 條件構造器的方法的第一個參數基本上都是字段名

queryWrapper.eq("username","lisi").or().eq("password","123456");

// 3.查詢

/**
     * 批量查詢 selectList() 方法中若是參數是null 就是查詢全部
     */
    List<CmfzAdmin> cmfzAdmins = cmfzAdminDao.selectList(queryWrapper);

    for (CmfzAdmin cmfzAdmin : cmfzAdmins) {
        System.out.println(cmfzAdmin);
    }
}
條件修改:把用戶名爲zhangsan的管理員密碼修改成123456

/**

* 用法和查詢相似
 * 把用戶名爲zhangsan的管理員密碼修改成123456
 *
 */
@Test
public void test6(){

// 1.建立條件構造器

UpdateWrapper<CmfzAdmin> updateWrapper = new UpdateWrapper<>();

// 2.構造條件
// where username = zhangsan
// set password = 123456

updateWrapper.set("password","123123");

    updateWrapper.eq("username","zhangsan");

// 3.使用方法修改

/**
     * update
     * 參數1  一個管理員對象  能夠直接寫null
     * 參數2  條件構造器
     */
    cmfzAdminDao.update(null,updateWrapper);
}
#### 實現分頁查詢

1. 配置分頁插件  配置在啓動類中便可

@Bean

public PaginationInterceptor paginationInterceptor() {
       PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
       // 設置請求的頁面大於最大頁後操做, true調回到首頁,false 繼續請求  默認false
       // paginationInterceptor.setOverflow(false);
       // 設置最大單頁限制數量,默認 500 條,-1 不受限制
       // paginationInterceptor.setLimit(500);
       // 開啓 count 的 join 優化,只針對部分 left join
       paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
       return paginationInterceptor;
   }
2. 直接在代碼可使用分頁查詢

/**

* 測試分頁查詢
    *
    * 獲取第二頁的數據(頁碼) 每頁顯示兩條(size)
    */
   @Test
   public void test7(){

// 1.建立一個page對象 封裝分頁數據

/**
        * 建立對象的時候 直接寫入分頁數據
        * 參數1 頁碼
        * 參數2 每頁條數
        */
       Page<CmfzAdmin> adminPage = new Page<>(2,2);

// 2.使用分頁查詢的方法

/**
        * 分頁查詢的方法 selectPage()
        * 參數1 IPage 分頁對象 封裝分頁信息 封裝獲取第二頁的數據(頁碼) 每頁顯示兩條(size)
        * 參數2 條件構造器 能夠直接寫null
        */
       Page<CmfzAdmin> cmfzAdminPage = cmfzAdminDao.selectPage(adminPage, null);

// 3.從Page對象中獲取查詢到的數據

/**
        * getRecords() 獲取查詢結果
        * getTotal() 總條數
        */
       System.out.println("總條數:"+cmfzAdminPage.getTotal());

       List<CmfzAdmin> adminList = cmfzAdminPage.getRecords();
       for (CmfzAdmin cmfzAdmin : adminList) {
           System.out.println(cmfzAdmin);
       }

   }
## 總結

> Tips:關於在SpringBoot項目中簡化Mybatis的開發,本教程提供三個技術 MBG,MybatisPlus,通用Mapper,總有一款適合你。


**恭喜你完成了本章的學習,爲你鼓掌!若是本文對你有幫助,請幫忙點贊,評論,轉發,這對做者很重要,謝謝。**

![](https://image-static.segmentfault.com/282/884/282884372-dff2818675f6e0e5_articlex)


讓咱們再次回顧本文的學習目標

> * 掌握SpringBoot中MybatisPlus的使用


要掌握SpringBoot更多的用法,請持續關注本系列教程。

## 求關注,求點贊,求轉發
相關文章
相關標籤/搜索