使用Mybatis生成代码
1.导包
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.1.0</version>
</dependency>
2.编写自动生成的代码
public void test(){
AutoGenerator mpg = new AutoGenerator();
GlobalConfig gc = new GlobalConfig();
String objectPath = System.getProperty("user.dir");
gc.setOutputDir(objectPath+"/src/main/java");
gc.setOpen(false);
gc.setServiceName("%sService");
gc.setFileOverride(true);
gc.setIdType(IdType.AUTO);
gc.setDateType(DateType.ONLY_DATE);
gc.setSwagger2(true);
mpg.setGlobalConfig(gc);
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setDbType(DbType.MYSQL);
dsc.setUsername("root");
dsc.setPassword("123456");
dsc.setUrl("jdbc:mysql://101.200.240.155:3306/edu?serverTimezone=GMT%2B8");
mpg.setDataSource(dsc);
PackageConfig pc = new PackageConfig();
pc.setParent("com.wcc");
pc.setController("controller");
pc.setService("service");
pc.setEntity("pojo");
pc.setMapper("mapper");
mpg.setPackageInfo(pc);
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setTablePrefix("edu"+"_");
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setLogicDeleteFieldName("is_deleted");
strategy.setEntityBooleanColumnRemoveIsPrefix(true);
TableFill gmtCreate = new TableFill("gmt_create", FieldFill.INSERT);
TableFill gmtModified = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE);
ArrayList<TableFill> list = new ArrayList<>();
list.add(gmtCreate);
list.add(gmtModified);
strategy.setTableFillList(list);
mpg.setStrategy(strategy);
mpg.execute();
}