Spring boot整合Mybatis-plus過程解析
Mybatis初期使用比較麻煩,需要很多配置文件、實(shí)體類、dao層映射、還有很多其他的配置。初期開發(fā)使用generator可以根據(jù)表結(jié)構(gòu)自動(dòng)生產(chǎn)實(shí)體類、dao層代碼,這樣是可以減輕一部分開發(fā)量;后期mybatis進(jìn)行大量的優(yōu)化,現(xiàn)在可以使用注解版本,自動(dòng)管理dao層和配置文件。
maven 依賴 注意:本文使用的是mysql,數(shù)據(jù)庫(kù)依賴就不展示了
<!-- 引入mvbatie -plus starter-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>2.3</version>
</dependency>
<!-- 模板引擎 mybatis使用code生成代碼需要 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
代碼模版引擎需要velocity或freemarker(mybatis-plus默認(rèn)使用velocity,兩者任選其一),這里使用velocity
代碼生成器
public static void main(String[] args) {
CodeGeneration codeGeneration = new CodeGeneration();
codeGeneration.execute();
}
/**
*
* @ClassName: CodeGeneration
* @Description: 代碼生成器
*/
public void execute() {
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
//生成的代碼路徑(系統(tǒng)路徑)
gc.setOutputDir("/Users/wangxiaowei/wxw/eclipseWorkSpace/study/src/main/java");
gc.setFileOverride(true);
gc.setActiveRecord(false);// 不需要ActiveRecord特性的請(qǐng)改為false
gc.setEnableCache(false);// XML 二級(jí)緩存
gc.setBaseResultMap(true);// XML ResultMap
gc.setBaseColumnList(false);// XML columList
gc.setAuthor("wxw");// 作者
// 自定義文件命名,注意 %s 會(huì)自動(dòng)填充表實(shí)體屬性!
gc.setControllerName("%sController");
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");
gc.setMapperName("%sDao");
gc.setXmlName("%sMapper");
mpg.setGlobalConfig(gc);
// 數(shù)據(jù)源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.MYSQL);
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("xxx");//數(shù)據(jù)庫(kù)用戶名
dsc.setPassword("xxx");//密碼
//數(shù)據(jù)庫(kù)路徑
dsc.setUrl(
"jdbc:mysql://localhost:30870/horus_dev?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true");
mpg.setDataSource(dsc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setTablePrefix(new String[] { "" });// 此處可以修改為您的表前綴
strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
// 需要生成的表的名稱,這里app_user_info即為表名
strategy.setInclude(new String[] {
"app_user_info",
});
strategy.setSuperServiceClass(null);
strategy.setSuperServiceImplClass(null);
strategy.setSuperMapperClass(null);
mpg.setStrategy(strategy);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.wang.study");//父包,下面的子包均在這父包之下
pc.setController("controller");//上面生成的controller類 放到controller子包
pc.setService("service");//上面生成的service 放到service子包,下面類似
pc.setMapper("dao");
pc.setEntity("pojo");
pc.setXml("mapper");
mpg.setPackageInfo(pc);
// 執(zhí)行生成
mpg.execute();
}
mybatis 基礎(chǔ)配置(這里使用的properties)
#數(shù)據(jù)源
spring.datasource.url=jdbc:mysql://localhost:30870/horus_dev?useUnicode=true&characterEncoding=utf-8
spring.datasource.username =xxx
spring.datasource.password =xxx
spring.datasource.type =com.alibaba.druid.pool.DruidDataSource
#mapper文件
mybatis-plus.mapper-locations=classpath:com/wang/study/mapper/*.xml
#數(shù)據(jù)庫(kù)表對(duì)應(yīng)的實(shí)體類所在包
mybatis-plus.type-aliases-package=com/wang/study/pojo
#日志 打印sql
logging.level.com.wang.study.dao=debug
mybatis-plus 分頁,在配置類里添加以下配置
/**
* mybatis-plus分頁插件<br>
* 文檔:http://mp.baomidou.com<br>
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
paginationInterceptor.setDialectType("mysql");
return paginationInterceptor;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java替換int數(shù)組中重復(fù)數(shù)據(jù)的方法示例
這篇文章主要介紹了Java替換int數(shù)組中重復(fù)數(shù)據(jù)的方法,涉及java針對(duì)數(shù)組的遍歷、轉(zhuǎn)換、判斷等相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
Java數(shù)據(jù)結(jié)構(gòu)之稀疏矩陣定義與用法示例
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)之稀疏矩陣定義與用法,結(jié)合實(shí)例形式分析了java稀疏矩陣的定義、運(yùn)算、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
Java?中很好用的數(shù)據(jù)結(jié)構(gòu)EnumSet
這篇文章主要介紹了Java?中很好用的數(shù)據(jù)結(jié)構(gòu)EnumSet,EnumMap即屬于一個(gè)Map,下文圍繞主題展開詳細(xì)內(nèi)容,需要的小伙伴可以參考參考一下2022-05-05
使用AbstractRoutingDataSource實(shí)現(xiàn)數(shù)據(jù)源動(dòng)態(tài)切換的實(shí)例
AbstractRoutingDataSource 是 Spring 框架提供的一個(gè)抽象類,用于實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源路由,這個(gè)類主要用于多數(shù)據(jù)源場(chǎng)景,其中可以根據(jù)不同的條件動(dòng)態(tài)地切換到不同的數(shù)據(jù)源,本文給大家介紹了如何使用AbstractRoutingDataSource實(shí)現(xiàn)數(shù)據(jù)源動(dòng)態(tài)切換,需要的朋友可以參考下2024-03-03
Servlet實(shí)現(xiàn)簡(jiǎn)單的用戶登錄功能實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于利用Servlet實(shí)現(xiàn)簡(jiǎn)單的用戶登錄功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Mybatis generator mapper文件覆蓋原文件的示例代碼
這篇文章主要介紹了Mybatis generator mapper文件覆蓋原文件,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
Mac OS X 下 IntelliJ IDEA、jEdit 等 Java 程序中文標(biāo)點(diǎn)輸入無效的完美解決方法
Mac OS X 下基于 Java 的程序會(huì)出現(xiàn)中文標(biāo)點(diǎn)輸入無效的問題,在中文輸入法狀態(tài),可以輸入中文字,但輸入中文標(biāo)點(diǎn)最后上去的是英文標(biāo)點(diǎn).這篇文章主要介紹了Mac OS X 下 IntelliJ IDEA、jEdit 等 Java 程序中文標(biāo)點(diǎn)輸入無效的完美解決方法,需要的朋友可以參考下2016-10-10

