SpringBoot下使用MyBatis-Puls程式碼生成器的方法
阿新 • • 發佈:2020-10-20
1.官方地址:
http://mybatis.plus/guide/generator.html#%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B
2.資料庫結構:
3.依賴匯入
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <version>5.1.39</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.30</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
配置freemarker是因為myBatis中預設的引擎是freemarker,支援自定義引擎
3.目錄結構
4.官方生成器類
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.setSwagger2(true); 實體屬性 Swagger2 註解 mpg.setGlobalConfig(gc); // 資料來源配置 /** * 設定資料庫名稱和資料庫賬戶密碼 */ DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:3306/temporary?useUnicode=true&useSSL=false&characterEncoding=utf8"); // dsc.setSchemaName("public"); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("12345"); mpg.setDataSource(dsc); // 包配置 /** * 設定生成檔案儲存地址,模組名為命令視窗輸入的模組名 */ PackageConfig pc = new PackageConfig(); pc.setModuleName(scanner("模組名")); pc.setParent("com.baomidou.ant"); 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"; /** * 這裡定義的是生成xml文件的輸出配置,存放在resource下 */ // 自定義輸出配置 // List<FileOutConfig> focList = new ArrayList<>(); // 自定義配置會被優先輸出 // focList.add(new FileOutConfig(templatePath) { // @Override // public String outputFile(TableInfo tableInfo) { // // 自定義輸出檔名 , 如果你 Entity 設定了前後綴、此處注意 xml 的名稱會跟著發生變化!! // return projectPath + "/maven1018/src/main/resources/mapper/" + pc.getModuleName() // + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; // } // }); /* cfg.setFileCreate(new IFileCreate() { @Override public boolean isCreate(ConfigBuilder configBuilder,FileType fileType,String filePath) { // 判斷自定義資料夾是否需要建立 checkDir("呼叫預設方法建立的目錄,自定義目錄用"); if (fileType == FileType.MAPPER) { // 已經生成 mapper 檔案判斷存在,不想重新生成返回 false return !new File(filePath).exists(); } // 允許生成模板檔案 return true; } }); */ // cfg.setFileOutConfigList(focList); // mpg.setCfg(cfg); // 配置模板 TemplateConfig templateConfig = new TemplateConfig(); // 配置自定義輸出模板 //指定自定義模板路徑,注意不要帶上.ftl/.vm,會根據使用的模板引擎自動識別 // templateConfig.setEntity("templates/entity2.java"); // templateConfig.setService(); // templateConfig.setController(); templateConfig.setXml(null); mpg.setTemplate(templateConfig); // 策略配置 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("你自己的父類控制器,沒有就不用設定!"); // 寫於父類中的公共欄位 // strategy.setSuperEntityColumns("id"); //註釋這行否則生成的實體類中沒有Id變數 strategy.setInclude(scanner("表名,多個英文逗號分割").split(",")); strategy.setControllerMappingHyphenStyle(true); strategy.setTablePrefix(pc.getModuleName() + "_"); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } }
5. 程式碼生成後的配置
- 預設生成的程式碼中實體類是沒有id屬性的,在程式碼生成類中註釋掉
strategy.setSuperEntityColumns("id");
- 預設生成的mapper物件上是沒有@Mapper註解,需要在主配置類中加入@MapperScan註解,進行mapper掃描
@SpringBootApplication @MapperScan("com.example.crount.mapper") public class Demo1018Application { public static void main(String[] args) { SpringApplication.run(Demo1018Application.class,args); } }
另外自己要執行程式碼進行資料庫訪問,所以application.properties中也要配置資料來源
# 資料庫配置 spring.datasource.url=jdbc:mysql:///temporary?characterEncoding=utf-8&useSSL=false spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=12345 #連線池配置 #spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
6.controller開發
注入service,修改訪問的地址,寫入訪問的方法
@RestController public class StudentController { @Autowired private IStudentService studentService; @GetMapping("/demo1") public String m1(){ Student student = studentService.getById(3); return student.getSSex(); } }
7.生成的程式碼放到主配置類的同級目錄下,執行程式碼
到此這篇關於SpringBoot下使用MyBatis-Puls程式碼生成器的方法的文章就介紹到這了,更多相關MyBatis-Puls程式碼生成器內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!