Mybatis - plus
mybatis-plus 官方文檔
1、配置
引入對應的文件包,spring boot + mybatis 需添加依賴文件如下:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- 添加 代碼生成器 依賴 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.1.1</version>
</dependency>
<!-- 添加 代碼生成器 依賴 -->
<dependency>
<groupId>org.shenjia</groupId>
<artifactId>MyBatisX</artifactId>
<version>0.0.5</version>
</dependency>
<!-- 其他模板引擎 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
<!-- 其他模板引擎 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId><!--mysql數據庫驅動-->
<version>5.1.46</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</artifactId>
<version>1.1.10</version>
</dependency>
</dependencies>
application.yml添加mybatis - plus相關配置
配置mybatis-plus代碼生成器

/** * @author: zhoujiong * @description: 演示例子,執行 main 方法控制台輸入模塊表名回車自動生成對應項目目錄中 * @className: CodeGenerator * @date: 2019/5/13 15:37 */ public class CodeGenerator {</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) { </span><span style="color: #008000;">//</span><span style="color: #008000;">1 - FreemarkerTemplateEngine, 0-velocity</span> <span style="color: #0000ff;">int</span> result = 0<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 自定義需要填充的字段</span> List<TableFill> tableFillList = <span style="color: #0000ff;">new</span> ArrayList<><span style="color: #000000;">(); tableFillList.add(</span><span style="color: #0000ff;">new</span> TableFill("modify_time"<span style="color: #000000;">, FieldFill.INSERT_UPDATE)); tableFillList.add(</span><span style="color: #0000ff;">new</span> TableFill("create_time"<span style="color: #000000;">, FieldFill.INSERT)); </span><span style="color: #008000;">//</span><span style="color: #008000;">String tables[] = new String[]{"SYS_PERMISSION", "PK_SYS_PERMISSION", "T_BLOG", "PK_T_BLOG", "R_TAG_BLOG", "PK_R_TAG_BLOG", "SYS_PARAM", "SYS_USER", "PK_SYS_USER", "T_BLOG_CATEGORY", "PK_T_BLOG_CATEGORY", "T_BLOG_TAG", "PK_T_BLOG_TAG", "SYS_FUNCTION", "PK_T_COMMENT", "T_COMMENT_MSG", "R_USER_PERMISSION"};</span> String tables[] = <span style="color: #0000ff;">new</span> String[]{"activity_rules"<span style="color: #000000;">}; System.out.println(System.getProperty(</span>"user.dir"<span style="color: #000000;">)); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 代碼生成器</span> AutoGenerator mpg = <span style="color: #0000ff;">new</span><span style="color: #000000;"> AutoGenerator( ).setGlobalConfig( </span><span style="color: #008000;">//</span><span style="color: #008000;"> 全局配置</span> <span style="color: #0000ff;">new</span><span style="color: #000000;"> GlobalConfig() .setOutputDir(System.getProperty(</span>"user.dir") +"/src/main/java/")<span style="color: #008000;">//</span><span style="color: #008000;">輸出目錄</span> .setFileOverride(<span style="color: #0000ff;">true</span>)<span style="color: #008000;">//</span><span style="color: #008000;"> 是否覆蓋文件</span> .setActiveRecord(<span style="color: #0000ff;">false</span>)<span style="color: #008000;">//</span><span style="color: #008000;"> 開啟 activeRecord 模式</span> .setEnableCache(<span style="color: #0000ff;">false</span>)<span style="color: #008000;">//</span><span style="color: #008000;"> XML 二級緩存</span> .setBaseResultMap(<span style="color: #0000ff;">true</span>)<span style="color: #008000;">//</span><span style="color: #008000;"> XML ResultMap</span> .setBaseColumnList(<span style="color: #0000ff;">true</span>)<span style="color: #008000;">//</span><span style="color: #008000;"> XML columList </span><span style="color: #008000;">//</span><span style="color: #008000;">.setKotlin(true) </span><span style="color: #008000;">//</span><span style="color: #008000;">是否生成 kotlin 代碼</span> .setAuthor("zhoujiong"<span style="color: #000000;">) </span><span style="color: #008000;">//</span><span style="color: #008000;"> 自定義文件命名,注意 %s 會自動填充表實體屬性!</span> .setEntityName("%s"<span style="color: #000000;">) .setMapperName(</span>"%sMapper"<span style="color: #000000;">) .setXmlName(</span>"%sMapper"<span style="color: #000000;">) .setServiceName(</span>"I%sService"<span style="color: #000000;">) .setServiceImplName(</span>"%sServiceImpl"<span style="color: #000000;">) .setControllerName(</span>"%sController"<span style="color: #000000;">) .setOpen(</span><span style="color: #0000ff;">false</span>)<span style="color: #008000;">//</span><span style="color: #008000;">生成代碼后不自動打開文件所在目錄</span>
).setDataSource(
// 數據源配置
new DataSourceConfig()
.setDbType(DbType.MYSQL)// 數據庫類型
.setTypeConvert(new MySqlTypeConvert() {
// 自定義數據庫表字段類型轉換【可選】
@Override
public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
System.out.println("自動檢測到數據庫類型:" + fieldType);
//金額或者敏感數值單位需要精確
if (fieldType.contains("NUMBER(") && fieldType.contains("2)")) {//含有這種格式的識別為金額類型
return DbColumnType.BIG_DECIMAL;
//編碼枚舉數值類型或者布爾類型使用整形表示
} else if (fieldType.contains("NUMBER") && !fieldType.contains(",")) {
return DbColumnType.INTEGER;
} else {
//使用默認的
return super.processTypeConvert(globalConfig, fieldType);
}
}
})
.setDriverName("com.mysql.jdbc.Driver")
.setUsername("")
.setPassword("")
.setUrl("")
).setStrategy(
// 策略配置<span style="color: #0000ff;">new</span><span style="color: #000000;"> StrategyConfig() </span><span style="color: #008000;">//</span><span style="color: #008000;"> .setCapitalMode(true)</span><span style="color: #008000;">//</span><span style="color: #008000;"> 全局大寫命名 </span><span style="color: #008000;">//</span><span style="color: #008000;"> .setDbColumnUnderline(true)</span><span style="color: #008000;">//</span><span style="color: #008000;">全局下划線命名 </span><span style="color: #008000;">//</span><span style="color: #008000;">.setTablePrefix(new String[]{"t_"})</span><span style="color: #008000;">//</span><span style="color: #008000;"> 此處可以修改為您的表前綴</span> .setNaming(NamingStrategy.underline_to_camel)<span style="color: #008000;">//</span><span style="color: #008000;"> 表名生成策略 </span><span style="color: #008000;">//</span><span style="color: #008000;">.setInclude(new String[]{"SYS_USER","T_BLOG","R_TAG_BLOG","SYS_PARAM","T_BLOG_CATEGORY","T_BLOG_TAG","T_COMMENT","T_LEAVE_MSG"}) </span><span style="color: #008000;">//</span><span style="color: #008000;"> 需要生成的表</span> .setInclude(tables) <span style="color: #008000;">//</span><span style="color: #008000;"> 需要生成的表 </span><span style="color: #008000;">//</span><span style="color: #008000;"> .setExclude(new String[]{"test"}) </span><span style="color: #008000;">//</span><span style="color: #008000;"> 排除生成的表 </span><span style="color: #008000;">//</span><span style="color: #008000;"> 自定義實體父類</span> .setSuperEntityClass("com.mybatis.puls.mybatisPuls.entity.BaseEntity"<span style="color: #000000;">) </span><span style="color: #008000;">//</span><span style="color: #008000;"> 自定義實體,公共字段 </span><span style="color: #008000;">//</span><span style="color: #008000;">.setSuperEntityColumns(new String[]{"test_id"})</span>
.setTableFillList(tableFillList)
// 自定義 mapper 父類
// .setSuperMapperClass("com.mybatis.puls.mybatisPuls.mapper.BaseMapper")
// 自定義 service 父類
// .setSuperServiceClass("com.mybatis.puls.mybatisPuls.service.IBaseService")
// 自定義 service 實現類父類
// .setSuperServiceImplClass("com.mybatis.puls.mybatisPuls.service.impl.BaseServiceImpl")
// 自定義 controller 父類
.setSuperControllerClass("com.mybatis.puls.mybatisPuls.controller.BaseController")
// 【實體】是否生成字段常量(默認 false)
// public static final String ID = "test_id";
// .setEntityColumnConstant(true)
// 【實體】是否為構建者模型(默認 false)
// public SysUser setName(String name) {this.name = name; return this;}
// .setEntityBuilderModel(true)
// 【實體】是否為lombok模型(默認 false)<a href="https://projectlombok.org/">document</a>
.setEntityLombokModel(true)
// Boolean類型字段是否移除is前綴處理
.setEntityBooleanColumnRemoveIsPrefix(true)
//設置是否使用restController注解
.setRestControllerStyle(true)
// .setControllerMappingHyphenStyle(true)
//設置邏輯刪除字段,默認1有效,0無效
.setLogicDeleteFieldName("enabled")
).setPackageInfo(
// 包配置
new PackageConfig()
//.setModuleName("")
.setParent("com.mybatis.puls.mybatisPuls")// 自定義包路徑
.setEntity("entity")
.setMapper("mapper")
.setService("service")
.setServiceImpl("service.impl")
.setController("controller")).setCfg( </span><span style="color: #008000;">//</span><span style="color: #008000;"> 注入自定義配置,可以在 VM 中使用 cfg.abc 設置的值</span> <span style="color: #0000ff;">new</span><span style="color: #000000;"> InjectionConfig() { @Override </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> initMap() { Map</span><String, Object> map = <span style="color: #0000ff;">new</span> HashMap<><span style="color: #000000;">(); map.put(</span>"email", "@qq.com"<span style="color: #000000;">); </span><span style="color: #0000ff;">this</span><span style="color: #000000;">.setMap(map); } }.setFileOutConfigList(Collections.</span><FileOutConfig>singletonList(<span style="color: #0000ff;">new</span><span style="color: #000000;"> FileOutConfig( </span>"/generator-templates-custom/mapper.xml" + ((1 == result) ? ".ftl" : ".vm"<span style="color: #000000;">)) { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 自定義輸出文件目錄</span>
@Override
public String outputFile(TableInfo tableInfo) {
return System.getProperty("user.dir") +"/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper.xml";
}
}))
).setTemplate(
// 關閉默認 xml 生成,調整生成 至 根目錄
new TemplateConfig()
.setXml(null)
// 自定義模板配置,模板可以參考源碼 /mybatis-plus/src/main/resources/template 使用 copy
// 至您項目 src/main/resources/template 目錄下,模板名稱也可自定義如下配置:
.setController("generator-templates-custom/controller.java")
.setEntity("generator-templates-custom/entity.java")
.setMapper("generator-templates-custom/mapper.java")
.setService("generator-templates-custom/service.java")
.setServiceImpl("generator-templates-custom/serviceImpl.java")
);
// 執行生成
if (1 == result) {
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
}
mpg.execute();</span><span style="color: #008000;">//</span><span style="color: #008000;"> 打印注入設置,這里演示模板里面怎么獲取注入內容【可無】</span> System.err.println(mpg.getCfg().getMap().get("email"<span style="color: #000000;">)); }
}
准備模板文件
ftl文件是freemarker模板文件
vm文件是velocity模板文件
本地配置SQL執行效率查詢插件
結果顯示
配置分頁插件
詳細也可參照官網
運用
page.setCurrent 設置當前頁
page.setSize 設置一頁大小
page.setOptimizeCountSql (false)不進行 count sql 優化,解決 MP 無法自動優化 SQL 問題
page.setSearchCount 是否查詢總數
然后通過setRecords()方法返回Page對象。
其他插件請參照官網。(例如 注入sql自定義,自定義全局操作)
注意:
- 在建表時不要已condition作為表字段,在使用條件構造器wrapper時,雖然mybatis-plus支持關鍵詞自動轉義,但對condition無效!
- 運用了mybatis-plus就不需要在引入mybatis相關包,以免引起沖突!
2、調用
- 通過調用 service crud 接口,條件查詢數據庫中 id = 20 的數據。
- 通過自已定義mapper(與注入自定義SQL不同,自定義SQL注入是在初始化就講SQL方法注入到mybatis容器中,類似mybatis-plus中的 BaseMapper中的方法)
controller
service
繼承 com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
mapper
繼承 com.baomidou.mybatisplus.core.mapper.BaseMapper
mapper.xml
條件構造器wrapper
詳細構造器用法見文檔
條件參數說明
查詢方式 | 說明 |
---|---|
setSqlSelect | 設置 SELECT 查詢字段 |
where | WHERE 語句,拼接 + WHERE 條件 |
and | AND 語句,拼接 + AND 字段=值 |
andNew | AND 語句,拼接 + AND (字段=值) |
or | OR 語句,拼接 + OR 字段=值 |
orNew | OR 語句,拼接 + OR (字段=值) |
eq | 等於= |
allEq | 基於 map 內容等於= |
ne | 不等於<> |
gt | 大於> |
ge | 大於等於>= |
lt | 小於< |
le | 小於等於<= |
like | 模糊查詢 LIKE |
notLike | 模糊查詢 NOT LIKE |
in | IN 查詢 |
notIn | NOT IN 查詢 |
isNull | NULL 值查詢 |
isNotNull | IS NOT NULL |
groupBy | 分組 GROUP BY |
having | HAVING 關鍵詞 |
orderBy | 排序 ORDER BY |
orderAsc | ASC 排序 ORDER BY |
orderDesc | DESC 排序 ORDER BY |
exists | EXISTS 條件語句 |
notExists | NOT EXISTS 條件語句 |
between | BETWEEN 條件語句 |
notBetween | NOT BETWEEN 條件語句 |
addFilter | 自由拼接 SQL |
last | 拼接在最后,例如:last("LIMIT 1") |
原文地址:https://www.cnblogs.com/BestWishesZJ/p/10844815.html