springboot結(jié)合mybatis-plus快速生成項(xiàng)目模板的方法
mybatis-plus簡(jiǎn)介:
Mybatis-Plus(簡(jiǎn)稱(chēng)MP)是一個(gè) Mybatis 的增強(qiáng)工具,在 Mybatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開(kāi)發(fā)、提高效率而生。這是官方給的定義,關(guān)于mybatis-plus的更多介紹及特性,可以參考mybatis-plus官網(wǎng)。那么它是怎么增強(qiáng)的呢?其實(shí)就是它已經(jīng)封裝好了一些crud方法,我們不需要再寫(xiě)xml了,直接調(diào)用這些方法就行,就類(lèi)似于JPA。
項(xiàng)目模板
1、項(xiàng)目概覽
項(xiàng)目結(jié)構(gòu)

創(chuàng)建項(xiàng)目時(shí),父項(xiàng)目用springboot,子項(xiàng)目用maven,父項(xiàng)目統(tǒng)一管理,子項(xiàng)目分模塊
springboot父項(xiàng)目(子項(xiàng)目用maven建) - common模塊 - web模塊 - .......
父項(xiàng)目pom.xml:統(tǒng)一管理版本
<properties>
<!--java8-->
<java.version>1.8</java.version>
<!--mybatis-plus-->
<mp.version>3.4.2</mp.version>
<!--swagger-->
<swagger.version>3.0.0</swagger.version>
<!--velocity模板引擎-->
<velocity.version>2.3</velocity.version>
<!--java連接數(shù)據(jù)庫(kù)-->
<mysql-connect-java.version>8.0.25</mysql-connect-java.version>
<!--velocity-->
<velocity.version>2.3</velocity.version>
<!--mybatis-plus-generator-->
<mybatis-plus-generator.version>3.4.1</mybatis-plus-generator.version>
<!--knife4j-->
<knife4j.version>3.0.2</knife4j.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mp.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>${mybatis-plus-generator.version}</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>${velocity.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connect-java.version}</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>${knife4j.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
子項(xiàng)目pom.xml:公共模塊(common)提取出來(lái)
<dependencies>
<!--controller相關(guān)-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<!--代碼生成器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
</dependency>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
<!--java連接數(shù)據(jù)庫(kù)-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--knife4j-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
子項(xiàng)目web模塊pom.xml(直接引用common模塊)
<dependencies>
<dependency>
<groupId>cn.jie</groupId>
<artifactId>admin-base-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
代碼生成器
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("請(qǐng)輸入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotBlank(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("請(qǐng)輸入正確的" + tip + "!");
}
public static void main(String[] args) {
// 代碼生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = scanner("項(xiàng)目路徑");
//項(xiàng)目生成路徑
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("sky");
//打開(kāi)資源管理器
gc.setOpen(false);
//開(kāi)啟swagger
gc.setSwagger2(true);
//覆蓋文件
gc.setFileOverride(false);
gc.setServiceName("%sService");
//主鍵自增
gc.setIdType(IdType.AUTO);
//java.util.date
gc.setDateType(DateType.ONLY_DATE);
mpg.setGlobalConfig(gc);
// 數(shù)據(jù)源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/adminweb?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8");
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName(scanner("模塊名"));
pc.setParent("cn.jie");
pc.setEntity("entity");
pc.setMapper("mapper");
pc.setController("controller");
pc.setService("service");
pc.setServiceImpl("service.impl");
mpg.setPackageInfo(pc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
//表
strategy.setInclude(scanner("表名,多個(gè)英文逗號(hào)分割").split(","));
//駝峰命名
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//lombok
strategy.setEntityLombokModel(true);
//restful
strategy.setRestControllerStyle(true);
strategy.setControllerMappingHyphenStyle(true);
mpg.setStrategy(strategy);
mpg.execute();
}
}
springboot配置
spring:
application:
name: admin-base-web
datasource:
type:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/adminweb?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8
username: root
password: root
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
server:
port: 8081
mybatis-plus:
global-config:
db-config:
logic-delete-field: deleted # 全局邏輯刪除的實(shí)體字段名(since 3.3.0,配置后可以忽略不配置步驟2)
logic-delete-value: 1 # 邏輯已刪除值(默認(rèn)為 1)
logic-not-delete-value: 0 # 邏輯未刪除值(默認(rèn)為 0)
mapper-locations: classpath*:/mapper/*.xml

2、美化swagger-ui
swagger注意要用3.0.0版本
配置完swagger2config
@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class Swagger2Config {
@Bean(value = "defaultApi2")
public Docket defaultApi2() {
Docket docket=new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
//.title("swagger-bootstrap-ui-demo RESTful APIs")
.description("物資管理系統(tǒng)API文檔")
.termsOfServiceUrl("https://www.cnblogs.com/thatbluesky/")
.contact(new Contact("我的博客","https://www.cnblogs.com/thatbluesky/","1879186403@qq.com"))
.version("1.0")
.build())
//分組名稱(chēng)
.groupName("1.0版本")
.select()
//這里指定Controller掃描包路徑
.apis(RequestHandlerSelectors.basePackage("cn.jie.system.controller"))
.paths(PathSelectors.any())
.build();
return docket;
}
}
訪(fǎng)問(wèn):http://localhost:8081/doc.html

以上就是springboot結(jié)合mybatis-plus快速生成項(xiàng)目模板的方法的詳細(xì)內(nèi)容,更多關(guān)于springboot mybatis-plus項(xiàng)目模板 的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
如何只返回實(shí)體類(lèi)中的部分字段問(wèn)題
這篇文章主要介紹了如何只返回實(shí)體類(lèi)中的部分字段問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
SpringBoot整合PageHelper分頁(yè)無(wú)效的常見(jiàn)原因分析
這篇文章主要介紹了SpringBoot整合PageHelper分頁(yè)無(wú)效的常見(jiàn)原因分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Resttemplate中設(shè)置超時(shí)時(shí)長(zhǎng)方式
這篇文章主要介紹了Resttemplate中設(shè)置超時(shí)時(shí)長(zhǎng)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Kotlin與Java 泛型缺陷和應(yīng)用場(chǎng)景詳解
這篇文章主要為大家介紹了Kotlin與Java 泛型缺陷和應(yīng)用場(chǎng)景詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Spring Session實(shí)現(xiàn)分布式session的簡(jiǎn)單示例
本篇文章主要介紹了Spring Session實(shí)現(xiàn)分布式session的簡(jiǎn)單示例,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-05-05

