前言
大家好,本文是基于 SpringBoot 从0搭建一个企业级开发项目,基于SpringBoot 的项目,并集成MyBatis-Plus、Druid、Logback 等主流技术。希望对大家有所帮助~
目录
- 前言
- 一、概述
- 二、创建统一的依赖管理
- 2.1、统一的依赖管理工程创建
- 2.2、Maven托管
- 三、创建通用模块
- 3.1、创建通用模块工程
- 3.2、Maven托管
- 3.3、完善目录结构
- 3.4、定义通用类库
- 3.4.1、返回状态码
- 3.4.2、通用返回结果
- 3.4.3、通用工具类
- 四、创建项目服务模块
- 4.1、创建项目服务模块工程
- 4.2、Maven托管
- 4.3、完善目录结构
- 4.4、创建 Application
- 4.5、整合 Logback
- 4.6、整合 Druid 数据库连接池
- 五、创建代码生成模块
- 5.1、创建代码生成模块工程
- 5.2、Maven托管
- 5.3、完善目录结构
- 5.4、测试代码生成
- 5.5、测试生成效果
- 5.6、测试 Controller 层
- 六、源码获取
一、概述
随着技术栈的不断更新迭代,我们需要快速的在企业中搭建一个项目,并使用市面上前沿且稳定的技术栈。本期将跟大家一起从0到1搭建一个基于SpringBoot 的项目,并集成MyBatis-Plus、Druid、Logback 等主流技术。后续也会整合 Docker,自定义构建 Docker 项目,实现 Docker Compose 部署。源码附在文末。
二、创建统一的依赖管理
2.1、统一的依赖管理工程创建
创建一个名称为 my-project-dependencies
的项目,该项目负责统一的依赖管理
pom.xml
内容如下:
<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 http://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.3.9.RELEASE</version> </parent> <groupId>com.micromaple</groupId> <artifactId>my-project-dependencies</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>pom</packaging> <name>my-project-dependencies</name> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot-alibaba-druid.version>1.2.11</spring-boot-alibaba-druid.version> <spring-boot-mybatis-plus.version>3.5.2</spring-boot-mybatis-plus.version> <mysql.version>8.0.30</mysql.version> <lombok.version>1.18.18</lombok.version> <guava.version>25.1-jre</guava.version> <jackson.version>2.11.4</jackson.version> <apache-httpclient.version>4.5.5</apache-httpclient.version> <collections.version>3.2.1</collections.version> <poi.version>3.10-FINAL</poi.version> <hutool.version>5.3.7</hutool.version> <velocity.version>2.3</velocity.version> <hibernate-validator.version>6.0.15.Final</hibernate-validator.version> <log4j2.version>2.16.0</log4j2.version> <mybatis-plus-generator.version>3.5.1</mybatis-plus-generator.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${spring-boot-alibaba-druid.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${spring-boot-mybatis-plus.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>${guava.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>${collections.version}</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>${apache-httpclient.version}</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>fluent-hc</artifactId> <version>${apache-httpclient.version}</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>${apache-httpclient.version}</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>${poi.version}</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>${hutool.version}</version> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>${velocity.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>${mybatis-plus-generator.version}</version> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <showWarnings>true</showWarnings> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <addMavenDescriptor>false</addMavenDescriptor> </archive> </configuration> <executions> <execution> <configuration> <archive> <manifest> <addDefaultImplementationEntries>true</addDefaultImplementationEntries> <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries> <addClasspath>true</addClasspath> </manifest> </archive> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-install-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-clean-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> </plugin> </plugins> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <executions> <execution> <phase>prepare-package</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>yuicompressor-maven-plugin</artifactId> <version>1.5.1</version> <executions> <execution> <phase>prepare-package</phase> <goals> <goal>compress</goal> </goals> </execution> </executions> <configuration> <encoding>UTF-8</encoding> <jswarn>false</jswarn> <nosuffix>true</nosuffix> <linebreakpos>30000</linebreakpos> <force>true</force> <includes> <include>**/*.js</include> <include>**/*.css</include> </includes> <excludes> <exclude>**/*.min.js</exclude> <exclude>**/*.min.css</exclude> </excludes> </configuration> </plugin> </plugins> </pluginManagement> <resources> <resource> <directory>src/main/java</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> </build></project>
2.2、Maven托管
将其交给Maven
托管
三、创建通用模块
3.1、创建通用模块工程
创建一个名称为 my-project-common
的项目,该项目负责通用的类库以及工具类
pom.xml
内容如下:
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.micromaple</groupId> <artifactId>my-project-dependencies</artifactId> <version>1.0.0-SNAPSHOT</version> <relativePath>../my-project-dependencies/pom.xml</relativePath> </parent> <artifactId>my-project-common</artifactId> <packaging>jar</packaging> <name>my-project-common</name> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>fluent-hc</artifactId> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> </dependency> </dependencies></project>
3.2、Maven托管
可以使用右侧的Maven
的 +
号来进行项目的托管。
3.3、完善目录结构
创建 src/main/java
目录后,创建包名,包名为:com.micromaple.my.project.common
此工程是放一些通用的类库或者是工具类之类的。
3.4、定义通用类库
3.4.1、返回状态码
创建常量包constant
。
通用常量返回状态码HttpStatus
package com.micromaple.my.project.common.constant;/** * 返回状态码 * Title: HttpStatus * Description: * * @author Micromaple */public class HttpStatus { /** * 操作成功 */ public static final int SUCCESS = 200; /** * 对象创建成功 */ public static final int CREATED = 201; /** * 请求已经被接受 */ public static final int ACCEPTED = 202; /** * 操作已经执行成功,但是没有返回数据 */ public static final int NO_CONTENT = 204; /** * 资源已被移除 */ public static final int MOVED_PERM = 301; /** * 重定向 */ public static final int SEE_OTHER = 303; /** * 资源没有被修改 */ public static final int NOT_MODIFIED = 304; /** * 参数列表错误(缺少,格式不匹配) */ public static final int BAD_REQUEST = 400; /** * 未授权 */ public static final int UNAUTHORIZED = 401; /** * 访问受限,授权过期 */ public static final int FORBIDDEN = 403; /** * 资源,服务未找到 */ public static final int NOT_FOUND = 404; /** * 不允许的http方法 */ public static final int BAD_METHOD = 405; /** * 资源冲突,或者资源被锁 */ public static final int CONFLICT = 409; /** * 不支持的数据,媒体类型 */ public static final int UNSUPPORTED_TYPE = 415; /** * 系统内部错误 */ public static final int ERROR = 500; /** * 接口未实现 */ public static final int NOT_IMPLEMENTED = 501;}
3.4.2、通用返回结果
创建数据传输包dto
。
通用数据传输返回结果BaseResult
package com.micromaple.my.project.common.dto;import com.micromaple.my.project.common.constant.HttpStatus;import java.util.HashMap;/** * 返回结果 * Title: BaseResult * Description: * * @author Micromaple */public class BaseResult extends HashMap<String, Object> { private static final long serialVersionUID = 1L; /** * 状态码 */ public static final String CODE_TAG = "code"; /** * 返回内容 */ public static final String MSG_TAG = "msg"; /** * 数据对象 */ public static final String DATA_TAG = "data"; /** * 初始化一个新创建的 BaseResult 对象,使其表示一个空消息。 */ public BaseResult() { } /** * 初始化一个新创建的 BaseResult 对象 * * @param code 状态码 * @param msg 返回内容 */ public BaseResult(int code, String msg) { super.put(CODE_TAG, code); super.put(MSG_TAG, msg); } /** * 初始化一个新创建的 BaseResult 对象 * * @param code 状态码 * @param msg 返回内容 * @param data 数据对象 */ public BaseResult(int code, String msg, Object data) { super.put(CODE_TAG, code); super.put(MSG_TAG, msg); super.put(DATA_TAG, data); } /** * 返回成功消息 * * @return 成功消息 */ public static BaseResult success() { return BaseResult.success("操作成功"); } /** * 返回成功数据 * * @return 成功消息 */ public static BaseResult success(Object data) { return BaseResult.success("操作成功", data); } /** * 返回成功消息 * * @param msg 返回内容 * @return 成功消息 */ public static BaseResult success(String msg) { return BaseResult.success(msg, null); } /** * 返回成功消息 * * @param msg 返回内容 * @param data 数据对象 * @return 成功消息 */ public static BaseResult success(String msg, Object data) { return new BaseResult(HttpStatus.SUCCESS, msg, data); } /** * 返回错误消息 * * @return */ public static BaseResult error() { return BaseResult.error("操作失败"); } /** * 返回错误消息 * * @param msg 返回内容 * @return 警告消息 */ public static BaseResult error(String msg) { return BaseResult.error(msg, null); } /** * 返回错误消息 * * @param msg 返回内容 * @param data 数据对象 * @return 警告消息 */ public static BaseResult error(String msg, Object data) { return new BaseResult(HttpStatus.ERROR, msg, data); } /** * 返回错误消息 * * @param code 状态码 * @param msg 返回内容 * @return 警告消息 */ public static BaseResult error(int code, String msg) { return new BaseResult(code, msg, null); }}
3.4.3、通用工具类
创建工具类包utils
。
通用Jackson
工具类MapperUtils
package com.micromaple.my.project.common.utils;import com.fasterxml.jackson.annotation.JsonInclude;import com.fasterxml.jackson.databind.DeserializationFeature;import com.fasterxml.jackson.databind.JavaType;import com.fasterxml.jackson.databind.ObjectMapper;import org.apache.commons.lang3.StringUtils;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.Map;/** * Jackson 工具类 * Title: MapperUtils * Description: * * @author Micromaple */public class MapperUtils { private final static ObjectMapper objectMapper = new ObjectMapper(); public static ObjectMapper getInstance() { return objectMapper; } /** * 转换为 JSON 字符串 * * @param obj * @return * @throws Exception */ public static String obj2json(Object obj) { try { return objectMapper.writeValueAsString(obj); } catch (Exception e) { e.printStackTrace(); } return ""; } /** * 转换为 JSON 字符串,忽略空值 * * @param obj * @return * @throws Exception */ public static String obj2jsonIgnoreNull(Object obj) throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); return mapper.writeValueAsString(obj); } /** * 转换为 JavaBean * * @param jsonString * @param clazz * @return * @throws Exception */ public static <T> T json2pojo(String jsonString, Class<T> clazz){ objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); try{ return objectMapper.readValue(jsonString, clazz); }catch (Exception e){ e.printStackTrace(); } return null; } /** * 字符串转换为 Map * * @param jsonString * @return * @throws Exception */ public static <T> Map<String, Object> json2map(String jsonString){ if(StringUtils.isBlank(jsonString)) return null; ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); try { return mapper.readValue(jsonString, Map.class); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 深度转换 JSON 成 Map * * @param json * @return */ public static Map<String, Object> json2mapDeeply(String json) throws Exception { return json2MapRecursion(json, objectMapper); } /** * 把 JSON 解析成 List,如果 List 内部的元素存在 jsonString,继续解析 * * @param json * @param mapper 解析工具 * @return * @throws Exception */ private static List<Object> json2ListRecursion(String json, ObjectMapper mapper) throws Exception { if (json == null) { return null; } List<Object> list = mapper.readValue(json, List.class); for (Object obj : list) { if (obj != null && obj instanceof String) { String str = (String) obj; if (str.startsWith("[")) { obj = json2ListRecursion(str, mapper); } else if (obj.toString().startsWith("{")) { obj = json2MapRecursion(str, mapper); } } } return list; } /** * 把 JSON 解析成 Map,如果 Map 内部的 Value 存在 jsonString,继续解析 * * @param json * @param mapper * @return * @throws Exception */ private static Map<String, Object> json2MapRecursion(String json, ObjectMapper mapper) throws Exception { if (json == null) { return null; } Map<String, Object> map = mapper.readValue(json, Map.class); for (Map.Entry<String, Object> entry : map.entrySet()) { Object obj = entry.getValue(); if (obj != null && obj instanceof String) { String str = ((String) obj); if (str.startsWith("[")) { List<" /> 四、创建项目服务模块
4.1、创建项目服务模块工程
创建一个名称为 my-project-server
的项目,该项目负责实现具体业务,以及提供服务。
pom.xml
内容如下:
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.micromaple</groupId> <artifactId>my-project-dependencies</artifactId> <version>1.0.0-SNAPSHOT</version> <relativePath>../my-project-dependencies/pom.xml</relativePath> </parent> <artifactId>my-project-server</artifactId> <packaging>jar</packaging> <name>my-project-server</name> <dependencies> <dependency> <groupId>com.micromaple</groupId> <artifactId>my-project-common</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass></mainClass> </configuration> </plugin> </plugins> </build></project>
4.2、Maven托管
可以使用右侧的Maven
的 +
号来进行项目的托管。
4.3、完善目录结构
创建 src/main/java
和 src/main/resource
目录
在 src/main/java
目录下创建包名,包名为:com.micromaple.my.project.server
在 src/main/resource
目录下创建application.yml
,内容如下:
# Spring配置spring: jackson: #时间戳统一转换为指定格式 date-format: yyyy-MM-dd HH:mm:ss # 时区修改为东8区 time-zone: GMT+8server: port: 8899
4.4、创建 Application
这里使用 Spring 提供的 MyBatis 包扫面注解
package com.micromaple.my.project.server;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@MapperScan(value = "com.micromaple.my.project.server.mapper")public class MyProjectServerApplication { public static void main(String[] args) { SpringApplication.run(MyProjectServerApplication.class, args); }}
在 pom.xml
中的 mainClass 指定该 Application
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.micromaple.my.project.server.MyProjectServerApplication</mainClass> </configuration> </plugin> </plugins> </build>
4.5、整合 Logback
在 src/main/resource
目录下创建logback-spring.xml
,内容如下:
<configuration> <substitutionProperty name="logBase" value="./logs/" /> <substitutionProperty name="logName" value="my-project-server" /> <substitutionProperty name="logPatternExp" value="%date{YYYY-MM-dd HH:mm:ss.SSS} [%-5level] [%thread] %logger{9999} %L -=> %msg%n" /> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${logPatternExp}</pattern> </encoder> </appender> <appender name="rollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${logBase}${logName}.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${logBase}${logName}-%d{yyyy-MM-dd}-%i.zip</fileNamePattern> <maxHistory>180</maxHistory> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>30MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <pattern>${logPatternExp}</pattern> </encoder> </appender> <logger name="com.micromaple.my.project.server" level="DEBUG" /> <logger name="org.springframework" level="ERROR" /> <root level="DEBUG"> <appender-ref ref="console" /> <appender-ref ref="rollingFile" /> </root></configuration>
在application.yml
增加配置:
logging: config: classpath:logback-spring.xml
4.6、整合 Druid 数据库连接池
在 src/main/resource
目录下创建application-druid.yml
,内容如下:
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://192.168.110.135:3306/my-project?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8 username: root password: 123456 druid: #连接池配置 初始化连接池的连接数量 大小,最小,最大 initial-size: 5 min-idle: 5 max-active: 20 #配置获取连接等待超时的时间 max-wait: 60000 #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 time-between-eviction-runs-millis: 60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 min-evictable-idle-time-millis: 30000 validation-query: SELECT 1 FROM DUAL test-while-idle: true test-on-borrow: true test-on-return: false # 是否缓存preparedStatement,也就是PSCache 官方建议MySQL下建议关闭 个人建议如果想用SQL防火墙 建议打开 pool-prepared-statements: true max-pool-prepared-statement-per-connection-size: 20 # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 filter: stat: merge-sql: true slow-sql-millis: 5000 #3.基础监控配置 web-stat-filter: enabled: true url-pattern: /* #设置不统计哪些URL exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*" session-stat-enable: true session-stat-max-count: 100 stat-view-servlet: enabled: true # 数据监控访问路径 url-pattern: /druid/* reset-enable: true #设置监控页面的登录名和密码 login-username: admin login-password: admin allow: 127.0.0.1
在 application.yml
增加配置,内容如下:
# Spring配置spring: # 启用指定配置文件 #(文件名需满足application-{profile}.yml的格式) profiles: active: druid
application.yml
完整配置如下:
# Spring配置spring: jackson: #时间戳统一转换为指定格式 date-format: yyyy-MM-dd HH:mm:ss # 时区修改为东8区 time-zone: GMT+8 # 启用指定配置文件 #(文件名需满足application-{profile}.yml的格式) profiles: active: druidserver: port: 8899logging: config: classpath:logback-spring.xml
最终项目效果图如下:
五、创建代码生成模块
5.1、创建代码生成模块工程
创建一个名称为 my-project-generator
的项目,该项目负责通过数据库自动生成通用的增删改查。
pom.xml
内容如下:
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.micromaple</groupId> <artifactId>my-project-dependencies</artifactId> <version>1.0.0-SNAPSHOT</version> <relativePath>../my-project-dependencies/pom.xml</relativePath> </parent> <artifactId>my-project-generator</artifactId> <packaging>jar</packaging> <name>my-project-generator</name> <dependencies> <dependency> <groupId>com.micromaple</groupId> <artifactId>my-project-common</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> </dependency> </dependencies></project>
5.2、Maven托管
可以使用右侧的Maven
的 +
号来进行项目的托管。
5.3、完善目录结构
创建 src/main/java
和 src/main/resource
目录
在 src/main/java
目录下创建包名,包名为:com.micromaple.my.project.generator
。
创建代码生成类,内容如下:
package com.micromaple.my.project.generator;import com.baomidou.mybatisplus.generator.FastAutoGenerator;import com.baomidou.mybatisplus.generator.config.DataSourceConfig;import com.baomidou.mybatisplus.generator.config.OutputFile;import com.baomidou.mybatisplus.generator.config.StrategyConfig;import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;import java.util.Collections;/** * 代码生成 * Title: GeneratorApplication * Description: * * @author Micromaple */public class GeneratorApplication { public static void main(String[] args) { String projectPath = System.getProperty("user.dir"); String finalProjectPath = projectPath + "/my-project-generator"; // 数据源配置 DataSourceConfig.Builder dataSourceConfig = new DataSourceConfig .Builder( "jdbc:mysql://192.168.110.135:3306/my-project?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8", "root", "123456") .dbQuery(new MySqlQuery()) .typeConvert(new MySqlTypeConvert()) .keyWordsHandler(new MySqlKeyWordsHandler()); new StrategyConfig.Builder() .serviceBuilder() .formatServiceFileName("%sService") .formatServiceImplFileName("%sServiceImp") .build(); FastAutoGenerator.create(dataSourceConfig) .globalConfig(builder -> { builder.author("Micromaple") //设置作者 .commentDate("yyyy-MM-dd HH:mm:ss")//注释日期 .outputDir(finalProjectPath + "/src/main/java"); //指定输出目录 }) .packageConfig(builder -> { builder.parent("com.micromaple.my.project.server") .moduleName("") // 设置父包模块名 .entity("domain") //设置entity包名 //.other("model.dto") // 设置dto包名 .pathInfo(Collections.singletonMap(OutputFile.mapperXml, finalProjectPath + "/src/main/resources/mapper")); // 设置mapperXml生成路径 }) .strategyConfig(builder -> { builder.addInclude("sys_user") // 设置需要生成的表名 .addTablePrefix("tb_"); // 设置过滤表前缀 builder.entityBuilder().enableLombok();//开启 lombok 模型 builder.entityBuilder().enableTableFieldAnnotation();//开启生成实体时生成字段注解 builder.controllerBuilder().enableRestStyle();//开启生成@RestController 控制器 }) .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板 .execute(); }}
5.4、测试代码生成
建表语句:
CREATE TABLE `sys_user` ( `id` int NOT NULL AUTO_INCREMENT COMMENT '主键', `code` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '编号', `name` varchar(50) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '姓名', `sex` varchar(2) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '性别', `age` int DEFAULT NULL COMMENT '年龄', `birthday` date DEFAULT NULL COMMENT '生日', `mail` varchar(50) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '邮箱', `created_date` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `updated_date` datetime DEFAULT NULL COMMENT '更新时间', PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='用户表'
新增数据:
insert into `sys_user` (`code`, `name`, `sex`, `age`, `birthday`, `mail`, `created_date`, `updated_date`) values('admin','超级管理员','男','18','2004-01-01','micromaple@qq.com','2022-09-21 21:48:33',NULL);
执行 GeneratorApplication
类的 main 方法后,可以看到生成的代码,效果如下图:
将其拷贝到my-project-server
工程模块中,完整效果如下:
5.5、测试生成效果
我们在 my-project-server
工程编写单元测试,测试生成的代码是否可用。
创建 src/test/java
目录,在该目录创建包 com.micromaple.my.project.server.service
;
接着创建测试类 SysUserServiceTest
,内容如下:
package com.micromaple.my.project.server.service;import com.micromaple.my.project.server.MyProjectServerApplication;import com.micromaple.my.project.server.domain.SysUser;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import java.util.List;@SpringBootTest(classes = MyProjectServerApplication.class)@RunWith(SpringRunner.class)public class SysUserServiceTest { @Autowired private ISysUserService sysUserService; /** * 测试新增 */ @Test public void addTest() { SysUser sysUser = new SysUser(); sysUser.setCode("user"); sysUser.setName("用户"); sysUser.setSex("女"); sysUser.setAge(18); boolean save = sysUserService.save(sysUser); System.out.println(save); // 输出 true 代表插入成功 } /** * 测试查询 */ @Test public void selectTest() { List<SysUser> sysUsers = sysUserService.getBaseMapper().selectList(null); for (SysUser sysUser : sysUsers) { System.out.println(sysUser.getName()); } /** * 输出内容如下: * 超级管理员 * 用户 */ }}
5.6、测试 Controller 层
SysUserController 代码如下:
package com.micromaple.my.project.server.controller;import com.micromaple.my.project.common.dto.BaseResult;import com.micromaple.my.project.common.utils.MapperUtils;import com.micromaple.my.project.server.domain.SysUser;import com.micromaple.my.project.server.service.ISysUserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;/** * * 用户表 前端控制器 *
* * @author Micromaple * @since 2022-09-21 21:51:15 */@RestController@RequestMapping("/sys-user")public class SysUserController { @Autowired private ISysUserService sysUserService; @GetMapping("/get/all") public BaseResult getAll() { List<SysUser> sysUsers = sysUserService.getBaseMapper().selectList(null); return BaseResult.success(sysUsers); }}
启动 MyProjectServerApplication
服务,访问地址:
http://localhost:8899/sys-user/get/all
效果如下:
六、源码获取
源码在 GitHub
和 码云
,持续更新中,别忘了 star
喔~
GitHub
https://github.com/micromaples/my-project
码云gitee
https://gitee.com/micromaple/my-project
读到这里,想必你已经知道了如何搭建一个Spring Boot项目,并整合其他技术栈,快跟着一起动手吧。别忘了三连支持博主呀~
我是微枫Micromaple,期待你的关注~