Mybatis-Plus代码生成器
新版本:
public class MybatisGenerator { private static final String url = "jdbc:mysql://localhost:3306/xxx"; private static final String userName = "root"; private static final String password = "123456"; private static final String outPutDir = "C:/"; // 输出路径 private static final String[] tableNames = {"sys_role"}; private static final String packageName = "com.xxx.xxx"; private static final String superServiceClass = "com.baomidou.mybatisplus.extension.service.IService"; private static final String author = "xxx"; private static final String tablePrefix = ""; //去除表前缀 private static final String[] fieldPrefix = {}; //去除属性前缀 public static void main(String[] args) { MybatisGenerator.run(); } public static void run() { FastAutoGenerator.create(url, userName, password) .globalConfig(builder -> { builder.author(author) .outputDir(outPutDir); }) .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(packageName) .entity("entity") .controller("controller") .service("service") .serviceImpl("service.impl") .mapper("mapper") .xml("xml"); }) .strategyConfig(builder -> { builder.addFieldPrefix(fieldPrefix) .addTablePrefix(tablePrefix) .addInclude(tableNames); builder.controllerBuilder() .enableFileOverride() .formatFileName("%sController"); builder.serviceBuilder() .enableFileOverride() .superServiceClass(superServiceClass) .formatServiceFileName("%sService") .formatServiceImplFileName("%sServiceImpl"); builder.mapperBuilder() .enableFileOverride() .formatMapperFileName("%sMapper"); builder.entityBuilder() .enableFileOverride() .addTableFills(getTableFills()) .enableLombok(); }) .templateEngine(new VelocityTemplateEngine()) .execute(); } private static List<IFill> getTableFills() { List<IFill> list = new ArrayList<>(); list.add(new Column("create_by", FieldFill.INSERT)); list.add(new Column("create_time", FieldFill.INSERT)); list.add(new Column("update_by", FieldFill.UPDATE)); list.add(new Column("update_time", FieldFill.UPDATE)); return list; } }
旧版本:
public class MybatisGenerator { private static final DbType dbType = DbType.MYSQL; private static final String url = "jdbc:mysql://localhost:3306/xxx"; private static final String driver = "com.mysql.cj.jdbc.Driver"; private static final String userName = "root"; private static final String password = "123456"; private static final String outPutDir = "C:/"; // 输出路径 private static final String[] tableNames = {"sys_role"}; private static final String packageName = "com.xxx.xxx"; private static final String superServiceClass = "com.baomidou.mybatisplus.extension.service.IService"; private static final String superControllerClass = ""; private static final String superEntityClass = ""; private static final String author = "zxz"; private static final boolean isLombok = true; private static final String tablePrefix = ""; //去除表前缀 private static final String[] fieldPrefix = {}; //去除属性前缀 // 包名配置 private static final String controllerPath = packageName+".controller"; private static final String servicePath = packageName+".service"; private static final String serviceImplPath = packageName+".service.impl"; private static final String entityPath = packageName+".entity"; private static final String mapperPath = packageName+".mapper"; private static final String xmlPath = packageName + ".xml"; public static void main(String[] args) { MybatisGenerator.run(); } public static void run() { AutoGenerator autoGenerator = new AutoGenerator(); // 1.配置数据源 autoGenerator.setDataSource(getDataSourceConfig()); // 2.全局配置 autoGenerator.setGlobalConfig(getGlobalConfig()); // 3.策略配置 autoGenerator.setStrategy(getStrategyConfig()); // 4.包路径配置 autoGenerator.setPackageInfo(getPackageConfig()); // 5.模板文件配置 autoGenerator.setTemplate(getTemplateConfig()); // 6.设置模板引擎 autoGenerator.setTemplateEngine(new VelocityTemplateEngine()); autoGenerator.setCfg(new InjectionConfig() { @Override public void initMap() { List<TableInfo> tableInfoList = this.getConfig().getTableInfoList(); System.out.println(tableInfoList); } }); // 7.执行生成文件 autoGenerator.execute(); } private static TemplateConfig getTemplateConfig() { return new TemplateConfig() .setController("templates/controller.java.vm") .setEntity("templates/entity.java.vm") .setService("templates/service.java.vm") .setServiceImpl("templates/serviceImpl.java.vm") .setMapper("templates/mapper.java.vm") .setXml("templates/mapper.xml.vm"); } private static PackageConfig getPackageConfig() { return new PackageConfig() .setParent("") .setXml(xmlPath) .setMapper(mapperPath) .setController(controllerPath) .setEntity(entityPath) .setService(servicePath) .setServiceImpl(serviceImplPath); } @SuppressWarnings({"ConstantConditions"}) private static StrategyConfig getStrategyConfig() { StrategyConfig strategyConfig = new StrategyConfig() // 全局大写命名ORACLE注意 .setCapitalMode(true) //从数据库表到文件的命名策略 .setNaming(NamingStrategy.underline_to_camel) //需要生成的的表名,多个表名传数组 .setInclude(tableNames) //使用lombok .setSuperServiceClass(superServiceClass) .setEntityLombokModel(isLombok) //rest风格 .setRestControllerStyle(true) .setEntitySerialVersionUID(true) .setEntityTableFieldAnnotationEnable(true) .setEntityBooleanColumnRemoveIsPrefix(true) .setTableFillList(getTableFills()); if (!superControllerClass.isEmpty()) { strategyConfig.setSuperControllerClass(superControllerClass); } if (!superEntityClass.isEmpty()) { strategyConfig.setSuperEntityClass(superEntityClass); } if (!tablePrefix.isEmpty()) { strategyConfig.setTablePrefix(tablePrefix); } if (fieldPrefix.length > 0) { strategyConfig.setFieldPrefix(fieldPrefix); } return strategyConfig; } private static GlobalConfig getGlobalConfig() { return new GlobalConfig() .setBaseColumnList(true) .setBaseResultMap(true) .setActiveRecord(false) .setAuthor(author) // 作者 .setOutputDir(outPutDir) //设置输出路径 .setFileOverride(true) .setServiceName("%sService") .setServiceImplName("%sServiceImpl"); } private static DataSourceConfig getDataSourceConfig() { return new DataSourceConfig() .setDbType(dbType) .setUrl(url) .setDriverName(driver) .setUsername(userName) .setPassword(password); } private static List<TableFill> getTableFills() { List<TableFill> tableFills = new ArrayList<>(); TableFill createdByFill = new TableFill("create_by", FieldFill.INSERT); TableFill createdTimestampFill = new TableFill("create_time", FieldFill.INSERT); TableFill updatedByFill = new TableFill("update_by", FieldFill.UPDATE); TableFill updatedTimestampFill = new TableFill("update_time", FieldFill.UPDATE); tableFills.add(createdByFill); tableFills.add(createdTimestampFill); tableFills.add(updatedByFill); tableFills.add(updatedTimestampFill); return tableFills; } }
==END==
作者:[一柒微笑]