简介:
基于mybatis-plus代码生成工具
需要引入的依赖:
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
</dependencies>
java代码工具类如下:
public static void main(String[] args) {
AutoGenerator autoGenerator = new AutoGenerator();
autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());
//全局配置
GlobalConfig gc = new GlobalConfig();
final String projectPath = System.getProperty("user.dir");//得到当前项目的路径
gc.setOutputDir(projectPath + "/src/main/java"); //生成文件输出根目录
gc.setOpen(false);//生成完成后不弹出文件框
gc.setFileOverride(true); //文件覆盖
gc.setIdType(IdType.AUTO); // 主键策略
gc.setActiveRecord(false);// 不需要ActiveRecord特性的请改为false
gc.setEnableCache(false);// XML 二级缓存
gc.setBaseResultMap(false);// XML ResultMap
gc.setBaseColumnList(false);// XML columList
gc.setAuthor("xd");// 作者
// 自定义文件命名,注意 %s 会自动填充表实体属性!
gc.setControllerName("%sController");
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");
gc.setMapperName("%sMapper");
gc.setXmlName(null);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mappers/"
+ "/" + tableInfo.getEntityName() + "Mapper.xml";
}
});
cfg.setFileOutConfigList(focList);
autoGenerator.setCfg(cfg);
autoGenerator.setGlobalConfig(gc);
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
autoGenerator.setTemplate(templateConfig);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.MYSQL); //设置数据库类型,我是postgresql
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
dsc.setUrl("jdbc:mysql://192.168.0.103:3306/service_activity?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8&useSSL=true&tinyInt1isBit=false"); //指定数据库
autoGenerator.setDataSource(dsc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel); // 表名生成策略
strategy.setInclude(new String[] { "tb_social_activity"}); // 需要生成的表
autoGenerator.setStrategy(strategy);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.crazy.example");
pc.setController("controller.rest");
pc.setService("service");
pc.setServiceImpl("service.impl");
pc.setMapper("dao");
pc.setEntity("entity");
autoGenerator.setPackageInfo(pc);
// 执行生成
autoGenerator.execute();
}
号外:
😜创作一篇文章需要投入大量的心血和努力,每一篇原创作品都是作者独特思想和情感的结晶。如果您觉得我的文章对您有所启发或帮助,那么请您支持我,给我一个点赞和关注,这是对我最大的鼓励。同时,如果您有任何问题或建议,也欢迎您随时与我交流,让我们一起共同进步。