整合代码生成器过程中,发现好多博主提供的无法使用,自己整合了一套,没有花里胡哨,直接可用
备注:常规的依赖自己导入,提供的这套,默认已经导入了mybatis-plus,srpingboot等依赖了.
1.maven依赖导入,版本号要与自己的版本号想同
<!--代码生成器依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.0</version>
</dependency>
<!--代码生成器引擎-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
</dependency>
2.导入插件
<!-- mybatis generator 自动生成代码插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
3.创建生成代码类
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.rules.NamingStrategy;
/**
* 代码生成器
* @Author: test
* @Date: 2023/11/08/9:07
* @Version: 1.0
*/
public class MybatisPlusCodeGenerator {
public static void main(String[] args) {
//step1:代码生成器
AutoGenerator autoGenerator = new AutoGenerator();
//step2:配置全局信息,包括输出路径、包名、作者等信息
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setOutputDir("D:/a-service/src/main/java");
globalConfig.setAuthor("liubc");
globalConfig.setOpen(false);
autoGenerator.setGlobalConfig(globalConfig);
//step3:
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setUrl("jdbc:mysql://127.0.0.1:3306/a_dwx?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai");
dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
dataSourceConfig.setUsername("root");
dataSourceConfig.setPassword("root.");
autoGenerator.setDataSource(dataSourceConfig);
//step4:
PackageConfig packageConfig = new PackageConfig();
packageConfig.setParent("com.a.devicewx");
packageConfig.setModuleName("user");
packageConfig.setEntity("entity");
packageConfig.setMapper("mapper");
packageConfig.setService("service");
packageConfig.setController("mgt.api");
packageConfig.setXml("mapper.xml");
autoGenerator.setPackageInfo(packageConfig);
//step5:
StrategyConfig strategyConfig = new StrategyConfig();
//数据库表名
strategyConfig.setInclude("at_member_history");
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
strategyConfig.setEntityLombokModel(true);
strategyConfig.setRestControllerStyle(true);
strategyConfig.setControllerMappingHyphenStyle(true);
//此处不同的版本号可能不同,有的版本不要set,直接entity...enable
strategyConfig.setEntityTableFieldAnnotationEnable(true);
strategyConfig.setTablePrefix("t_");
autoGenerator.setStrategy(strategyConfig);
//step6:
autoGenerator.execute();
}
}
执行main方法生成代码