第一步、
再数据库里创建表
第二步、
在项目pom.xml文件中添加相关依赖:
注意:看对自己的mysql的版本号
<?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.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
</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.example.demo;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.lang.reflect.Field;
import java.util.*;
@SpringBootApplication
public class MysqlGenerator {
public static int scanner() {
Scanner scanner = new Scanner(System.in);
StringBuilder h = new StringBuilder();
h.append("!!代码生成,输入0表示使用Velocity引擎!!");
h.append("\n对照表");
h.append("\n0= Velocity 引擎");
h.append("\n1 = Freemarker 引擎");
h.append("\n请输入");
System.out.println(h.toString());
int slt = 0;
//现在有输入数据
if(scanner.hasNext()){
String ipt = scanner.next();
if("1".equals(ipt)){
slt=1;
}
}
return slt;
}
public static void main(String[] args) {
int result =scanner();
//自定义需要填充的字段
List<TableFill> tableFillList = new ArrayList<TableFill>();
tableFillList.add(new TableFill("create_user", FieldFill.INSERT));
tableFillList.add(new TableFill("update_user",FieldFill.UPDATE));
tableFillList.add(new TableFill("create_time",FieldFill.INSERT));
tableFillList.add(new TableFill("update_time",FieldFill.UPDATE));
//代码生成器
AutoGenerator mpg = new AutoGenerator().setGlobalConfig(
new GlobalConfig()
.setOutputDir("D:\\Ds") //输出目录
.setFileOverride(true) //是否覆盖文件
.setActiveRecord(true) //开启activeRecord模式
.setEnableCache(false) //xml 二级缓存
.setBaseResultMap(true) //xml resultMap
.setBaseColumnList(true) //xml columList
.setMapperName("%sMapper")
.setXmlName("%sMapper")
.setServiceName("%sService")
.setServiceImplName("%sServiceImpl")
.setControllerName("%sController")
).setDataSource(
new DataSourceConfig()
.setDriverName("com.mysql.cj.jdbc.Driver")
.setUsername("root")
.setPassword("123456")
.setUrl("jdbc:mysql://localhost:3306/puladuo?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=UTC")
).setPackageInfo(
new PackageConfig()
.setModuleName("config")
.setParent("com.example.demo.sys")
.setController("controller")
).setCfg(
new InjectionConfig() {
@Override
public void initMap() {
Map<String,Object> map = new HashMap<>();
map.put("abc",this.getConfig().getGlobalConfig().getAuthor()+"-mp");
this.setMap(map);
}
}.setFileOutConfigList(Collections.<FileOutConfig>singletonList(new FileOutConfig(
"/templates/mapper.xml" + ((1 == result) ? ".ftl" : ".vm")) {
@Override
public String outputFile(TableInfo tableInfo) {
return "D:/mapping/" + tableInfo.getEntityName() + "Mapper.xml";
}
}))
).setTemplate(
new TemplateConfig().setXml(null)
);
StrategyConfig strategyConfig= new StrategyConfig();
strategyConfig
.setEntityLombokModel(true)
.setTablePrefix("sys_")
.setNaming(NamingStrategy.underline_to_camel)
.setInclude("sys_config")
.setSuperEntityClass("com.common.platform.sys.base.pojo.BaseEntity")
.setSuperEntityColumns("create_time","update_time","create_user","update_user")
.setSuperControllerClass("com.common.platform.sys.base.controller.BaseController");
mpg.setStrategy(strategyConfig);
//执行生成
if(1 == result){
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
}
mpg.execute();
System.out.println(mpg.getCfg().getMap().get("abc"));
}
}
第四步
运行成功
第五步、
最后再文件夹里显示