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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

MyBatis-Plus 快速开始及详测 SpringBoot 集成Mybatis-Plus

發(fā)布時間:2025/3/19 javascript 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MyBatis-Plus 快速开始及详测 SpringBoot 集成Mybatis-Plus 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

MyBatis-Plus 快速開始及詳測

簡介

MyBatis-Plus (opens new window)(簡稱 MP)是一個 MyBatis (opens new window)的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡化開發(fā)、提高效率而生。

特性:

  • 無侵入:只做增強(qiáng)不做改變,引入它不會對現(xiàn)有工程產(chǎn)生影響,如絲般順滑
  • 損耗小:啟動即會自動注入基本 CURD,性能基本無損耗,直接面向?qū)ο蟛僮?/li>
  • 強(qiáng)大的 CRUD 操作:內(nèi)置通用 Mapper、通用 Service,僅僅通過少量配置即可實(shí)現(xiàn)單表大部分 CRUD 操作,更有強(qiáng)大的條件構(gòu)造器,滿足各類使用需求
  • 支持 Lambda 形式調(diào)用:通過 Lambda 表達(dá)式,方便的編寫各類查詢條件,無需再擔(dān)心字段寫錯
  • 支持主鍵自動生成:支持多達(dá) 4 種主鍵策略(內(nèi)含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解決主鍵問題
  • 支持 ActiveRecord 模式:支持 ActiveRecord 形式調(diào)用,實(shí)體類只需繼承 Model 類即可進(jìn)行強(qiáng)大的 CRUD 操作
  • 支持自定義全局通用操作:支持全局通用方法注入( Write once, use anywhere )
  • 內(nèi)置代碼生成器:采用代碼或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 層代碼,支持模板引擎,更有超多自定義配置等您來使用
  • 內(nèi)置分頁插件:基于 MyBatis 物理分頁,開發(fā)者無需關(guān)心具體操作,配置好插件之后,寫分頁等同于普通 List 查詢
  • 分頁插件支持多種數(shù)據(jù)庫:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多種數(shù)據(jù)庫
  • 內(nèi)置性能分析插件:可輸出 Sql 語句以及其執(zhí)行時間,建議開發(fā)測試時啟用該功能,能快速揪出慢查詢
  • 內(nèi)置全局?jǐn)r截插件:提供全表 delete 、 update 操作智能分析阻斷,也可自定義攔截規(guī)則,預(yù)防誤操作

架構(gòu):

步驟:

  • 建庫建表

    DROP TABLE IF EXISTS user;CREATE TABLE user (id BIGINT(20) NOT NULL COMMENT '主鍵ID',name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',age INT(11) NULL DEFAULT NULL COMMENT '年齡',email VARCHAR(50) NULL DEFAULT NULL COMMENT '郵箱',PRIMARY KEY (id) ); DELETE FROM user;INSERT INTO user (id, name, age, email) VALUES (1, 'Jone', 18, 'test1@baomidou.com'), (2, 'Jack', 20, 'test2@baomidou.com'), (3, 'Tom', 28, 'test3@baomidou.com'), (4, 'Sandy', 21, 'test4@baomidou.com'), (5, 'Billie', 24, 'test5@baomidou.com');
  • 創(chuàng)建項(xiàng)目

  • 導(dǎo)入依賴

    <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.1</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.4</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.1</version></dependency></dependencies>
  • yaml 配置文件

    server:port: 8484 spring:datasource:driver-class-name: com.mysql.cj.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSourceusername: rootpassword: // 這里用自己的密碼 下面的數(shù)據(jù)庫名也是一樣url: jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=UTC&useSSL=false&characterEncoding=utf8&serverTimezone=GMT
  • pojo層 : 這里是使用lombok 的插件 簡單易寫(就是方便偷懶)

    import com.baomidou.mybatisplus.annotation.TableName; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.Accessors; @Data @AllArgsConstructor @NoArgsConstructor @Accessors(chain = true) // 這個也是lombok的注解 方便 鏈?zhǔn)綍鴮?/span> @TableName(value = "user") public class User {private Long id;private String name;private Integer age;private String email; }
  • mapper層

    import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.crush.pojo.User;public interface UserMapper extends BaseMapper<User> {}
  • config:

    import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;//Spring boot方式 @Configuration public class MybatisPlusConfig {// 分頁 這里用的是mybatis-plus自帶的分頁@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return interceptor;} }

詳細(xì)測試:

  • 增刪改查齊全 mybatis-plus 自帶的單表增刪改查基本都測了
  • 多表的感覺還是要像原來一樣 那樣子應(yīng)該會更方便。
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.crush.mapper.UserMapper; import com.crush.pojo.User; import org.junit.Assert; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.junit.Test;import java.util.*;@RunWith(SpringRunner.class) @SpringBootTest public class SpringbootMybatsiPlusStudyApplicationTests {@Autowiredprivate UserMapper userMapper;//TODO 查詢?nèi)?/span>@Testpublic void testSelect() {System.out.println(("----- selectAll method test ------"));List<User> userList = userMapper.selectList(null); // Assert.assertEquals(8, userList.size());userList.forEach(System.out::println);}//TODO 分頁查詢@Testpublic void select2(){System.out.println("====分頁查詢====");Page<User> page=new Page<User>();page.setSize(2);page.setMaxLimit(10L);Page<User> userPage = userMapper.selectPage(page, null);System.out.println("獲取列表==>"+userPage.getRecords());System.out.println("總數(shù)==>"+page.getTotal());System.out.println("是否有上一頁==>"+page.hasPrevious());System.out.println("當(dāng)前頁==>"+page.getCurrent());System.out.println("是否有下一頁==>"+page.hasNext());System.out.println("size()==>"+page.getSize());System.out.println("getMaxLimit==>"+page.getMaxLimit());System.out.println("是否進(jìn)行 count 查詢==>"+page.isSearchCount());}//TODO 條件查詢@Testpublic void select3(){System.out.println("====條件查詢====");Map<String, Object> map = new HashMap<>();map.put("name","李小斌");List<User> users = userMapper.selectByMap(map);users.forEach(System.out::println);}//TODO 批量查詢@Testpublic void select4(){System.out.println("====批量查詢====");List<User> users = userMapper.selectBatchIds(new ArrayList<String>(Arrays.asList("1", "2", "3")));users.forEach(System.out::println);}//TODO 根據(jù)實(shí)體類存在條件查詢@Testpublic void select5(){System.out.println("====根據(jù)實(shí)體類存在條件查詢====");User user = new User().setName("李小斌");QueryWrapper<User> wrapper = new QueryWrapper<>(user);System.out.println(userMapper.selectOne(wrapper));}//TODO 根據(jù)ID查詢@Testpublic void select6(){System.out.println("====根據(jù)ID查詢====");System.out.println(userMapper.selectById(100L));}//TODO 查詢count 總條數(shù)@Testpublic void select7(){System.out.println("====查詢count 總條數(shù)====");User user = new User().setName("李小斌");QueryWrapper<User> wrapper = new QueryWrapper<>(user);System.out.println(userMapper.selectCount(wrapper));}//TODO 根據(jù)條件查詢 分頁 map 轉(zhuǎn)換@Testpublic void select8(){System.out.println("====根據(jù)條件查詢 分頁 map 轉(zhuǎn)換 ====");User user = new User().setEmail("951930136@qq.com");QueryWrapper<User> wrapper = new QueryWrapper<>(user);IPage<Map<String, Object>> page = new Page<>(1, 2);IPage<Map<String, Object>> userPage = userMapper.selectMapsPage(page, wrapper);System.out.println("當(dāng)前分頁總頁數(shù)==>"+userPage.getPages());System.out.println("當(dāng)前分頁總頁數(shù)==>"+userPage.getTotal());List<Map<String, Object>> users = userPage.getRecords();users.forEach(System.out::println);}//TODO 查詢 獲取id 獲取id@Testpublic void select9(){QueryWrapper<User> wrapper = new QueryWrapper<>();User user = new User().setName("李小斌");wrapper.setEntity(user);List<Object> objects = userMapper.selectObjs(wrapper);objects.forEach(System.out::println);}//TODO 根據(jù)ID更新數(shù)據(jù)@Testpublic void update1(){User user = new User().setId(100L).setName("王小慧");int i = userMapper.updateById(user);System.out.println(i);}//TODO 根據(jù)給出的實(shí)體類的條件更新數(shù)據(jù)@Testpublic void update2(){User user = new User().setId(100L);QueryWrapper<User> wrapper = new QueryWrapper<>();User user1 = new User().setName("李小斌");wrapper.setEntity(user);int update = userMapper.update(user1, wrapper);System.out.println(update);}//TODO insert@Testpublic void insert() {int i = userMapper.insert(new User(100L, "李小斌", 3, "951930136@qq.com"));Assert.assertEquals(1, i);}//TODO 根據(jù)id刪除@Testpublic void delete1() {int i = userMapper.deleteById(100);System.out.println(i);}//TODO 批量刪除@Testpublic void delete2(){int i = userMapper.deleteBatchIds(new ArrayList<String>(Arrays.asList("1337575143814062082", "1337575143814062083", "1337575143814062084")));System.out.println(i);}//TODO 刪除全部@Testpublic void delete3(){userMapper.delete(null);}//TODO 條件刪除@Testpublic void delete4(){Map<String, Object> map = new HashMap<>();map.put("name","李小斌");int i = userMapper.deleteByMap(map);System.out.println(i);}}

個人見解:

使用 Mybatis-Plus 感覺是方便快捷很多,給我一種 簡單的sql 語句要被干掉的感覺

因?yàn)槲沂褂孟聛?單表的操作 根本不需要看到 sql 語句。

看了一些項(xiàng)目 多表的話 還是需要自己寫的 。

建議:

不過 mybatis-plus 它沒有寫sql 但是執(zhí)行sql語句

里面的問題還是很值的探究 有興趣的一定要進(jìn)去康康 多打打斷點(diǎn)

后面接著學(xué)的話 肯定還會接著更新滴 。

在這次學(xué)習(xí)中 發(fā)現(xiàn)一個技巧 如果什么不會了 立馬點(diǎn)進(jìn)源碼里面慢慢看

再慢慢測試 這比看別人的博客和視頻 學(xué)習(xí)還會更快和更牢固一些。

共勉。

總結(jié)

以上是生活随笔為你收集整理的MyBatis-Plus 快速开始及详测 SpringBoot 集成Mybatis-Plus的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。