springboot整合mybatis-plus代碼生成器的配置解析
更新時間:2021年02月26日 09:27:14 作者:heromps
這篇文章主要介紹了springboot整合mybatis-plus代碼生成器的配置解析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
AutoGenerator 是 MyBatis-Plus 的代碼生成器,通過 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各個模塊的代碼,極大的提升了開發(fā)效率。
具體實(shí)實(shí)現(xiàn)以及配置解析如下:
package mybatis_plus; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException; import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.InjectionConfig; import com.baomidou.mybatisplus.generator.config.*; import com.baomidou.mybatisplus.generator.config.po.TableFill; import com.baomidou.mybatisplus.generator.config.po.TableInfo; import com.baomidou.mybatisplus.generator.config.rules.DateType; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import org.apache.commons.lang3.StringUtils; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class CodeGenerator { 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) { // 構(gòu)建代碼生成器 AutoGenerator mpg = new AutoGenerator(); //配置策略 //1.全局配置 GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + "/src/main/java"); gc.setAuthor("heroMps"); gc.setOpen(false); gc.setFileOverride(false); //是否覆蓋 gc.setServiceName("%sService");//去除Service前面的I gc.setIdType(IdType.ID_WORKER); gc.setDateType(DateType.ONLY_DATE); // gc.setSwagger2(true); //實(shí)體屬性 Swagger2 注解 mpg.setGlobalConfig(gc); //2.設(shè)置數(shù)據(jù)源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://127.0.0.1:3306/mybatis_plus?useUnicode=true&useSSL=false&characterEncoding=utf8"); // dsc.setSchemaName("public"); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("root"); dsc.setDbType(DbType.MYSQL); mpg.setDataSource(dsc); //3.包配置 PackageConfig pc = new PackageConfig(); // pc.setModuleName("blog"); pc.setParent("mybatis_plus"); pc.setEntity("entity"); pc.setMapper("mapper"); pc.setService("service"); pc.setController("controller"); 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ā)生變化??! return projectPath + "/src/main/resources/mapper/" + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; } }); /* cfg.setFileCreate(new IFileCreate() { @Override public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) { // 判斷自定義文件夾是否需要創(chuàng)建 checkDir("調(diào)用默認(rèn)方法創(chuàng)建的目錄,自定義目錄用"); if (fileType == FileType.MAPPER) { // 已經(jīng)生成 mapper 文件判斷存在,不想重新生成返回 false return !new File(filePath).exists(); } // 允許生成模板文件 return true; } }); */ cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); // 配置模板 TemplateConfig templateConfig = new TemplateConfig(); // 配置自定義輸出模板 //指定自定義模板路徑,注意不要帶上.ftl/.vm, 會根據(jù)使用的模板引擎自動識別 // templateConfig.setEntity("templates/entity2.java"); // templateConfig.setService(); // templateConfig.setController(); templateConfig.setXml(null); mpg.setTemplate(templateConfig); //4.策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setInclude(scanner("表名,多個英文逗號分割").split(",")); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true); strategy.setLogicDeleteFieldName("deleted"); //自動填充設(shè)置 TableFill create_time = new TableFill("create_time", FieldFill.INSERT); TableFill update_time = new TableFill("update_time", FieldFill.INSERT_UPDATE); ArrayList<TableFill> list = new ArrayList<>(); list.add(create_time); list.add(update_time); strategy.setTableFillList(list); //5.樂觀鎖配置 strategy.setVersionFieldName("version"); strategy.setRestControllerStyle(true); strategy.setControllerMappingHyphenStyle(true); mpg.setStrategy(strategy); mpg.execute(); } }
生成目錄如下:
測試查詢
到此這篇關(guān)于springboot整合mybatis-plus代碼生成器的文章就介紹到這了,更多相關(guān)springboot整合mybatis-plus內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaWeb實(shí)戰(zhàn)之編寫單元測試類測試數(shù)據(jù)庫操作
這篇文章主要介紹了JavaWeb實(shí)戰(zhàn)之編寫單元測試類測試數(shù)據(jù)庫操作,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)javaweb的小伙伴們有很大的幫助,需要的朋友可以參考下2021-04-04