1、引入依赖
<!-- SpringBoot集成mybatis-plus框架 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> </dependency>
2、配置数据库,项目路径等
public class MybatisPlusCodeGenerator { public static void main(String[] args) { // final String projectPath = System.getProperty("user.dir"); final String moduleName = scanner("模块名"); final String tableName = scanner("表名,多个英文逗号分割"); AutoGenerator mpg = new AutoGenerator() .setGlobalConfig( // 全局配置 new GlobalConfig() .setOutputDir(projectPath + "/cloud-provider-8001/src/main/java") //项目路径 .setFileOverride(true) .setAuthor("junHong") .setOpen(false) .setBaseResultMap(true) .setBaseColumnList(true) ).setDataSource( // 数据源配置 new DataSourceConfig() .setUrl("jdbc:mysql://192.168.1.1:3306/db1?useUnicode=true&useSSL=false&characterEncoding=utf8") .setDriverName("com.mysql.cj.jdbc.Driver") .setUsername("自己的数据库用户名") .setPassword("摸吗")) .setPackageInfo( // 包配置 new PackageConfig() .setParent("com.casaba.provider") .setEntity("domain." + moduleName) .setMapper("mapper." + moduleName) .setService("service." + moduleName) .setServiceImpl("service." + moduleName + ".impl") ).setStrategy( // 策略配置 new StrategyConfig() .setNaming(underline_to_camel) .setColumnNaming(underline_to_camel) .setSuperEntityClass("com.casaba.provider.domain.BaseDomain") .setSuperEntityColumns(new String[]{"create_at", "update_at"}) .setEntityLombokModel(true) .setEntityBooleanColumnRemoveIsPrefix(true) .setEntityTableFieldAnnotationEnable(true) .setInclude(StringUtils.split(tableName, ",")) .setTablePrefix("t") ).setCfg( // 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值 new InjectionConfig() { @Override public void initMap() { } }.setFileOutConfigList(Collections.singletonList(new FileOutConfig("/templates/mapper.xml.vm") { // 自定义输出文件目录 @Override public String outputFile(TableInfo tableInfo) { return projectPath + "/cloud-provider-8001/src/main/resources/mapper/" + moduleName + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; } })) ).setTemplate(new TemplateConfig() .setController(null) .setXml(null)); mpg.execute(); } /** * <p> * 读取控制台内容 * </p> */ public static String scanner(final String tip) { @SuppressWarnings("resource") 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.isNotEmpty(ipt)) { if (StringUtils.equals(ipt, "?")) { return ""; } return ipt; } } return ""; } }
标签:逆向,实体类,mybatisPlus,String,moduleName,new,true,public,scanner From: https://www.cnblogs.com/junnnnnnnn/p/16850776.html