目录
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
执行sql
CREATE TABLE if not exists `tbl_product_category` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`category_pic_code` varchar(100) NOT NULL comment '图片地址id',
`name` varchar(255) NOT NULL comment '产品类别名字',
`status` bit(1) NOT NULL default 1 comment '0 无效 1有效',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 comment='产品类别信息';
执行代码
public static void main(String[] args) {
String projectPath = System.getProperty("user.dir");
String outputDir = projectPath + "/src/main/java"; //生成文件输出目录
String author = "duanlsh"; //注释作者
String entity = "ProductCategory"; //生成文件实体名称
String entityPrefix = "tbl_"; //前缀
String[] tableName = "tbl_product_category".split(","); //表名多个以 , 分割
String packageName = "com.apple"; //生成文件的 包名
//数据库
String dbUrl = "jdbc:mysql://localhost:3306/grainfull?characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC";
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDbType(DbType.MYSQL)
.setUrl(dbUrl)
.setUsername("root")
.setPassword("root")
.setDriverName("com.mysql.cj.jdbc.Driver");
GlobalConfig config = new GlobalConfig();
config.setAuthor(author);
config.setOutputDir(outputDir);
config.setFileOverride(true);
config.setOpen(false); //文件生成完是否打开目录
config.setEntityName("%sModel");//实体类添加Model
config.setServiceName("%sService");
config.setControllerName("%sController");
config.setServiceImplName("%sServiceImpl");
config.setMapperName("%sMapper");
config.setXmlName("%sMapper");
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig
.setCapitalMode(true)
.setEntityLombokModel(true) //设置是否使用 lombook
// .setDbColumnUnderline(true)
.setNaming(NamingStrategy.underline_to_camel)
.setColumnNaming(NamingStrategy.underline_to_camel)
.setRestControllerStyle(true)
.setControllerMappingHyphenStyle(true)
// .setSuperEntityClass("com.baomidou.mybatisplus.extension.service.impl.ServiceImpl")
// .setSuperMapperClass("com.baomidou.mybatisplus.core.mapper.BaseMapper")
.setInclude(tableName)//修改替换成你需要的表名,多个表名传数组
.setTablePrefix(entityPrefix)
;
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
//do nothing
}
};
PackageConfig packageConfig = new PackageConfig();
// packageConfig.setModuleName("ProductCategory"); //在parent包下面添加的包名 eg: com.apple.ProductCategory
packageConfig.setParent(packageName);
packageConfig.setController("controller");
packageConfig.setService("service");
packageConfig.setServiceImpl("service.impl");
packageConfig.setEntity("model");
// TemplateConfig templateConfig = new TemplateConfig();
// templateConfig.setEntity("/templates/entity2.java");
// templateConfig.setXml(null);
AutoGenerator mpg = new AutoGenerator();
// mpg.setTemplate(templateConfig);
mpg.setGlobalConfig(config);
mpg.setDataSource(dataSourceConfig);
mpg.setStrategy(strategyConfig);
mpg.setCfg(cfg);
mpg.setPackageInfo(packageConfig);
mpg.execute();
}
参考链接