SpringBoot項目使用mybatis-plus逆向自動生成全套代碼
1.在你的SpringBoot項目下新建子模塊項目
pom.xml添加以下依賴:
<properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.3.2</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-extension</artifactId> <version>3.3.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> <version>2.3.1.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
ps:名稱隨意,最好帶上generator 來辨別這是代碼自動生成模塊
2.在此模塊下新建一個包與一個java類 類名: CodeGenerator
完整代碼如下:
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException; import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.baomidou.mybatisplus.core.toolkit.StringUtils; 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.TableInfo; import com.baomidou.mybatisplus.generator.config.rules.DateType; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * @Description: 代碼生成類 */ public class CodeGenerator { //數(shù)據(jù)庫連接參數(shù) public static String driver = "com.mysql.cj.jdbc.Driver"; public static String url = "jdbc:mysql://localhost:3306/rht_test?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true"; public static String username="root"; public static String password="123456"; //父級別包名稱 public static String parentPackage = "cn.rht"; //代碼生成的目標(biāo)路徑 public static String generateTo = "/rht-generator/src/main/java"; //mapper.xml的生成路徑 public static String mapperXmlPath = "/rht-generator/src/main/resources/mapper"; //控制器的公共基類,用于抽象控制器的公共方法,null值表示沒有父類 public static String baseControllerClassName ; //業(yè)務(wù)層的公共基類,用于抽象公共方法 public static String baseServiceClassName ; //作者名 public static String author = "rht.cn"; //模塊名稱,用于組成包名 public static String modelName = "portal"; //Mapper接口的模板文件,不用寫后綴 .ftl public static String mapperTempalte = "/ftl/mapper.java"; /** * <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 + "!"); } /** * RUN THIS */ public static void main(String[] args) { // 代碼生成器 AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + generateTo); gc.setAuthor(author); gc.setOpen(false); //設(shè)置時間類型為Date gc.setDateType(DateType.TIME_PACK); //開啟swagger //gc.setSwagger2(true); //設(shè)置mapper.xml的resultMap gc.setBaseResultMap(true); mpg.setGlobalConfig(gc); // 數(shù)據(jù)源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl(url); // dsc.setSchemaName("public"); dsc.setDriverName(driver); dsc.setUsername(username); dsc.setPassword(password); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); pc.setEntity("model"); //pc.setModuleName(scanner("模塊名")); pc.setModuleName(modelName); pc.setParent(parentPackage); mpg.setPackageInfo(pc); // 自定義配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; List<FileOutConfig> focList = new ArrayList<>(); focList.add(new FileOutConfig("/templates/mapper.xml.ftl") { @Override public String outputFile(TableInfo tableInfo) { // 自定義輸入文件名稱 return projectPath + mapperXmlPath + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); mpg.setTemplate(new TemplateConfig().setXml(null)); mpg.setTemplate(new TemplateConfig().setMapper(mapperTempalte)); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); //字段駝峰命名 strategy.setColumnNaming(NamingStrategy.underline_to_camel); //設(shè)置實體類的lombok strategy.setEntityLombokModel(true); //設(shè)置controller的父類 if (baseControllerClassName!=null) strategy.setSuperControllerClass(baseControllerClassName); //設(shè)置服務(wù)類的父類 if (baseServiceClassName !=null ) strategy.setSuperServiceImplClass(baseServiceClassName); // strategy. //設(shè)置實體類屬性對應(yīng)表字段的注解 strategy.setEntityTableFieldAnnotationEnable(true); //設(shè)置表名 String tableName = scanner("表名, all全部表"); if(! "all".equalsIgnoreCase(tableName)){ strategy.setInclude(tableName); } strategy.setTablePrefix(pc.getModuleName() + "_"); strategy.setRestControllerStyle(true); mpg.setStrategy(strategy); // 選擇 freemarker 引擎需要指定如下加,注意 pom 依賴必須有! mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } }
3.在 resources 下新建 文件夾,用來存放 mapper文件
新建模板文件: mapper.java.ftl
模板完整代碼如下:
package ${package.Mapper}; import ${package.Entity}.${entity}; import ${superMapperClassPackage}; import org.springframework.stereotype.Repository; /** * <p> * ${table.comment!} Mapper 接口 * </p> * * @author ${author} * @since ${date} */ <#if kotlin> interface ${table.mapperName} : ${superMapperClass}<${entity}> <#else> @Repository public interface ${table.mapperName} extends ${superMapperClass}<${entity}> { } </#if>
4.配置CodeGenerator類
ps:請根據(jù)自己實際路徑配置
5.啟動代碼生成類main方法
ps:輸入all 將會自動生成配置數(shù)據(jù)庫下的所有配置文件,或者直接輸入單表名稱生成某一個表的Controller,mapper,service,model層與mapper.xml文件
下面是我輸入表名為:user,自動生成的部分文件信息展示
User實體類
UserMapper.xml文件
如果你有很多表要生成時,但又不想全部生成時,可以在CodeGenerator類代碼中134行代碼
//設(shè)置表名 String tableName = scanner("表名, all全部表"); if(! "all".equalsIgnoreCase(tableName)){ strategy.setInclude(tableName); }
替換為:
String[] tableNames = {"user","dept"};//數(shù)據(jù)庫表名的集合 for (int i = 0; i <tableNames.length ; i++) { strategy.setInclude(tableNames); }
來生成自己想要生成的文件
6.刪除文件
最后:也是重要的一點,在您將這些文件復(fù)制到了項目模塊上的時候,留下CodeGenerator類與文件夾下的mapper.java.ftl配置,其他生成的請及時刪除
至于原因是將來業(yè)務(wù)拓展后,數(shù)據(jù)庫新增表后,只要新創(chuàng)建表的文件,如果不刪除以前生成過的文件,到時候找起來比較麻煩,沒必要給自己添這層麻煩
到此這篇關(guān)于SpringBoot項目使用mybatis-plus逆向自動生成全套代碼的文章就介紹到這了,更多相關(guān)mybatis-plus逆向自動生成代碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot如何使用MyBatisPlus逆向工程自動生成代碼
- SpringBoot集成Mybatis-plus并實現(xiàn)自動生成相關(guān)文件的示例代碼
- Springboot Mybatis Plus自動生成工具類詳解代碼
- SpringBoot整合Mybatis Generator自動生成代碼
- SpringBoot根據(jù)目錄結(jié)構(gòu)自動生成路由前綴的實現(xiàn)代碼
- springboot整合freemarker代碼自動生成器
- SpringBoot整合screw實現(xiàn)數(shù)據(jù)庫文檔自動生成的示例代碼
- springboot 通過代碼自動生成pid的方法
- SpringBoot+MyBatis-Plus+Velocity實現(xiàn)代碼自動生成
相關(guān)文章
教你用java實現(xiàn)學(xué)生成績管理系統(tǒng)(附詳細(xì)代碼)
教學(xué)管理系統(tǒng)很適合初學(xué)者對于所學(xué)語言的練習(xí),下面這篇文章主要給大家介紹了關(guān)于如何用java實現(xiàn)學(xué)生成績管理系統(tǒng)的相關(guān)資料,文中給出了詳細(xì)的實例代碼,需要的朋友可以參考下2023-06-06Spring @Bean注解的使用場景與案例實現(xiàn)
隨著SpringBoot的流行,我們現(xiàn)在更多采用基于注解式的配置從而替換掉了基于XML的配置,所以本篇文章我們主要探討基于注解的@Bean以及和其他注解的使用2023-03-03使用Post方式提交數(shù)據(jù)到Tomcat服務(wù)器的方法
這篇將介紹使用Post方式提交數(shù)據(jù)到服務(wù)器,由于Post的方式和Get方式創(chuàng)建Web工程是一模一樣的,只用幾個地方的代碼不同,這篇文章主要介紹了使用Post方式提交數(shù)據(jù)到Tomcat服務(wù)器的方法,感興趣的朋友一起學(xué)習(xí)吧2016-04-04Java Mybatis中的 ${ } 和 #{ }的區(qū)別使用詳解
這篇文章主要介紹了Mybatis中的 ${ } 和 #{ }的區(qū)別使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07JAVA中出現(xiàn)異常、拋出異常后續(xù)代碼是否執(zhí)行情況詳析
當(dāng)產(chǎn)生異常后,并在異常處理器中進(jìn)行執(zhí)行之后,程序會是如何的一種狀態(tài),是終止還是繼續(xù)執(zhí)行處理之后的代碼呢,下面這篇文章主要給大家介紹了關(guān)于JAVA中出現(xiàn)異常、拋出異常后續(xù)代碼是否執(zhí)行情況的相關(guān)資料,需要的朋友可以參考下2024-05-05