本文章僅供小編學(xué)習(xí)使用朝蜘,如有侵犯他人版權(quán)兔沃,請聯(lián)系小編撤回或刪除
pom.xml
<!-- 模版引擎 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.1.2</version>
</dependency>
代碼生成類
/**
* <p>
* 代碼生成器,使用方法,直接運行萌衬;根據(jù)控制臺提示輸入模塊名(demo),然后輸入表名即可
* </p>
*
* @author liuzongqiang
* @since 2019-07-19
*/
public class CodeGenerator {
public static final String OUTPUTDIR = "/src/main/java";
public static final String AUTHOR = "liuzongqiang";
public static final String DBURL = "jdbc:mysql://39.98.172.65:9001/fine_admin?characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Taipei";
public static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
public static final String DBUSER = "xxxxxx";
public static final String DBPASSWORD = "xxxxxx";
public static void main(String[] args) {
// 代碼生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + OUTPUTDIR);
gc.setAuthor(AUTHOR);
gc.setOpen(false);
//是否覆蓋文件
gc.setFileOverride(false);
//自定義文件名
gc.setMapperName("%sMapper");
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");
mpg.setGlobalConfig(gc);
// 數(shù)據(jù)源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl(DBURL);
dsc.setDriverName(DBDRIVER);
dsc.setUsername(DBUSER);
dsc.setPassword(DBPASSWORD);
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName(scanner("模塊名"));
pc.setParent("com.example");
pc.setEntity("entity");
pc.setXml("mapper");
pc.setService("service");
mpg.setPackageInfo(pc);
// 自定義配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
// String templatePath = "/templates/mapper.xml.vm";
// 自定義輸出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定義配置會被優(yōu)先輸出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定義輸出文件名 姜盈, 如果你 Entity 設(shè)置了前后綴径缅、此處注意 xml 的名稱會跟著發(fā)生變化V蟹姜胖!
return projectPath + "/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.setSuperEntityColumns("id");
strategy.setInclude(scanner("表名"));
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix(pc.getModuleName() + "_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
/**
* <p>
* 讀取控制臺內(nèi)容
* </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.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("請輸入正確的" + tip + "!");
}
}
測試
H:\Java\jdk1.8.0_131\bin\java.exe "-javaagent:D:\develop\development tool\JetBrains\IntelliJ IDEA 2018.2\lib\idea_rt.jar=55117:D:\develop\development tool\JetBrains\IntelliJ IDEA 2018.2\bin" -Dfile.encoding=UTF-8 -classpath H:\Java\jdk1.8.0_131\jre\lib\charsets.jar;H:\Java\jdk1.8.0_131\jre\lib\deploy.jar;H:\Java\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar;H:\Java\jdk1.8.0_131\jre\lib\ext\cldrdata.jar;H:\Java\jdk1.8.0_131\jre\lib\ext\dnsns.jar;H:\Java\jdk1.8.0_131\jre\lib\ext\jaccess.jar;H:\Java\jdk1.8.0_131\jre\lib\ext\jfxrt.jar;H:\Java\jdk1.8.0_131\jre\lib\ext\localedata.jar;H:\Java\jdk1.8.0_131\jre\lib\ext\nashorn.jar;H:\Java\jdk1.8.0_131\jre\lib\ext\sunec.jar;H:\Java\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar;H:\Java\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar;H:\Java\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar;H:\Java\jdk1.8.0_131\jre\lib\ext\zipfs.jar;H:\Java\jdk1.8.0_131\jre\lib\javaws.jar;H:\Java\jdk1.8.0_131\jre\lib\jce.jar;H:\Java\jdk1.8.0_131\jre\lib\jfr.jar;H:\Java\jdk1.8.0_131\jre\lib\jfxswt.jar;H:\Java\jdk1.8.0_131\jre\lib\jsse.jar;H:\Java\jdk1.8.0_131\jre\lib\management-agent.jar;H:\Java\jdk1.8.0_131\jre\lib\plugin.jar;H:\Java\jdk1.8.0_131\jre\lib\resources.jar;H:\Java\jdk1.8.0_131\jre\lib\rt.jar;R:\SpringBoot集成MybatisPlus\mybatis-plus-demo\target\classes;F:\.m2\repository\org\springframework\boot\spring-boot-starter-web\2.1.6.RELEASE\spring-boot-starter-web-2.1.6.RELEASE.jar;F:\.m2\repository\org\springframework\boot\spring-boot-starter\2.1.6.RELEASE\spring-boot-starter-2.1.6.RELEASE.jar;F:\.m2\repository\org\springframework\boot\spring-boot\2.1.6.RELEASE\spring-boot-2.1.6.RELEASE.jar;F:\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.1.6.RELEASE\spring-boot-starter-logging-2.1.6.RELEASE.jar;F:\.m2\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;F:\.m2\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;F:\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.11.2\log4j-to-slf4j-2.11.2.jar;F:\.m2\repository\org\apache\logging\log4j\log4j-api\2.11.2\log4j-api-2.11.2.jar;F:\.m2\repository\org\slf4j\jul-to-slf4j\1.7.26\jul-to-slf4j-1.7.26.jar;F:\.m2\repository\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;F:\.m2\repository\org\yaml\snakeyaml\1.23\snakeyaml-1.23.jar;F:\.m2\repository\org\springframework\boot\spring-boot-starter-json\2.1.6.RELEASE\spring-boot-starter-json-2.1.6.RELEASE.jar;F:\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.9.9\jackson-databind-2.9.9.jar;F:\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;F:\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.9.9\jackson-core-2.9.9.jar;F:\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.9\jackson-datatype-jdk8-2.9.9.jar;F:\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.9\jackson-datatype-jsr310-2.9.9.jar;F:\.m2\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.9\jackson-module-parameter-names-2.9.9.jar;F:\.m2\repository\org\springframework\boot\spring-boot-starter-tomcat\2.1.6.RELEASE\spring-boot-starter-tomcat-2.1.6.RELEASE.jar;F:\.m2\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.21\tomcat-embed-core-9.0.21.jar;F:\.m2\repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.21\tomcat-embed-el-9.0.21.jar;F:\.m2\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.21\tomcat-embed-websocket-9.0.21.jar;F:\.m2\repository\org\hibernate\validator\hibernate-validator\6.0.17.Final\hibernate-validator-6.0.17.Final.jar;F:\.m2\repository\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;F:\.m2\repository\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;F:\.m2\repository\com\fasterxml\classmate\1.4.0\classmate-1.4.0.jar;F:\.m2\repository\org\springframework\spring-web\5.1.8.RELEASE\spring-web-5.1.8.RELEASE.jar;F:\.m2\repository\org\springframework\spring-beans\5.1.8.RELEASE\spring-beans-5.1.8.RELEASE.jar;F:\.m2\repository\org\springframework\spring-webmvc\5.1.8.RELEASE\spring-webmvc-5.1.8.RELEASE.jar;F:\.m2\repository\org\springframework\spring-aop\5.1.8.RELEASE\spring-aop-5.1.8.RELEASE.jar;F:\.m2\repository\org\springframework\spring-context\5.1.8.RELEASE\spring-context-5.1.8.RELEASE.jar;F:\.m2\repository\org\springframework\spring-expression\5.1.8.RELEASE\spring-expression-5.1.8.RELEASE.jar;F:\.m2\repository\mysql\mysql-connector-java\8.0.16\mysql-connector-java-8.0.16.jar;F:\.m2\repository\org\projectlombok\lombok\1.18.8\lombok-1.18.8.jar;F:\.m2\repository\org\slf4j\slf4j-api\1.7.26\slf4j-api-1.7.26.jar;F:\.m2\repository\org\springframework\spring-core\5.1.8.RELEASE\spring-core-5.1.8.RELEASE.jar;F:\.m2\repository\org\springframework\spring-jcl\5.1.8.RELEASE\spring-jcl-5.1.8.RELEASE.jar;F:\.m2\repository\org\springframework\boot\spring-boot-starter-freemarker\2.1.6.RELEASE\spring-boot-starter-freemarker-2.1.6.RELEASE.jar;F:\.m2\repository\org\freemarker\freemarker\2.3.28\freemarker-2.3.28.jar;F:\.m2\repository\org\springframework\spring-context-support\5.1.8.RELEASE\spring-context-support-5.1.8.RELEASE.jar;F:\.m2\repository\com\baomidou\mybatis-plus-boot-starter\3.1.2\mybatis-plus-boot-starter-3.1.2.jar;F:\.m2\repository\com\baomidou\mybatis-plus\3.1.2\mybatis-plus-3.1.2.jar;F:\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.1.6.RELEASE\spring-boot-autoconfigure-2.1.6.RELEASE.jar;F:\.m2\repository\org\springframework\boot\spring-boot-starter-jdbc\2.1.6.RELEASE\spring-boot-starter-jdbc-2.1.6.RELEASE.jar;F:\.m2\repository\com\zaxxer\HikariCP\3.2.0\HikariCP-3.2.0.jar;F:\.m2\repository\org\springframework\spring-jdbc\5.1.8.RELEASE\spring-jdbc-5.1.8.RELEASE.jar;F:\.m2\repository\org\springframework\spring-tx\5.1.8.RELEASE\spring-tx-5.1.8.RELEASE.jar;F:\.m2\repository\com\baomidou\mybatis-plus-generator\3.1.2\mybatis-plus-generator-3.1.2.jar;F:\.m2\repository\com\baomidou\mybatis-plus-extension\3.1.2\mybatis-plus-extension-3.1.2.jar;F:\.m2\repository\com\baomidou\mybatis-plus-core\3.1.2\mybatis-plus-core-3.1.2.jar;F:\.m2\repository\com\baomidou\mybatis-plus-annotation\3.1.2\mybatis-plus-annotation-3.1.2.jar;F:\.m2\repository\com\github\jsqlparser\jsqlparser\1.2\jsqlparser-1.2.jar;F:\.m2\repository\org\mybatis\mybatis\3.5.1\mybatis-3.5.1.jar;F:\.m2\repository\org\mybatis\mybatis-spring\2.0.1\mybatis-spring-2.0.1.jar;F:\.m2\repository\com\alibaba\fastjson\1.2.47\fastjson-1.2.47.jar com.example.demo.generator.CodeGenerator
請輸入模塊名:
demo
請輸入表名:
fine_org
16:25:48.795 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================準備生成文件...==========================
16:25:49.516 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/mapper.xml.ftl; 文件:R:\SpringBoot集成MybatisPlus\mybatis-plus-demo/src/main/resources/mapper/demo/FineOrgMapper.xml
16:25:49.605 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/entity.java.ftl; 文件:R:\SpringBoot集成MybatisPlus\mybatis-plus-demo/src/main/java\com\example\demo\entity\FineOrg.java
16:25:49.610 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/mapper.java.ftl; 文件:R:\SpringBoot集成MybatisPlus\mybatis-plus-demo/src/main/java\com\example\demo\mapper\FineOrgMapper.java
16:25:49.612 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/service.java.ftl; 文件:R:\SpringBoot集成MybatisPlus\mybatis-plus-demo/src/main/java\com\example\demo\service\IFineOrgService.java
16:25:49.615 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/serviceImpl.java.ftl; 文件:R:\SpringBoot集成MybatisPlus\mybatis-plus-demo/src/main/java\com\example\demo\service\impl\FineOrgServiceImpl.java
16:25:49.618 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/controller.java.ftl; 文件:R:\SpringBoot集成MybatisPlus\mybatis-plus-demo/src/main/java\com\example\demo\controller\FineOrgController.java
16:25:49.618 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================文件生成完成5砩ⅰS依场蚜锨!==========================
Process finished with exit code 0