教你怎么用SpringBoot+Mybati-Plus快速搭建代碼
前言
先放一個官網(wǎng)吧,其實本案例就是根據(jù)官網(wǎng)案例來的,只是進行了修改配置。
一、搭建一個springboot項目
這一步應(yīng)該不用多說了,使用idea可以非常方便的就搭建出來了。
二、配置依賴
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.xxx</groupId> <artifactId>mybatis-plus</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mybatis-plus</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!--web依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.baomidou/mybatisplus-spring-boot-starter --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.31</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
三、配置application.yml文件
#我使用的是mysql8.0,所以驅(qū)動書寫有點區(qū)別。 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/school?useUnicode=true&useSSL=false&characterEncoding=utf8 username: root password: 自己數(shù)據(jù)庫密碼
四、啟動類
根據(jù)官網(wǎng),我們需要寫一個啟動類來生成這些文件
package com.xxx.mybatisplus.generator; 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.NamingStrategy; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class CodeGenerator { /** * <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.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"); gc.setOutputDir(projectPath + "/src/main/java"); //作者 gc.setAuthor("程序逸"); //打開輸出目錄 gc.setOpen(false); // gc.setSwagger2(true); 實體屬性 Swagger2 注解 mpg.setGlobalConfig(gc); // 數(shù)據(jù)源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:3306/school?useUnicode=true&useSSL=false&characterEncoding=utf8"); // dsc.setSchemaName("public"); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("自己密碼"); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); // pc.setModuleName(scanner("模塊名")); pc.setParent("com.xxx.mybatisplus").setEntity("pojo").setMapper("mapper").setService("service").setServiceImpl("serviceimpl") .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/" + pc.getModuleName() + "/" + 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)用默認方法創(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); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); // 寫于父類中的公共字段 strategy.setInclude(scanner("表名,多個英文逗號分割").split(",")); strategy.setControllerMappingHyphenStyle(true); strategy.setTablePrefix("t_"); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } }
五、運行main方法
運行該main方法,然后在控制臺輸入你想生成的表,用,隔開。
六、項目結(jié)構(gòu)
到此這篇關(guān)于教你怎么用SpringBoot+Mybati-Plus快速搭建代碼的文章就介紹到這了,更多相關(guān)SpringBoot+Mybati-Plus搭建代碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Mybatis-Plus的應(yīng)用場景描述及注入SQL原理分析
- SpringBoot+MyBatisPlus+Vue 前后端分離項目快速搭建過程(前端篇)
- SpringBoot+Shiro+Redis+Mybatis-plus 實戰(zhàn)項目及問題小結(jié)
- SpringBoot基于Mybatis-Plus自動代碼生成
- 使用springboot整合mybatis-plus實現(xiàn)數(shù)據(jù)庫的增刪查改示例
- mybatis plus in方法使用詳解
- mybatis plus代碼生成工具的實現(xiàn)代碼
- Mybatis-plus與Mybatis依賴沖突問題解決方法
- MybatisPlus 不修改全局策略和字段注解如何將字段更新為null
相關(guān)文章
常用數(shù)字簽名算法RSA與DSA的Java程序內(nèi)實現(xiàn)示例
這篇文章主要介紹了常用數(shù)字簽名算法RSA與DSA的Java程序內(nèi)實現(xiàn)示例,一般來說DSA算法用于簽名的效率會比RSA要快,需要的朋友可以參考下2016-04-04Spring Boot拓展XML格式的請求和響應(yīng)操作過程
在我們開發(fā)過程中,我們經(jīng)常使用的參數(shù)絕大多少事HTML和JSON格式的請求和響應(yīng)處理,但是我們在實際開發(fā)過程中,我們可能經(jīng)歷一些,比如對于XML格式的請求,本文給大家介紹Spring Boot拓展XML格式的請求和響應(yīng),感興趣的朋友一起看看吧2023-10-10聊聊@RequestParam,@PathParam,@PathVariable等注解的區(qū)別
這篇文章主要介紹了聊聊@RequestParam,@PathParam,@PathVariable等注解的區(qū)別,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02Java并發(fā)編程之ReentrantLock實現(xiàn)原理及源碼剖析
ReentrantLock 是常用的鎖,相對于Synchronized ,lock鎖更人性化,閱讀性更強,文中將會詳細的說明,請君往下閱讀2021-09-09SpringMVC中Model和ModelAndView的EL表達式取值方法
下面小編就為大家分享一篇SpringMVC中Model和ModelAndView的EL表達式取值方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03Spring Boot+Mybatis+Pagehelper分頁實現(xiàn)
本篇文章主要講述的是Spring Boot+Mybatis+Pagehelper分頁實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04