<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wang</groupId>
<artifactId>mybatis_plus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatis_plus</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<!--資料庫驅動-->
<dependencies>
<!-- 檔案上傳-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.48</version>
</dependency>
<!-- druid 連接配接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<!-- log4j日志-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--htymeleaf導入插件 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--格式化時間類型 -->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<!-- velocity 模闆引擎,MyBatisPlus代碼生成器需要-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<!-- 注釋處理器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.yongdeng;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.ArrayList;
//代碼自動生成器
public class Code {
public static void main(String[] args) {
//需要建構一個代碼自動生成器對象
AutoGenerator mpg = new AutoGenerator();
//配置政策
//1.全局配置
GlobalConfig gc = new GlobalConfig();
//擷取系統目前目錄
String property = System.getProperty("user.dir");
//代碼生成到這個目錄下
gc.setOutputDir(property+"/src/main/java");
gc.setAuthor("王");//代碼上注釋的作者
gc.setOpen(false);//是否打開資料總管(代碼生成後打開代碼所在檔案夾)
gc.setFileOverride(true);//是否覆寫原來生成的
gc.setServiceName("%sService");//去Service的I字首
gc.setIdType(IdType.ID_WORKER);//生成政策
gc.setDateType(DateType.ONLY_DATE);//時間類型
gc.setSwagger2(true);//自動配置swagger文檔
mpg.setGlobalConfig(gc);
//2.設定資料源(配使用者名連結密碼連接配接資料庫)
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/decorate?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
dsc.setDbType(DbType.MYSQL);//資料庫的類型
mpg.setDataSource(dsc);
//3.包的配置
PackageConfig pc = new PackageConfig();
pc.setModuleName("blog");//子產品名字
pc.setParent("com.yongdeng");//生成子產品在這個路徑下
pc.setEntity("entity");
pc.setMapper("mapper");
pc.setService("service");
pc.setController("controller");
mpg.setPackageInfo(pc);
//政策配置
StrategyConfig strategy = new StrategyConfig();
strategy.setInclude("user");//要生成的表名,想要生成哪個表的代碼就填表名,可傳多個參數,","隔開
strategy.setNaming(NamingStrategy.underline_to_camel);//下劃線轉駝峰命名
strategy.setColumnNaming(NamingStrategy.underline_to_camel);//列的名字下劃線轉駝峰
strategy.setEntityLombokModel(true); //是否使用lombok開啟注解鍊式變成是否支援
strategy.setLogicDeleteFieldName("deleted");//邏輯删除(deleted表明)
//自動填充配置
TableFill tableFill = new TableFill("user", FieldFill.INSERT);//建立時自動填充政策user資料庫表
TableFill tableFill1 = new TableFill("user", FieldFill.INSERT_UPDATE);//修改時
ArrayList<TableFill> list = new ArrayList<>();
list.add(tableFill);
list.add(tableFill1);
strategy.setTableFillList(list);
//樂觀鎖
strategy.setVersionFieldName("version");//樂觀鎖
strategy.setRestControllerStyle(true);//開啟駝峰命名格式
strategy.setControllerMappingHyphenStyle(true);//localhost:8080/hello_id_2
mpg.execute();//執行
}
}