mybatis|mybatis plus generator 根据数据库自动生成实体类的实现示例

目录

  • 1、添加依赖
  • 2、编写代码生成器
  • 3、运行主程序,输入表名

1、添加依赖
com.baomidoumybatis-plus-boot-starter3.3.2com.baomidoumybatis-plus-generator3.3.2org.freemarkerfreemarker2.3.30

【mybatis|mybatis plus generator 根据数据库自动生成实体类的实现示例】
2、编写代码生成器
public class CodeGenerator {/*** * 读取控制台内容*
*/public static String scanner(String tip) {Scanner scanner = new Scanner(System.in); System.out.println("请输入" + tip + ":"); if (scanner.hasNext()) {String ipt = scanner.next(); if (StringUtils.isNotBlank(ipt)) {return ipt; }}throw new MybatisPlusException("请输入正确的" + tip + "!"); }public static void main(String[] args) {// 代码生成器AutoGenerator mpg = new AutoGenerator(); // 全局配置GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + "/check-service/src/main/java"); gc.setAuthor("raoyujie"); gc.setOpen(false); // gc.setSwagger2(true); 实体属性 Swagger2 注解mpg.setGlobalConfig(gc); // 数据源配置DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8"); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("root"); mpg.setDataSource(dsc); // 包配置PackageConfig pc = new PackageConfig(); pc.setParent("com.hrbust.health.check.service"); mpg.setPackageInfo(pc); // 自定义配置InjectionConfig cfg = new InjectionConfig() {@Overridepublic void initMap() {// to do nothing}}; // 如果模板引擎是 freemarkerString templatePath = "/templates/mapper.xml.ftl"; // 自定义输出配置List focList = new ArrayList<>(); // 自定义配置会被优先输出focList.add(new FileOutConfig(templatePath) {@Overridepublic String outputFile(TableInfo tableInfo) {// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!return projectPath + "/check-service/src/main/resources/mapper/" + pc.getModuleName()+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; }}); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); // 配置模板TemplateConfig templateConfig = new TemplateConfig(); templateConfig.setXml(null); mpg.setTemplate(templateConfig); // 策略配置StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); strategy.setInclude(scanner("表名,多个英文逗号分割").split(",")); strategy.setControllerMappingHyphenStyle(true); strategy.setTablePrefix(pc.getModuleName() + "_"); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); }}


3、运行主程序,输入表名 mybatis|mybatis plus generator 根据数据库自动生成实体类的实现示例
文章图片

到此这篇关于mybatis plus generator 根据数据库自动生成实体类的实现示例的文章就介绍到这了,更多相关mybatis plus generator自动生成实体类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读