springboot集成mybatisplus的詳細(xì)步驟
Mybatis-Plus介紹
簡介
MyBatis-Plus (opens new window)(簡稱 MP)是一個(gè) MyBatis (opens new window)的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡化開發(fā)、提高效率而生。
特性(官網(wǎng)提供)
- 無侵入:只做增強(qiáng)不做改變,引入它不會對現(xiàn)有工程產(chǎn)生影響,如絲般順滑
- 損耗?。簡蛹磿詣幼⑷牖?CURD,性能基本無損耗,直接面向?qū)ο蟛僮?,BaseMapper
- 強(qiáng)大的 CRUD 操作:內(nèi)置通用 Mapper、通用 Service,僅僅通過少量配置即可實(shí)現(xiàn)單表大部分 CRUD 操作,更有強(qiáng)大的條件構(gòu)造器,滿足各類使用需求,簡單的CRUD操作不用自己編寫。
- 支持 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í)行時(shí)間,建議開發(fā)測試時(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
簡單寫個(gè)注解sql
public interface TestMapper extends BaseMapper<UserEntity> { @Select("select id from sys_user limit 1") String getId(); }
五、測試Controller
測試通過: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(); } }
六、啟動類
通過@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)容請搜索腳本之家以前的文章或繼續(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注解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-10-10springboot創(chuàng)建多module項(xiàng)目的實(shí)例
這篇文章主要介紹了springboot創(chuàng)建多module項(xiàng)目的實(shí)例代碼,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02