CodeGenerator
public class CodeGenerator { /** * <p> * 读取控制台内容 * </p> */ public static String scanner(String tip) { Scanner scanner = new Scanner(System.in); StringBuilder help = new StringBuilder(); help.append("请输入" + tip + ":"); System.out.println(help.toString()); 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"); /** * 这里需要设定一下保存的地址是本项目下的/src/main/java */ gc.setOutputDir(projectPath + "/maven1018/src/main/java"); gc.setAuthor("XYD"); gc.setOpen(false); gc.setFileOverride(true); //重新生成时文件是否覆盖 // gc.setSwagger2(true); 实体属性 Swagger2 注解 gc.setServiceName("%sService"); //去掉Service接口的首字母I gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型 mpg.setGlobalConfig(gc); // 数据源配置 /** * 设置数据库名称和数据库账户密码 */ DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:3306/temporary?useUnicode=true&useSSL=false&characterEncoding=utf8"); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("12345"); dsc.setDbType(DbType.MYSQL); mpg.setDataSource(dsc); // 包配置 /** * 设置生成文件保存地址,模块名为命令窗口输入的模块名 */ PackageConfig pc = new PackageConfig(); pc.setModuleName(scanner("模块名")); pc.setParent("com.baomidou.ant"); mpg.setPackageInfo(pc); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); // strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!"); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); // 公共父类 // strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!"); // 写于父类中的公共字段 //注释这行否则生成的实体类中没有Id变量 // strategy.setSuperEntityColumns("id"); strategy.setInclude(scanner("表名,多个英文逗号分割").split(",")); strategy.setControllerMappingHyphenStyle(true); strategy.setTablePrefix(pc.getModuleName() + "_"); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } }因为生成的Mapper接口默认是没有@Mapper注解的,所以需要在主配置类中进行扫描注入
@SpringBootApplication @MapperScan("com.example.crount.mapper") //注入Mapper接口 public class Demo1018Application { public static void main(String[] args) { SpringApplication.run(Demo1018Application.class, args); } }