代码生成器
AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。
public static void main(String[] args) {
AutoGenerator generator = new AutoGenerator();
//1.全局配置
GlobalConfig config = new GlobalConfig();
//获取项目路径
String path = System.getProperty("user.dir");
//设置代码输出目录
config.setOutputDir(path + "/src/main/java");
//设置作者
config.setAuthor("枫叶");
//去除servicesI前缀
config.setServiceName("%sService");
//主键生成策略使用雪花算法
config.setIdType(IdType.ASSIGN_ID);
//使用Swagger2
config.setSwagger2(true);
generator.setGlobalConfig(config);
//2.设置数据源
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.MYSQL);
dsc.setUrl("jdbc:mysql://www.maplexl.xyz:3306/db2020?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8");
dsc.setUsername("MyDream");
dsc.setPassword("maple1234");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
generator.setDataSource(dsc);
//3.设置包名
PackageConfig packageConfig = new PackageConfig();
packageConfig.setModuleName("mybatisplus");
packageConfig.setParent("com.maple");
//设置Entity包名,默认是entity
packageConfig.setEntity("pojo");
generator.setPackageInfo(packageConfig);
//4.策略设置
StrategyConfig strategyConfig = new StrategyConfig();
//设置要映射的表名,参数为String... include
strategyConfig.setInclude("user");
//设置下划线转驼峰
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
//自动Lombok
strategyConfig.setEntityLombokModel(true);
//设置逻辑删除列
strategyConfig.setLogicDeleteFieldName("is_deleted");
//自动填充配置
TableFill gmt_create = new TableFill("gmt_create", FieldFill.INSERT);
TableFill gmt_modify = new TableFill("gmt_modify", FieldFill.INSERT_UPDATE);
ArrayList<TableFill> tableFills = new ArrayList<>();
tableFills.add(gmt_create);
tableFills.add(gmt_modify);
strategyConfig.setTableFillList(tableFills);
//乐观锁
strategyConfig.setVersionFieldName("version");
strategyConfig.setRestControllerStyle(true);
strategyConfig.setControllerMappingHyphenStyle(true);
generator.setStrategy(strategyConfig);
generator.execute();
}