基于Mybatis Plus实现代码生成器CodeGenerator
导入生成器需要的依赖坐标:
com.baomidou mybatis-plus-generator 3.2.0 org.apache.velocity velocity-engine-core 2.1 org.freemarker freemarker 2.3.29 com.ibeetl beetl 3.0.11.RELEASE
新建了一个普通Maven空项目:
创建我们的代码生成器程序:
importcom.baomidou.mybatisplus.core.exceptions.MybatisPlusException; importcom.baomidou.mybatisplus.generator.AutoGenerator; importcom.baomidou.mybatisplus.generator.config.DataSourceConfig; importcom.baomidou.mybatisplus.generator.config.GlobalConfig; importcom.baomidou.mybatisplus.generator.config.PackageConfig; importcom.baomidou.mybatisplus.generator.config.StrategyConfig; importcom.baomidou.mybatisplus.generator.config.rules.NamingStrategy; importorg.apache.commons.lang3.StringUtils; importjava.util.Scanner; /** *@authorDaiZhiZhou *@fileMP-Spring *@create2020-08-069:11 */ publicclassCodeGenerator{ /** **读取控制台内容 *
*/ publicstaticStringscanner(Stringtip){ Scannerscanner=newScanner(System.in); StringBuilderhelp=newStringBuilder(); help.append("请输入").append(tip).append(":"); System.out.println(help.toString()); if(scanner.hasNext()){ Stringipt=scanner.next(); if(StringUtils.isNotEmpty(ipt)){ returnipt; } } thrownewMybatisPlusException("请输入正确的"+tip+"!"); } publicstaticvoidmain(String[]args){ //代码生成器 AutoGeneratormybatisPlusCodeGenerator=newAutoGenerator(); //全局配置 GlobalConfiggc=newGlobalConfig(); //先得到当前工程目录 StringprojectPath=System.getProperty("user.dir"); //是maven项目的结构,就是工程目录+/src/main/java gc.setOutputDir(projectPath+"/src/main/java"); //gc.setOutputDir("D:\\workspace-sts\\0520adv\\02_mp_springboot/src/main/java"); //设置生成文件的作者信息 gc.setAuthor("Echo42"); //当代码生成完成之后是否打开代码所在的文件夹 gc.setOpen(true); //gc.setSwagger2(true);实体属性Swagger2注解 //gc.setServiceName("%sService"); //将上述的全局配置注入 mybatisPlusCodeGenerator.setGlobalConfig(gc); //数据源配置 DataSourceConfigdataSourceConfiguration=newDataSourceConfig(); dataSourceConfiguration.setUrl("jdbc:mysql://localhost:3306/oa?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC"); //dataSourceConfiguration.setSchemaName("public"); dataSourceConfiguration.setDriverName("com.mysql.cj.jdbc.Driver"); dataSourceConfiguration.setUsername("root"); dataSourceConfiguration.setPassword("123456"); mybatisPlusCodeGenerator.setDataSource(dataSourceConfiguration); //包配置 PackageConfigpc=newPackageConfig(); //设置父级包名 pc.setParent("cn.echo42");//controllerentityserviceservice.impl pc.setModuleName(scanner("模块名")); //pc.setModuleName("sys"); //实体类名称 pc.setEntity("domain"); //mapper包名称 pc.setMapper("mapper"); //mapper对应的映射器xml pc.setXml("mapper.xml"); //业务包层名称 pc.setService("service"); //业务接口的实现类包 pc.setServiceImpl("service.impl"); //控制器包名称 pc.setController("controller"); //装填包信息对象 mybatisPlusCodeGenerator.setPackageInfo(pc); //策略配置 StrategyConfigstrategy=newStrategyConfig(); //设置字段和表名的是否把下划线完成驼峰命名规则 strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); //设置生成的实体类继承的父类 //strategy.setSuperEntityClass("com.sxt.BaseEntity"); //是否启动lombok strategy.setEntityLombokModel(true); //是否生成resetController strategy.setRestControllerStyle(true); //公共父类 //strategy.setSuperControllerClass("com.sxt.BaseController"); //写于父类中的公共字段 //strategy.setSuperEntityColumns("person_id","person_name"); //要设置生成哪些表如果不设置就是生成所有的表 //strategy.setInclude(scanner("表名,多个英文逗号分割").split(",")); strategy.setControllerMappingHyphenStyle(true); //strategy.setTablePrefix(pc.getModuleName()+"_"); strategy.setTablePrefix("sys_"); mybatisPlusCodeGenerator.setStrategy(strategy); mybatisPlusCodeGenerator.execute(); } }
点击运行:
然后生成的这个目录结构:
基础四层就帮我们自动创建完成了:
controller报错是因为没有加入SpringMVC:
如果是SSMP的开发模式,编写结构文件使用这个能省力很多,
但是对结构不清楚的情况还是多人肉手写熟悉熟悉
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。