前提工作:數(shù)據(jù)庫得哆、數(shù)據(jù)庫里有表沥曹、連接數(shù)據(jù)庫的依賴髓帽、springboot + maven
【參考資料】:?MyBatis-Plus 代碼生成器
????首先奄薇,先介紹一下AutoGenerator驳阎。(下面代碼運(yùn)用到)
????AutoGenerator 是 MyBatis-Plus 的代碼生成器,通過 AutoGenerator 可以快速生成 Entity馁蒂、Mapper呵晚、Mapper XML、Service沫屡、Controller 等各個(gè)模塊的代碼饵隙,極大的提升了開發(fā)效率。
【實(shí)例快速上手】
? ? 【第一步】沮脖,先配置三個(gè)依賴金矛。
<!-- mysql -->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>mysql</groupId>
? ? ? ? ? ? <artifactId>mysql-connector-java</artifactId>
? ? ? ? ? ? <scope>runtime</scope>
? ? ? ? </dependency>
<!-- mysql -->
<!-- MP -->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.baomidou</groupId>
? ? ? ? ? ? <artifactId>mybatis-plus-boot-starter</artifactId>
? ? ? ? ? ? <version>3.3.1.tmp</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.baomidou</groupId>
? ? ? ? ? ? <artifactId>mybatis-plus-generator</artifactId>
? ? ? ? ? ? <version>3.3.1.tmp</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.velocity</groupId>
? ? ? ? ? ? <artifactId>velocity-engine-core</artifactId>
? ? ? ? ? ? <version>2.2</version>
? ? ? ? </dependency>
? ? ? ? <!-- MP -->
????【第二步】在該項(xiàng)目里創(chuàng)建 代碼生成器 類(參考:代碼生成器使用教程)
package com.anpai.demo.mybatisplus;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
// 演示例子芯急,執(zhí)行 main 方法控制臺(tái)輸入模塊表名回車自動(dòng)生成對應(yīng)項(xiàng)目目錄中
public class CodeGenerator {
? ? /**
? ? * <p>
? ? * 讀取控制臺(tái)內(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 + "!");
? ? }*/
? ? public static void main(String[] args) {
? ? ? ? // 代碼生成器
? ? ? ? AutoGenerator mpg = new AutoGenerator();
? ? ? ? // 全局配置
? ? ? ? GlobalConfig gc = new GlobalConfig();
? ? ? ? String projectPath = System.getProperty("user.dir");
? ? ? ? gc.setOutputDir("D:\\University\\Anpai\\anpai\\mybatis-plus" + "/src/main/java");
? ? ? ? gc.setFileOverride(false);
? ? ? ? gc.setServiceName("%sService");
? ? ? ? gc.setIdType(IdType.ID_WORKER_STR);
? ? ? ? gc.setDateType(DateType.ONLY_DATE);
? ? ? ? gc.setSwagger2(true);
? ? ? ? gc.setAuthor("luowenlong");
? ? ? ? gc.setOpen(false);
? ? ? ? // gc.setSwagger2(true); 實(shí)體屬性 Swagger2 注解
? ? ? ? mpg.setGlobalConfig(gc);
? ? ? ? // 數(shù)據(jù)源配置
? ? ? ? DataSourceConfig dsc = new DataSourceConfig();
? ? ? ? dsc.setUrl("jdbc:mysql://47.112.247.160:3306/anpaidb?serverTimezone=GMT%2B8");
? ? ? ? // dsc.setSchemaName("public");
? ? ? ? dsc.setDriverName("com.mysql.cj.jdbc.Driver");
? ? ? ? dsc.setUsername("root");
? ? ? ? dsc.setPassword("123456");
? ? ? ? dsc.setDbType(DbType.MYSQL);
? ? ? ? mpg.setDataSource(dsc);
? ? ? ? // 包配置
? ? ? ? PackageConfig pc = new PackageConfig();
? ? ? ? pc.setModuleName("mybatisplus");
? ? ? ? pc.setParent("com.anpai.demo");
? ? ? ? pc.setController("controller");
? ? ? ? pc.setEntity("entity");
? ? ? ? pc.setService("service");
? ? ? ? pc.setMapper("mapper");
? ? ? ? mpg.setPackageInfo(pc);
? ? ? ? // 策略配置
? ? ? ? StrategyConfig strategy = new StrategyConfig();
? ? ? ? // strategy.setInclude("edu_teacher");
? ? ? ? strategy.setNaming(NamingStrategy.underline_to_camel); // 數(shù)據(jù)庫表映射到實(shí)體的命名策略
? ? ? ? strategy.setTablePrefix(pc.getModuleName() + "_"); // 生成實(shí)體時(shí)去掉表前綴
? ? ? ? strategy.setColumnNaming(NamingStrategy.underline_to_camel); // 數(shù)據(jù)庫表字段映射到實(shí)體的命名策略
? ? ? ? strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter鏈?zhǔn)讲僮?/p>
? ? ? ? strategy.setRestControllerStyle(true); // restful api風(fēng)格控制器
? ? ? ? strategy.setControllerMappingHyphenStyle(true); // url中駝峰轉(zhuǎn)連字符
? ? ? ? mpg.setStrategy(strategy);
? ? ? ? // 執(zhí)行
? ? ? ? mpg.execute();
? ? }
}