首先,引入pom文件
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.6</version> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.1</version> </dependency>
然后,代码生成器如下:
package domain; import com.baomidou.mybatisplus.annotation.DbType; 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; import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine; /** * 生成代码工具 */ public class Generate { public static void main(String[] args) { Generate g = new Generate(); boolean startWithI = true; String projectName = "Demo"; String packageName = "com.xp.boot.web"; String tableName = "test_log"; g.generateByTables(startWithI,projectName,packageName,tableName); } /** * 根据表自动生成 * * @param serviceNameStartWithI 默认为false * @param packageName 包名 * @param tableNames 表名 */ private void generateByTables(boolean serviceNameStartWithI,String projectName, String packageName, String... tableNames) { //配置数据源 DataSourceConfig dataSourceConfig = getDataSourceConfig(); // 策略配置 StrategyConfig strategyConfig = getStrategyConfig(tableNames); //全局变量配置 GlobalConfig globalConfig = getGlobalConfig(serviceNameStartWithI,projectName); //包名配置 PackageConfig packageConfig = getPackageConfig(packageName); //自动生成 atuoGenerator(dataSourceConfig, strategyConfig, globalConfig, packageConfig); } /** * 集成 * * @param dataSourceConfig 配置数据源 * @param strategyConfig 策略配置 * @param config 全局变量配置 * @param packageConfig 包名配置 */ private void atuoGenerator(DataSourceConfig dataSourceConfig, StrategyConfig strategyConfig, GlobalConfig config, PackageConfig packageConfig) { new AutoGenerator() .setGlobalConfig(config) .setDataSource(dataSourceConfig) .setStrategy(strategyConfig) .setPackageInfo(packageConfig) .setTemplateEngine(new VelocityTemplateEngine()) .execute(); } /** * 设置包名 * * @param packageName 父路径包名 * @return PackageConfig 包名配置 */ private PackageConfig getPackageConfig(String packageName) { return new PackageConfig() .setParent(packageName) .setXml("mapper") .setMapper("mapper") .setController("controller") .setService("service") .setEntity("entity"); } /** * 全局配置 * * @param serviceNameStartWithI false * @return GlobalConfig */ private GlobalConfig getGlobalConfig(boolean serviceNameStartWithI,String projectName) { GlobalConfig globalConfig = new GlobalConfig(); globalConfig .setBaseColumnList(true) .setBaseResultMap(true) .setActiveRecord(false) .setAuthor("尼古拉斯-肖") //设置输出路径 .setOutputDir(System.getProperty("user.dir")+"/"+projectName+"/src/main/java/") .setFileOverride(true); if (!serviceNameStartWithI) { //设置service名 globalConfig.setServiceName("%sService"); } return globalConfig; } /** * 策略配置 * * @param tableNames 表名 * @return StrategyConfig */ private StrategyConfig getStrategyConfig(String... tableNames) { return new StrategyConfig() // 全局大写命名 ORACLE 注意 .setCapitalMode(true) .setEntityLombokModel(true) .setRestControllerStyle(false) //从数据库表到文件的命名策略 .setNaming(NamingStrategy.underline_to_camel) .setColumnNaming(NamingStrategy.underline_to_camel) //需要生成的的表名,多个表名传数组 .setInclude(tableNames); } /** * 配置数据源 * * @return 数据源配置 DataSourceConfig */ private DataSourceConfig getDataSourceConfig() { return new DataSourceConfig().setDbType(DbType.MYSQL) .setUrl("jdbc:mysql://localhost:3306/test?serverTimezone=Hongkong&nullCatalogMeansCurrent=true&characterEncoding=UTF-8&useSSL=false") .setUsername("root") .setPassword("123456") .setDriverName("com.mysql.cj.jdbc.Driver"); } }
配置好数据库,以及表名、包名等参数后,main方法运行: