0


从0到1项目搭建-框架搭建(附源码)

前言

大家好,本文是基于 SpringBoot 从0搭建一个企业级开发项目,基于SpringBoot 的项目,并集成MyBatis-Plus、Druid、Logback 等主流技术。希望对大家有所帮助~

目录

一、概述

随着技术栈的不断更新迭代,我们需要快速的在企业中搭建一个项目,并使用市面上前沿且稳定的技术栈。本期将跟大家一起从0到1搭建一个基于SpringBoot 的项目,并集成MyBatis-Plus、Druid、Logback 等主流技术。后续也会整合 Docker,自定义构建 Docker 项目,实现 Docker Compose 部署。源码附在文末。

二、创建统一的依赖管理

2.1、统一的依赖管理工程创建

创建一个名称为

my-project-dependencies

的项目,该项目负责统一的依赖管理

pom.xml

内容如下:

<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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><!-- Environment Settings --><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><!-- Spring Settings --><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><!-- Environment Setting --><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><!-- Common Setting--><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 --><log4j2.version>2.16.0</log4j2.version><!-- mybatis-plus-generator --><mybatis-plus-generator.version>3.5.1</mybatis-plus-generator.version></properties><dependencyManagement><dependencies><!-- Spring Boot Starter Begin --><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><!-- Spring Boot Starter End --><!-- Environment Begin --><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><!-- Environment End --><!-- Apache Http Begin --><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><!-- Apache Http End --><!-- Common Begin --><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><!-- Common End --><!--mybatis-plus代码生成器--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId><version>${mybatis-plus-generator.version}</version></dependency></dependencies></dependencyManagement><build><plugins><!-- Compiler 插件, 设定 JDK 版本 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><showWarnings>true</showWarnings></configuration></plugin><!-- 打包 jar 文件时,配置 manifest 文件,加入 lib 包的 jar 依赖 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><configuration><archive><addMavenDescriptor>false</addMavenDescriptor></archive></configuration><executions><execution><configuration><archive><manifest><!-- Add directory entries --><addDefaultImplementationEntries>true</addDefaultImplementationEntries><addDefaultSpecificationEntries>true</addDefaultSpecificationEntries><addClasspath>true</addClasspath></manifest></archive></configuration></execution></executions></plugin><!-- resource --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId></plugin><!-- install --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-install-plugin</artifactId></plugin><!-- clean --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-clean-plugin</artifactId></plugin><!-- ant --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-antrun-plugin</artifactId></plugin><!-- dependency --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId></plugin></plugins><pluginManagement><plugins><!-- Java Document Generate --><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><!-- YUI Compressor (CSS/JS压缩) --><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

内容如下:

<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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><!-- Commons Begin--><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId></dependency><!-- Commons End--><!-- Environment Begin --><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><!-- Environment End --><!-- Apache Http Begin --><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><!-- Apache Http End --></dependencies></project>

3.2、Maven托管

可以使用右侧的

Maven

+

号来进行项目的托管。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.3、完善目录结构

创建

src/main/java

目录后,创建包名,包名为:

com.micromaple.my.project.common

此工程是放一些通用的类库或者是工具类之类的。

3.4、定义通用类库

3.4.1、返回状态码

创建常量包

constant

通用常量返回状态码

HttpStatus
packagecom.micromaple.my.project.common.constant;/**
 * 返回状态码
 * Title: HttpStatus
 * Description:
 *
 * @author Micromaple
 */publicclassHttpStatus{/**
     * 操作成功
     */publicstaticfinalint SUCCESS =200;/**
     * 对象创建成功
     */publicstaticfinalint CREATED =201;/**
     * 请求已经被接受
     */publicstaticfinalint ACCEPTED =202;/**
     * 操作已经执行成功,但是没有返回数据
     */publicstaticfinalint NO_CONTENT =204;/**
     * 资源已被移除
     */publicstaticfinalint MOVED_PERM =301;/**
     * 重定向
     */publicstaticfinalint SEE_OTHER =303;/**
     * 资源没有被修改
     */publicstaticfinalint NOT_MODIFIED =304;/**
     * 参数列表错误(缺少,格式不匹配)
     */publicstaticfinalint BAD_REQUEST =400;/**
     * 未授权
     */publicstaticfinalint UNAUTHORIZED =401;/**
     * 访问受限,授权过期
     */publicstaticfinalint FORBIDDEN =403;/**
     * 资源,服务未找到
     */publicstaticfinalint NOT_FOUND =404;/**
     * 不允许的http方法
     */publicstaticfinalint BAD_METHOD =405;/**
     * 资源冲突,或者资源被锁
     */publicstaticfinalint CONFLICT =409;/**
     * 不支持的数据,媒体类型
     */publicstaticfinalint UNSUPPORTED_TYPE =415;/**
     * 系统内部错误
     */publicstaticfinalint ERROR =500;/**
     * 接口未实现
     */publicstaticfinalint NOT_IMPLEMENTED =501;}

3.4.2、通用返回结果

创建数据传输包

dto

通用数据传输返回结果

BaseResult
packagecom.micromaple.my.project.common.dto;importcom.micromaple.my.project.common.constant.HttpStatus;importjava.util.HashMap;/**
 * 返回结果
 * Title: BaseResult
 * Description:
 *
 * @author Micromaple
 */publicclassBaseResultextendsHashMap<String,Object>{privatestaticfinallong serialVersionUID =1L;/**
     * 状态码
     */publicstaticfinalString CODE_TAG ="code";/**
     * 返回内容
     */publicstaticfinalString MSG_TAG ="msg";/**
     * 数据对象
     */publicstaticfinalString DATA_TAG ="data";/**
     * 初始化一个新创建的 BaseResult 对象,使其表示一个空消息。
     */publicBaseResult(){}/**
     * 初始化一个新创建的 BaseResult 对象
     *
     * @param code 状态码
     * @param msg  返回内容
     */publicBaseResult(int code,String msg){super.put(CODE_TAG, code);super.put(MSG_TAG, msg);}/**
     * 初始化一个新创建的 BaseResult 对象
     *
     * @param code 状态码
     * @param msg  返回内容
     * @param data 数据对象
     */publicBaseResult(int code,String msg,Object data){super.put(CODE_TAG, code);super.put(MSG_TAG, msg);super.put(DATA_TAG, data);}/**
     * 返回成功消息
     *
     * @return 成功消息
     */publicstaticBaseResultsuccess(){returnBaseResult.success("操作成功");}/**
     * 返回成功数据
     *
     * @return 成功消息
     */publicstaticBaseResultsuccess(Object data){returnBaseResult.success("操作成功", data);}/**
     * 返回成功消息
     *
     * @param msg 返回内容
     * @return 成功消息
     */publicstaticBaseResultsuccess(String msg){returnBaseResult.success(msg,null);}/**
     * 返回成功消息
     *
     * @param msg  返回内容
     * @param data 数据对象
     * @return 成功消息
     */publicstaticBaseResultsuccess(String msg,Object data){returnnewBaseResult(HttpStatus.SUCCESS, msg, data);}/**
     * 返回错误消息
     *
     * @return
     */publicstaticBaseResulterror(){returnBaseResult.error("操作失败");}/**
     * 返回错误消息
     *
     * @param msg 返回内容
     * @return 警告消息
     */publicstaticBaseResulterror(String msg){returnBaseResult.error(msg,null);}/**
     * 返回错误消息
     *
     * @param msg  返回内容
     * @param data 数据对象
     * @return 警告消息
     */publicstaticBaseResulterror(String msg,Object data){returnnewBaseResult(HttpStatus.ERROR, msg, data);}/**
     * 返回错误消息
     *
     * @param code 状态码
     * @param msg  返回内容
     * @return 警告消息
     */publicstaticBaseResulterror(int code,String msg){returnnewBaseResult(code, msg,null);}}

3.4.3、通用工具类

创建工具类包

utils

通用

Jackson

工具类

MapperUtils
packagecom.micromaple.my.project.common.utils;importcom.fasterxml.jackson.annotation.JsonInclude;importcom.fasterxml.jackson.databind.DeserializationFeature;importcom.fasterxml.jackson.databind.JavaType;importcom.fasterxml.jackson.databind.ObjectMapper;importorg.apache.commons.lang3.StringUtils;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;importjava.util.Map;/**
 * Jackson 工具类
 * Title: MapperUtils
 * Description:
 *
 * @author Micromaple
 */publicclassMapperUtils{privatefinalstaticObjectMapper objectMapper =newObjectMapper();publicstaticObjectMappergetInstance(){return objectMapper;}/**
     * 转换为 JSON 字符串
     *
     * @param obj
     * @return
     * @throws Exception
     */publicstaticStringobj2json(Object obj){try{return objectMapper.writeValueAsString(obj);}catch(Exception e){
            e.printStackTrace();}return"";}/**
     * 转换为 JSON 字符串,忽略空值
     *
     * @param obj
     * @return
     * @throws Exception
     */publicstaticStringobj2jsonIgnoreNull(Object obj)throwsException{ObjectMapper mapper =newObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);return mapper.writeValueAsString(obj);}/**
     * 转换为 JavaBean
     *
     * @param jsonString
     * @param clazz
     * @return
     * @throws Exception
     */publicstatic<T>Tjson2pojo(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();}returnnull;}/**
     * 字符串转换为 Map<String, Object>
     *
     * @param jsonString
     * @return
     * @throws Exception
     */publicstatic<T>Map<String,Object>json2map(String jsonString){if(StringUtils.isBlank(jsonString))returnnull;ObjectMapper mapper =newObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);try{return mapper.readValue(jsonString,Map.class);}catch(IOException e){
            e.printStackTrace();}returnnull;}/**
     * 深度转换 JSON 成 Map
     *
     * @param json
     * @return
     */publicstaticMap<String,Object>json2mapDeeply(String json)throwsException{returnjson2MapRecursion(json, objectMapper);}/**
     * 把 JSON 解析成 List,如果 List 内部的元素存在 jsonString,继续解析
     *
     * @param json
     * @param mapper 解析工具
     * @return
     * @throws Exception
     */privatestaticList<Object>json2ListRecursion(String json,ObjectMapper mapper)throwsException{if(json ==null){returnnull;}List<Object> list = mapper.readValue(json,List.class);for(Object obj : list){if(obj !=null&& obj instanceofString){String str =(String) obj;if(str.startsWith("[")){
                    obj =json2ListRecursion(str, mapper);}elseif(obj.toString().startsWith("{")){
                    obj =json2MapRecursion(str, mapper);}}}return list;}/**
     * 把 JSON 解析成 Map,如果 Map 内部的 Value 存在 jsonString,继续解析
     *
     * @param json
     * @param mapper
     * @return
     * @throws Exception
     */privatestaticMap<String,Object>json2MapRecursion(String json,ObjectMapper mapper)throwsException{if(json ==null){returnnull;}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 instanceofString){String str =((String) obj);if(str.startsWith("[")){List<?> list =json2ListRecursion(str, mapper);
                    map.put(entry.getKey(), list);}elseif(str.startsWith("{")){Map<String,Object> mapRecursion =json2MapRecursion(str, mapper);
                    map.put(entry.getKey(), mapRecursion);}}}return map;}/**
     * 将 JSON 数组转换为集合
     *
     * @param jsonArrayStr
     * @param clazz
     * @return
     * @throws Exception
     */publicstatic<T>List<T>json2list(String jsonArrayStr,Class<T> clazz)throwsException{JavaType javaType =getCollectionType(ArrayList.class, clazz);List<T> list =(List<T>) objectMapper.readValue(jsonArrayStr, javaType);return list;}/**
     * 获取泛型的 Collection Type
     *
     * @param collectionClass 泛型的Collection
     * @param elementClasses  元素类
     * @return JavaType Java类型
     * @since 1.0
     */publicstaticJavaTypegetCollectionType(Class<?> collectionClass,Class<?>... elementClasses){return objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);}/**
     * 将 Map 转换为 JavaBean
     *
     * @param map
     * @param clazz
     * @return
     */publicstatic<T>Tmap2pojo(T map,Class<T> clazz){return objectMapper.convertValue(map, clazz);}/**
     * 将 Map 转换为 JSON
     *
     * @param map
     * @return
     */publicstaticStringmapToJson(Map map){try{return objectMapper.writeValueAsString(map);}catch(Exception e){
            e.printStackTrace();}return"";}/**
     * 将 JSON 对象转换为 JavaBean
     *
     * @param obj
     * @param clazz
     * @return
     */publicstatic<T>Tobj2pojo(Object obj,Class<T> clazz){return objectMapper.convertValue(obj, clazz);}}
HttpClient

工具类

packagecom.micromaple.my.project.common.utils;importcom.google.common.collect.Maps;importorg.apache.http.HttpEntity;importorg.apache.http.HttpHost;importorg.apache.http.client.config.RequestConfig;importorg.apache.http.client.entity.UrlEncodedFormEntity;importorg.apache.http.client.methods.CloseableHttpResponse;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.entity.StringEntity;importorg.apache.http.impl.client.CloseableHttpClient;importorg.apache.http.impl.client.HttpClients;importorg.apache.http.message.BasicNameValuePair;importorg.apache.http.util.EntityUtils;importjava.io.IOException;importjava.util.Arrays;importjava.util.Map;/**
 * HttpClient 工具类
 * Title: HttpClientUtils
 * Description:
 *
 * @author Micromaple
 */publicclassHttpClientUtils{publicstaticfinalString GET ="get";publicstaticfinalString POST ="post";publicstaticfinalString REQUEST_HEADER_CONNECTION ="keep-alive";publicstaticfinalString REQUEST_HEADER_CONTENTTYPE ="application/json;charset=UTF-8";publicstaticfinalString REQUEST_HEADER_CONSUMES ="application/json";//设置连接超时时间,单位毫秒。publicstaticfinalint CONNECTTIMEOUT =18000;//请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。publicstaticfinalint SOCKETTIMEOUT =60000;//设置从connect Manager获取Connection 超时时间,单位毫秒publicstaticfinalint CONNECTIONREQUESTTIMEOUT =18000;/**
     * GET 请求
     *
     * @param url 请求地址
     * @return
     */publicstaticStringdoGet(String url){returncreateRequest(url, GET,null,false,null,0);}/**
     * GET 请求 - 代理
     *
     * @param url       请求地址
     * @param useProxy  是否需要代理
     * @param proxyHost 代理地址
     * @param proxyPort 代理端口
     * @return
     */publicstaticStringdoGet(String url,boolean useProxy,String proxyHost,int proxyPort){returncreateRequest(url, GET,null, useProxy, proxyHost, proxyPort);}/**
     * GET 请求
     *
     * @param url    请求地址
     * @param cookie cookie
     * @return
     */publicstaticStringdoGet(String url,String cookie){returncreateRequest(url, GET, cookie,false,null,0);}/**
     * POST 请求
     *
     * @param url    请求地址
     * @param params 请求参数(可选)
     * @return
     */publicstaticStringdoPost(String url,BasicNameValuePair... params){returncreateRequest(url, POST,null,false,null,0, params);}/**
     * POST 请求 - 代理
     *
     * @param url       请求地址
     * @param useProxy  是否需要代理
     * @param proxyHost 代理地址
     * @param proxyPort 代理端口
     * @param params    请求参数(可选)
     * @return
     */publicstaticStringdoPost(String url,boolean useProxy,String proxyHost,int proxyPort,BasicNameValuePair... params){returncreateRequest(url, POST,null, useProxy, proxyHost, proxyPort, params);}/**
     * POST 请求
     *
     * @param url    请求地址
     * @param cookie cookie
     * @param params 请求参数(可选)
     * @return
     */publicstaticStringdoPost(String url,String cookie,BasicNameValuePair... params){returncreateRequest(url, POST, cookie,false,null,0, params);}/**
     * POST 请求 - 文本 方式
     *
     * @param url            请求地址
     * @param jsonBodyParams 请求参数 (JSON 格式)
     * @return
     */publicstaticStringdoPostForRow(String url,String jsonBodyParams){returncreateRequestForRow(url, jsonBodyParams,false,null,0);}/**
     * POST 请求 - 文本 方式 - 代理
     *
     * @param url            请求地址
     * @param jsonBodyParams 请求参数
     * @param useProxy       是否需要代理
     * @param proxyHost      代理地址
     * @param proxyPort      代理端口
     * @return
     */publicstaticStringdoPostForRow(String url,String jsonBodyParams,boolean useProxy,String proxyHost,int proxyPort){returncreateRequestForRow(url, jsonBodyParams, useProxy, proxyHost, proxyPort);}/**
     * POST 请求
     *
     * @param url       请求地址
     * @param pMap      请求参数
     * @param useProxy  是否需要代理
     * @param proxyHost 代理地址
     * @param proxyPort 代理端口
     * @return
     */publicstaticMap<String,Object>doPost(String url,Map<String,String> pMap,boolean useProxy,String proxyHost,int proxyPort){String str =HttpClientUtils.doPost(url, useProxy, proxyHost, proxyPort,generatNameValuePair(pMap));Map<String,Object> rtnMap =Maps.newHashMap();try{
            rtnMap =MapperUtils.json2map(str);}catch(Exception e){
            e.printStackTrace();}return rtnMap;}/**
     * 创建请求
     *
     * @param url           请求地址
     * @param requestMethod 请求方式 GET/POST
     * @param cookie        cookie
     * @param useProxy      是否需要代理
     * @param proxyHost     代理地址
     * @param proxyPort     代理端口
     * @param params        请求参数 仅限于 POST 请求用
     * @return
     */privatestaticStringcreateRequest(String url,String requestMethod,String cookie,boolean useProxy,String proxyHost,int proxyPort,BasicNameValuePair... params){//创建 HTTPClient 客户端//        CloseableHttpClient httpClient = HttpClients.createDefault();CloseableHttpClient httpClient =getHttpClient(useProxy, proxyHost, proxyPort);String result =null;try{//请求结果
            result =null;//请求方式HttpGet httpGet =null;HttpPost httpPost =null;//响应CloseableHttpResponse httpResponse =null;// GET 请求if(GET.equals(requestMethod)){
                httpGet =newHttpGet(url);
                httpGet.setHeader("Connection", REQUEST_HEADER_CONNECTION);
                httpGet.setHeader("Cookie", cookie);

                httpResponse = httpClient.execute(httpGet);}// POST 请求elseif(POST.equals(requestMethod)){
                httpPost =newHttpPost(url);
                httpPost.setHeader("Connection", REQUEST_HEADER_CONNECTION);
                httpPost.setHeader("Cookie", cookie);//有参数进来if(params !=null&& params.length >0){
                    httpPost.setEntity(newUrlEncodedFormEntity(Arrays.asList(params),"UTF-8"));}

                httpResponse = httpClient.execute(httpPost);}HttpEntity httpEntity = httpResponse.getEntity();
            result =EntityUtils.toString(httpEntity);}catch(IOException e){
            e.printStackTrace();}finally{if(httpClient !=null){try{
                    httpClient.close();}catch(IOException e){
                    e.printStackTrace();}}}return result;}/**
     * 创建 ROW POST 请求
     *
     * @param url           请求地址
     * @param jsonBodyParam 请求参数 (JSON 格式)
     * @param useProxy      是否需要代理
     * @param proxyHost     代理地址
     * @param proxyPort     代理端口
     * @return
     */privatestaticStringcreateRequestForRow(String url,String jsonBodyParam,boolean useProxy,String proxyHost,int proxyPort){//创建 HTTPClient 客户端//        CloseableHttpClient httpClient = HttpClients.createDefault();CloseableHttpClient httpClient =getHttpClient(useProxy, proxyHost, proxyPort);String result =null;try{//请求结果
            result =null;//请求方式HttpPost httpPost =null;//响应CloseableHttpResponse httpResponse =null;

            httpPost =newHttpPost(url);
            httpPost.setHeader("Connection", REQUEST_HEADER_CONNECTION);
            httpPost.setHeader("Content-Type", REQUEST_HEADER_CONTENTTYPE);
            httpPost.setHeader("consumes", REQUEST_HEADER_CONSUMES);

            httpPost.setEntity(newStringEntity(jsonBodyParam,"UTF-8"));

            httpResponse = httpClient.execute(httpPost);HttpEntity httpEntity = httpResponse.getEntity();
            result =EntityUtils.toString(httpEntity);}catch(IOException e){
            e.printStackTrace();}finally{if(httpClient !=null){try{
                    httpClient.close();}catch(IOException e){
                    e.printStackTrace();}}}return result;}/**
     * 创建 HttpClient 客户端
     *
     * @param useProxy  是否代理
     * @param proxyHost 代理地址
     * @param proxyPort 代理端口
     * @return
     */privatestaticCloseableHttpClientgetHttpClient(boolean useProxy,String proxyHost,int proxyPort){CloseableHttpClient httpClient =null;if(useProxy){//设置代理IP、端口、协议HttpHost proxy =newHttpHost(proxyHost, proxyPort,"http");//把代理设置到请求配置//setConnectTimeout:设置连接超时时间,单位毫秒。//setConnectionRequestTimeout:设置从connect Manager获取Connection 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。//setSocketTimeout:请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用RequestConfig defaultRequestConfig =RequestConfig.custom().setConnectTimeout(CONNECTTIMEOUT).setSocketTimeout(SOCKETTIMEOUT).setConnectionRequestTimeout(CONNECTIONREQUESTTIMEOUT).setProxy(proxy).build();//创建 HTTPClient 客户端
            httpClient =HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();}else{//setConnectTimeout:设置连接超时时间,单位毫秒。//setConnectionRequestTimeout:设置从connect Manager获取Connection 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。//setSocketTimeout:请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用RequestConfig defaultRequestConfig =RequestConfig.custom().setConnectTimeout(CONNECTTIMEOUT).setSocketTimeout(SOCKETTIMEOUT).setConnectionRequestTimeout(CONNECTIONREQUESTTIMEOUT).build();//创建 HTTPClient 客户端
            httpClient =HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();}return httpClient;}/**
     * MAP类型数组转换成BasicNameValuePair类型
     *
     * @param properties MAP类型数组
     * @return BasicNameValuePair类型数组
     */privatestaticBasicNameValuePair[]generatNameValuePair(Map<String,String> properties){BasicNameValuePair[] basicNameValuePairs =newBasicNameValuePair[properties.size()];int i =0;for(Map.Entry<String,String> entry : properties.entrySet()){
            basicNameValuePairs[i++]=newBasicNameValuePair(entry.getKey(), entry.getValue());}return basicNameValuePairs;}}

创建好后,目录结构效果如下:
在这里插入图片描述

四、创建项目服务模块

4.1、创建项目服务模块工程

创建一个名称为

my-project-server

的项目,该项目负责实现具体业务,以及提供服务。

pom.xml

内容如下:

<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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><!-- Project Begin --><dependency><groupId>com.micromaple</groupId><artifactId>my-project-common</artifactId><version>1.0.0-SNAPSHOT</version></dependency><!-- Project End --><!-- Spring Boot Begin --><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><!-- Spring Boot End --><!-- Junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency><!-- Environment Begin --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- Environment End --><!-- Common Begin --><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId></dependency><!-- Common End --></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+8
server:port:8899

4.4、创建 Application

这里使用 Spring 提供的 MyBatis 包扫面注解

packagecom.micromaple.my.project.server;importorg.mybatis.spring.annotation.MapperScan;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@MapperScan(value ="com.micromaple.my.project.server.mapper")publicclassMyProjectServerApplication{publicstaticvoidmain(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

,内容如下:

<?xml version="1.0" encoding="UTF-8"?><configuration><substitutionPropertyname="logBase"value="./logs/"/><substitutionPropertyname="logName"value="my-project-server"/><substitutionPropertyname="logPatternExp"value="%date{YYYY-MM-dd HH:mm:ss.SSS} [%-5level] [%thread] %logger{9999} %L -=> %msg%n"/><appendername="console"class="ch.qos.logback.core.ConsoleAppender"><encoder><pattern>${logPatternExp}</pattern></encoder></appender><appendername="rollingFile"class="ch.qos.logback.core.rolling.RollingFileAppender"><file>${logBase}${logName}.log</file><rollingPolicyclass="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><fileNamePattern>${logBase}${logName}-%d{yyyy-MM-dd}-%i.zip</fileNamePattern><maxHistory>180</maxHistory><timeBasedFileNamingAndTriggeringPolicyclass="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"><maxFileSize>30MB</maxFileSize></timeBasedFileNamingAndTriggeringPolicy></rollingPolicy><encoder><pattern>${logPatternExp}</pattern></encoder></appender><!-- project default level --><loggername="com.micromaple.my.project.server"level="DEBUG"/><loggername="org.springframework"level="ERROR"/><!-- root --><rootlevel="DEBUG"><appender-refref="console"/><appender-refref="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%2B8username: root
    password:123456druid:#连接池配置 初始化连接池的连接数量 大小,最小,最大initial-size:5min-idle:5max-active:20#配置获取连接等待超时的时间max-wait:60000#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒time-between-eviction-runs-millis:60000# 配置一个连接在池中最小生存的时间,单位是毫秒min-evictable-idle-time-millis:30000validation-query: SELECT 1 FROM DUAL
      test-while-idle:truetest-on-borrow:truetest-on-return:false# 是否缓存preparedStatement,也就是PSCache  官方建议MySQL下建议关闭   个人建议如果想用SQL防火墙 建议打开pool-prepared-statements:truemax-pool-prepared-statement-per-connection-size:20# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙filter:stat:merge-sql:trueslow-sql-millis:5000#3.基础监控配置web-stat-filter:enabled:trueurl-pattern: /*
        #设置不统计哪些URLexclusions:"*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"session-stat-enable:truesession-stat-max-count:100stat-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: druid
server:port:8899logging:config: classpath:logback-spring.xml

最终项目效果图如下:
在这里插入图片描述

五、创建代码生成模块

5.1、创建代码生成模块工程

创建一个名称为

my-project-generator

的项目,该项目负责通过数据库自动生成通用的增删改查。

pom.xml

内容如下:

<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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><!-- Project Begin --><dependency><groupId>com.micromaple</groupId><artifactId>my-project-common</artifactId><version>1.0.0-SNAPSHOT</version></dependency><!-- Project End --><!-- Spring Boot Begin --><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><!-- Spring Boot End --><!-- Junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency><!-- Environment Begin --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency><!-- Environment End --><!-- MP  代码生成工具需要的依赖1 velocity-engine-core 2 slf4j-api   3slf4j-log4j12 --><!-- Apache velocity --><dependency><groupId>org.apache.velocity</groupId><artifactId>velocity-engine-core</artifactId></dependency><!--freemarker模板--><dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId></dependency><!-- Common End --><!--mybatis-plus代码生成器--><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

创建代码生成类,内容如下:

packagecom.micromaple.my.project.generator;importcom.baomidou.mybatisplus.generator.FastAutoGenerator;importcom.baomidou.mybatisplus.generator.config.DataSourceConfig;importcom.baomidou.mybatisplus.generator.config.OutputFile;importcom.baomidou.mybatisplus.generator.config.StrategyConfig;importcom.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;importcom.baomidou.mybatisplus.generator.config.querys.MySqlQuery;importcom.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;importcom.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;importjava.util.Collections;/**
 * 代码生成
 * Title: GeneratorApplication
 * Description:
 *
 * @author Micromaple
 */publicclassGeneratorApplication{publicstaticvoidmain(String[] args){String projectPath =System.getProperty("user.dir");String finalProjectPath = projectPath +"/my-project-generator";// 数据源配置DataSourceConfig.Builder dataSourceConfig =newDataSourceConfig
                .Builder("jdbc:mysql://192.168.110.135:3306/my-project?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8","root","123456").dbQuery(newMySqlQuery()).typeConvert(newMySqlTypeConvert()).keyWordsHandler(newMySqlKeyWordsHandler());newStrategyConfig.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(newFreemarkerTemplateEngine())// 使用Freemarker引擎模板,默认的是Velocity引擎模板.execute();}}

5.4、测试代码生成

建表语句:

CREATETABLE`sys_user`(`id`intNOTNULLAUTO_INCREMENTCOMMENT'主键',`code`varchar(100)COLLATE utf8mb4_general_ci DEFAULTNULLCOMMENT'编号',`name`varchar(50)COLLATE utf8mb4_general_ci DEFAULTNULLCOMMENT'姓名',`sex`varchar(2)COLLATE utf8mb4_general_ci DEFAULTNULLCOMMENT'性别',`age`intDEFAULTNULLCOMMENT'年龄',`birthday`dateDEFAULTNULLCOMMENT'生日',`mail`varchar(50)COLLATE utf8mb4_general_ci DEFAULTNULLCOMMENT'邮箱',`created_date`datetimeDEFAULTCURRENT_TIMESTAMPCOMMENT'创建时间',`updated_date`datetimeDEFAULTNULLCOMMENT'更新时间',PRIMARYKEY(`id`))ENGINE=InnoDBAUTO_INCREMENT=2DEFAULTCHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='用户表'

新增数据:

insertinto`sys_user`(`code`,`name`,`sex`,`age`,`birthday`,`mail`,`created_date`,`updated_date`)values('admin','超级管理员','男','18','2004-01-01','[email protected]','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

,内容如下:

packagecom.micromaple.my.project.server.service;importcom.micromaple.my.project.server.MyProjectServerApplication;importcom.micromaple.my.project.server.domain.SysUser;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.context.junit4.SpringRunner;importjava.util.List;@SpringBootTest(classes =MyProjectServerApplication.class)@RunWith(SpringRunner.class)publicclassSysUserServiceTest{@AutowiredprivateISysUserService sysUserService;/**
     * 测试新增
     */@TestpublicvoidaddTest(){SysUser sysUser =newSysUser();
        sysUser.setCode("user");
        sysUser.setName("用户");
        sysUser.setSex("女");
        sysUser.setAge(18);boolean save = sysUserService.save(sysUser);System.out.println(save);// 输出 true 代表插入成功}/**
     * 测试查询
     */@TestpublicvoidselectTest(){List<SysUser> sysUsers = sysUserService.getBaseMapper().selectList(null);for(SysUser sysUser : sysUsers){System.out.println(sysUser.getName());}/**
         * 输出内容如下:
         * 超级管理员
         * 用户
         */}}

5.6、测试 Controller 层

SysUserController 代码如下:

packagecom.micromaple.my.project.server.controller;importcom.micromaple.my.project.common.dto.BaseResult;importcom.micromaple.my.project.common.utils.MapperUtils;importcom.micromaple.my.project.server.domain.SysUser;importcom.micromaple.my.project.server.service.ISysUserService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;importjava.util.List;/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Micromaple
 * @since 2022-09-21 21:51:15
 */@RestController@RequestMapping("/sys-user")publicclassSysUserController{@AutowiredprivateISysUserService sysUserService;@GetMapping("/get/all")publicBaseResultgetAll(){List<SysUser> sysUsers = sysUserService.getBaseMapper().selectList(null);returnBaseResult.success(sysUsers);}}

启动

MyProjectServerApplication

服务,访问地址:

http://localhost:8899/sys-user/get/all

效果如下:
在这里插入图片描述

六、源码获取

源码在

GitHub

码云

,持续更新中,别忘了

start

喔~

GitHub

https://github.com/micromaples/my-project

码云gitee

https://gitee.com/micromaple/my-project

读到这里,想必你已经知道了如何搭建一个Spring Boot项目,并整合其他技术栈,快跟着一起动手吧。别忘了三连支持博主呀~

我是微枫Micromaple,期待你的关注~💪💪💪


本文转载自: https://blog.csdn.net/qq_41779565/article/details/126982775
版权归原作者 微枫Micromaple 所有, 如有侵权,请联系我们删除。

“从0到1项目搭建-框架搭建(附源码)”的评论:

还没有评论