springboot集成mybatisplus的詳細(xì)步驟
Mybatis-Plus介紹
簡(jiǎn)介
MyBatis-Plus (opens new window)(簡(jiǎn)稱 MP)是一個(gè) MyBatis (opens new window)的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開發(fā)、提高效率而生。
特性(官網(wǎng)提供)
- 無(wú)侵入:只做增強(qiáng)不做改變,引入它不會(huì)對(duì)現(xiàn)有工程產(chǎn)生影響,如絲般順滑
- 損耗小:?jiǎn)?dòng)即會(huì)自動(dòng)注入基本 CURD,性能基本無(wú)損耗,直接面向?qū)ο蟛僮?,BaseMapper
- 強(qiáng)大的 CRUD 操作:內(nèi)置通用 Mapper、通用 Service,僅僅通過(guò)少量配置即可實(shí)現(xiàn)單表大部分 CRUD 操作,更有強(qiáng)大的條件構(gòu)造器,滿足各類使用需求,簡(jiǎn)單的CRUD操作不用自己編寫。
- 支持 Lambda 形式調(diào)用:通過(guò) Lambda 表達(dá)式,方便的編寫各類查詢條件,無(wú)需再擔(dān)心字段寫錯(cuò)
- 支持主鍵自動(dòng)生成:支持多達(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 層代碼,支持模板引擎,更有超多自定義配置等您來(lái)使用(自動(dòng)生成代碼)
- 內(nèi)置分頁(yè)插件:基于 MyBatis 物理分頁(yè),開發(fā)者無(wú)需關(guān)心具體操作,配置好插件之后,寫分頁(yè)等同于普通 List 查詢
- 分頁(yè)插件支持多種數(shù)據(jù)庫(kù):支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多種數(shù)據(jù)庫(kù)
- 內(nèi)置性能分析插件:可輸出 SQL 語(yǔ)句以及其執(zhí)行時(shí)間,建議開發(fā)測(cè)試時(shí)啟用該功能,能快速揪出慢查詢
- 內(nèi)置全局?jǐn)r截插件:提供全表 delete 、 update 操作智能分析阻斷,也可自定義攔截規(guī)則,預(yù)防誤操作
一、引入POM依賴
<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>
二、配置文件application.yml
spring: datasource: url: jdbc:mysql://172.26.0.296:3306/he?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai username: root password: P0de driver-class-name: com.mysql.cj.jdbc.Driver mybatis-plus: mapper-locations: classpath:mapper/*.xml
三、編寫表映射實(shí)體類
@TableName("sys_user") // 指定表名 public class UserEntity { private String id; private String username; public String id() { return id; } public void setId(String id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
四、編寫Mapper
簡(jiǎn)單寫個(gè)注解sql
public interface TestMapper extends BaseMapper<UserEntity> { @Select("select id from sys_user limit 1") String getId(); }
五、測(cè)試Controller
測(cè)試通過(guò):QueryWrapper方式查詢 + 注解方式查詢。
@RestController @RequestMapping("/wechat/portal") public class WechatController { @Autowired private TestMapper testMapper; @GetMapping("/test") public String getTest() {] // QueryWrapper方式查詢 QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>(); List<UserEntity> userEntities = testMapper.selectList(queryWrapper); System.out.println("userEntities --- " + userEntities); // 注解方式查詢 String id = testMapper.getId(); System.out.println("id ---" + id); return userEntities.toString(); } }
六、啟動(dòng)類
通過(guò)@MapperScan指定mapper所在包路徑。
@SpringBootApplication @MapperScan("org.jeecg.modules.mp.mapper") // 指定mapper包路徑 public class WxMpDemoApplication { public static void main(String[] args) { SpringApplication.run(WxMpDemoApplication.class, args); } }
到此這篇關(guān)于springboot四步集成mybatisplus的文章就介紹到這了,更多相關(guān)springboot集成mybatisplus內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringMVC實(shí)現(xiàn)表單驗(yàn)證功能詳解
這篇文章主要為大家詳細(xì)介紹了SpringMVC 表單驗(yàn)證的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Spring中@RequestMapping、@RestController和Postman
本文介紹了Spring框架中常用的@RequestMapping和@RestController注解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-10-10淺談Java數(shù)組的一些使用方法及堆棧存儲(chǔ)
下面小編就為大家?guī)?lái)一篇淺談Java數(shù)組的一些使用方法及堆棧存儲(chǔ)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07springboot創(chuàng)建多module項(xiàng)目的實(shí)例
這篇文章主要介紹了springboot創(chuàng)建多module項(xiàng)目的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02