<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>3.1.5version>
<relativePath/>
parent>
<groupId>com.giser.mpgroupId>
<artifactId>giser-java-mybatis-plusartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>giser-java-mybatis-plusname>
<description>giser-java-mybatis-plusdescription>
<properties>
<java.version>17java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.5.4.1version>
<exclusions>
<exclusion>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-coreartifactId>
<version>3.5.4.1version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-extensionartifactId>
<version>3.5.4.1version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-generatorartifactId>
<version>3.5.4.1version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>dynamic-datasource-spring-boot-starterartifactId>
<version>3.6.1version>
<exclusions>
<exclusion>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-aopartifactId>
exclusion>
<exclusion>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
<version>3.1.5version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.33version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-freemarkerartifactId>
<version>3.1.5version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-autoconfigureartifactId>
<version>3.1.0version>
dependency>
dependencies>
<build>
<finalName>gis-mpfinalName>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<version>2.5.4version>
<configuration>
<mainClass>com.giser.mp.GiserJavaMybatisPlusApplicationmainClass>
configuration>
plugin>
plugins>
build>
project>
这里只是基础版,可以进行更多的配置,脱离低级简单的工作内容。
package com.giser.mp.generator;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.Scanner;
public class GeneratorCode {
public static void main(String[] args) {
/**
* 数据源配置
*/
String url = "jdbc:mysql://localhost:3306/db_mybatis_plus?useSSL=false&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=GMT%2B8";
String username = "root";
String password = "root";
DataSourceConfig dataSourceConfig =
new DataSourceConfig
.Builder(url, username, password)
.typeConvert(new MySqlTypeConvert())
.build();
/**
* 代码生成器
*/
AutoGenerator mpg = new AutoGenerator(dataSourceConfig);
/**
* 全局配置
*/
String projectPath = System.getProperty("user.dir");
GlobalConfig globalConfig =
new GlobalConfig
.Builder()
.outputDir(projectPath + "/giser-java-mybatis-plus/src/test/java/com/giser/mp/common/")
.author("your name")
.enableSwagger()
.dateType(DateType.ONLY_DATE)
.disableOpenDir()
// .fileOverride()
.build();
mpg.global(globalConfig);
/**
* 包配置
*/
PackageConfig pc =
new PackageConfig
.Builder()
.moduleName(scanner("模块名"))
.build();
mpg.packageInfo(pc);
/**
* 配置模板
*/
TemplateConfig templateConfig =
new TemplateConfig
.Builder()
.xml(null)
.build();
mpg.template(templateConfig);
/**
* 策略配置
*/
StrategyConfig strategy =
new StrategyConfig.Builder()
.addInclude(scanner("表名,多个逗号分割").split(","))
.addTablePrefix("t_")
.enableCapitalMode()
.build();
// 控制器配置
strategy.controllerBuilder()
.enableFileOverride()
.enableRestStyle()
.enableHyphenStyle();
// 实体配置构建:设置自定义继承的Entity类全称,带包名
strategy.entityBuilder()
.enableFileOverride()
.enableLombok()
// .superClass("com.gis.business.pojo.entity.BaseEntity")
.build();
//设置命名格式
// strategy.setNaming(NamingStrategy.underline_to_camel);
// strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.serviceBuilder()
.enableFileOverride()
.build();
strategy.mapperBuilder()
.enableFileOverride()
.mapperAnnotation(org.apache.ibatis.annotations.Mapper.class)
.build();
mpg.strategy(strategy);
FreemarkerTemplateEngine freemarkerTemplateEngine = new FreemarkerTemplateEngine();
mpg.execute(freemarkerTemplateEngine);
}
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotBlank(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确" + tip + "!");
}
}
package com.giser.mp.generator;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.giser.mp.common.BaseEntity;
import org.apache.commons.lang3.StringUtils;
import java.sql.Types;
import java.util.Collections;
import java.util.Scanner;
/**
* @description 新的代码生成器
适用版本:mybatis-plus-generator 3.5.1 及其以上版本,对历史版本不兼容!
*
* @author giserDev
* @date 2023-11-23 19:11:54
*/
public class GeneratorCodeNew {
public static void main(String[] args) {
fastGenMode();
}
private static void fastGenMode() {
/**
* 数据源配置
*/
String url = "jdbc:mysql://localhost:3306/db_mybatis_plus?useSSL=false&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=GMT%2B8";
String username = "root";
String password = "root";
String projectPath = System.getProperty("user.dir");
FastAutoGenerator.create(url, username, password)
.globalConfig(builder -> {
builder.author("baomidou") // 设置作者
.enableSwagger() // 开启 swagger 模式
.fileOverride() // 覆盖已生成文件
.outputDir(projectPath + "/giser-java-mybatis-plus/src/test/java/com/giser/mp/common/"); // 指定输出目录
})
.dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
int typeCode = metaInfo.getJdbcType().TYPE_CODE;
if (typeCode == Types.SMALLINT) {
// 自定义类型转换
return DbColumnType.INTEGER;
}
return typeRegistry.getColumnType(metaInfo);
}))
.packageConfig(builder -> {
builder.parent("com.giser.mp") // 设置父包名
.moduleName("user") // 设置父包模块名
.pathInfo(Collections.singletonMap(OutputFile.xml, "D://")); // 设置mapperXml生成路径
})
.strategyConfig(builder -> {
builder.addInclude(scanner("表名,多个逗号分割").split(",")) // 设置需要生成的表名
// 设置过滤表前缀
.addTablePrefix("t_", "c_")
// controller
.controllerBuilder()
.enableFileOverride()
.enableRestStyle()
.enableHyphenStyle()
// entity
.entityBuilder()
.enableFileOverride()
.enableLombok()
// 基类
.superClass(BaseEntity.class)
.columnNaming(NamingStrategy.underline_to_camel)
// 启用lombok
.enableLombok()
// chain
.enableChainModel()
// service
.serviceBuilder()
.enableFileOverride()
// 去掉默认的I前缀
.formatServiceFileName("%sService")
// mapper
.mapperBuilder()
.enableFileOverride()
.mapperAnnotation(org.apache.ibatis.annotations.Mapper.class)
;
})
// 使用Freemarker引擎模板,默认的是Velocity引擎模板
.templateEngine(new FreemarkerTemplateEngine())
// 将mybatis-plus-generator中的templates模板文件拷贝出来,并根据自身项目需要进行修改,如配置swagger等
.templateConfig(builder -> {
builder.controller("/mybatisplustemplate/controller.java");
builder.service("/mybatisplustemplate/service.java");
builder.serviceImpl("/mybatisplustemplate/serviceImpl.java");
builder.mapper("/mybatisplustemplate/mapper.java");
builder.entity("/mybatisplustemplate/entity.java");
builder.xml("/mybatisplustemplate/mapper.xml");
})
.execute();
}
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help);
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotBlank(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确" + tip + "!");
}
}