mybatisPlus 代碼生成器
前言
平時寫的pojo物體類都比較繁瑣,特別是當表多的時候,要一個個對著寫,還可能會寫錯,那么mybatisplus可以為我們解決這些問題
使用步驟
1.匯入所需要的依賴
2.配置資料源,mybaitsplus
3.撰寫代碼生成器類
4.測驗結果
1.匯入所需要的依賴
pom.xml
<!--mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--物體類的幫助注解 例如getset(@Data)-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<!--代碼生成器-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<!--模板引擎-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>3.3.2.RELEASE</version>
</dependency>
2.配置資料源,mybaitsplus
這里我使用的是yml進行配置
application.yml
spring:
datasource:
username: 用戶名
password: 密碼
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://url:3306/資料庫名?userUnicode=true&characterEncoding=gbk&serverTimezone=UTC&useSSL=false
# 資料源其他配置
initialSize: 1
minIdle: 3
maxActive: 20
initialization-mode: ALWAYS
# 配置獲取連接等待超時的時間
maxWait: 120000
# 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒
timeBetweenEvictionRunsMillis: 120000
# 配置一個連接在池中最小生存的時間,單位是毫秒
minEvictableIdleTimeMillis: 30000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打開PSCache,并且指定每個連接上PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# 配置監控統計攔截的filters,去掉后監控界面sql無法統計,'wall'用于防火墻
filters: stat,wall,slf4j
# 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
jackson:
data-format: yyyy-MM-dd HH:mm
time-zone: GMT+8
#設定開發環境
profiles:
active: dev
mybatis-plus:
global-config:
db-config:
#配置邏輯洗掉
logic-delete-value: 1
logic-not-delete-value: 0
configuration:
#開配置日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
3.撰寫代碼生成器類
TableCode.java
public class TableCode {
public static void main(String[] args) { // 需要構建一個 代碼自動生成器 物件
AutoGenerator mpg = new AutoGenerator(); // 配置策略
// 1、全域配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("RT");//設定作者名
gc.setOpen(false);
gc.setFileOverride(false); // 是否覆寫
gc.setServiceName("%sService"); // 去Service的I前綴
gc.setIdType(IdType.ID_WORKER);
gc.setDateType(DateType.ONLY_DATE);
gc.setSwagger2(true);
mpg.setGlobalConfig(gc);
//2、設定資料源
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://url:3306/資料庫名?userUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("用戶名");
dsc.setPassword("密碼");
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
//3、包的配置
PackageConfig pc = new PackageConfig();
//下面都是自己命名
pc.setModuleName("");
pc.setParent("com.railtransit");
pc.setEntity("entity");
pc.setMapper("mapper");
pc.setService("service");
pc.setController("Controller");
mpg.setPackageInfo(pc);
//4、策略配置
StrategyConfig strategy = new StrategyConfig();
//下面可以選擇該資料庫中哪些表進行代碼的生成
strategy.setInclude("ml_data","station_all","trips_count","trips_daycount","trips_instationname","trips_month","trips_morning","trips_night","trips_od","trips_outstationname","trips_price","trips_sitetosite","trips_week","users_age","users_gender","workday_count"); // 設定要映射的表名
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true); // 自動lombok;
//以下可以根據自己的需求來做,就不詳細說明是什么了
//strategy.setLogicDeleteFieldName("deleted"); // 自動填充配置
//TableFill gmtCreate = new TableFill("create_time", FieldFill.INSERT);
//TableFill gmtModified = new TableFill("update_time", FieldFill.INSERT_UPDATE);
//ArrayList<TableFill> tableFills = new ArrayList<>();
//tableFills.add(gmtCreate);
//tableFills.add(gmtModified);
//strategy.setTableFillList(tableFills); // 樂觀鎖
//strategy.setVersionFieldName("version");
strategy.setRestControllerStyle(true);
strategy.setControllerMappingHyphenStyle(true); // localhost:8080/hello_id_2
mpg.setStrategy(strategy);
mpg.execute(); //執行
}
}
4.測驗結果
執行Table.class中的main方法即可
結果:

記錄我的學習筆記
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/267070.html
標籤:java
上一篇:Map與Set詳解
