Java Fluent Mybatis 項(xiàng)目工程化與常規(guī)操作詳解流程篇 上
前言
接著上一篇,上篇已經(jīng)測(cè)試通過(guò),成功添加了數(shù)據(jù)。那么這篇主要是繼續(xù)上一個(gè)項(xiàng)目,將項(xiàng)目進(jìn)行工程化包裝,增加一些必要配置,并且生成增刪改查接口。
GitHub代碼倉(cāng)庫(kù):GitHub倉(cāng)庫(kù)
Maven依賴(lài)
增加了druid數(shù)據(jù)庫(kù)連接池,所以之前的配置文件也需要調(diào)整,下面會(huì)發(fā)出來(lái)。
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.5.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>3.0.2</version> </dependency>
配置文件調(diào)整
原來(lái)的application.properties就不用了,換成yml,看得清楚一點(diǎn)。
server: port: 8080 spring: application: name: fluent datasource: druid: db-type: mysql driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://192.168.0.108:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&verifyServerCertificate=false&allowMultiQueries=true&serverTimezone=GMT%2b8 username: root password: 123456 # 使用druid數(shù)據(jù)源 # 下面為連接池的補(bǔ)充設(shè)置,應(yīng)用到上面所有數(shù)據(jù)源中 # 初始化大小,最小,最大 initialSize: 10 minIdle: 10 maxActive: 200 # 配置獲取連接等待超時(shí)的時(shí)間 maxWait: 6000 # 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒 timeBetweenEvictionRunsMillis: 60000 # 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒 minEvictableIdleTimeMillis: 100000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false # 打開(kāi)PSCache,并且指定每個(gè)連接上PSCache的大小 poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 # 配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sql無(wú)法統(tǒng)計(jì),'wall'用于防火墻 filters: stat,wall,slf4j # 通過(guò)connectProperties屬性來(lái)打開(kāi)mergeSql功能;慢SQL記錄 connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # 合并多個(gè)DruidDataSource的監(jiān)控?cái)?shù)據(jù) #useGlobalDataSourceStat: true
Knife4j配置
這部分配置主要是為了后面調(diào)試接口方便。
上代碼
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j; import com.hy.fmp.dto.Result; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.oas.annotations.EnableOpenApi; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger.web.UiConfiguration; import springfox.documentation.swagger.web.UiConfigurationBuilder; import java.util.HashMap; @EnableOpenApi @Configuration @EnableKnife4j @Import(BeanValidatorPluginsConfiguration.class) public class SwaggerConfig { @Value("${knife4j.enable:true}") private boolean enable; @Bean UiConfiguration uiConfig() { return UiConfigurationBuilder.builder().build(); } @Bean public Docket nlpRestApi(ServerProperties serverProperties) { return new Docket(DocumentationType.OAS_30) .enable(enable) .apiInfo(apiInfo("FluentMybatis測(cè)試服務(wù)接口")) .pathMapping(serverProperties.getServlet().getContextPath()) .groupName("FluentMybatis測(cè)試") .select() .apis(RequestHandlerSelectors.basePackage("com.hy.fmp.ctrl")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo(String title) { return new ApiInfoBuilder() // 標(biāo)題 .title(title) // 描述 .description("所有的接口響應(yīng)在現(xiàn)有接口定義外包裝了一層標(biāo)準(zhǔn)結(jié)構(gòu):" + Result.ok(new HashMap<>(1))) // 作者信息 .contact(new Contact("劍客阿良ALiang", "", "3614322595@qq.com")) // 版本號(hào) .version("1.0.0") .build(); } }
添加必要實(shí)體
增加control層接口返回實(shí)體以及錯(cuò)誤碼枚舉類(lèi)。
結(jié)果實(shí)體
package com.hy.fmp.dto; import cn.hutool.json.JSONUtil; import lombok.AccessLevel; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor(access = AccessLevel.PRIVATE) public class Result<T> { private String code = "0"; private String message = "請(qǐng)求成功"; private boolean success = true; private T data; public Result(String code, String message, boolean success) { this.code = code; this.message = message; this.success = success; } public Result(String code, String message, boolean success, T data) { this.code = code; this.message = message; this.success = success; this.data = data; } public Result(T data) { this.data = data; } /** * 針對(duì)異常返回響應(yīng)體 * * @param success 是否成功 * @param code 錯(cuò)誤碼 * @param message 錯(cuò)誤信息 */ public Result(boolean success, String code, String message) { this.success = success; this.code = code; this.message = message; } public static <T> Result<T> ok(T obj) { return new Result<>(obj); } public static <T> Result<T> ok() { return new Result<>(); } public static <T> Result<T> error(String code, String message) { return new Result<>(code, message, false); } public static <T> Result<T> error(String code, String message, T data) { return new Result<>(code, message, false, data); } public Result<T> setMsg(String message) { this.message = message; return this; } @Override public String toString() { return JSONUtil.toJsonStr(this); } }
錯(cuò)誤碼枚舉
package com.hy.fmp.enm; /** @Author huyi @Date 2021/10/20 17:19 @Description: 報(bào)錯(cuò)code */ public enum ErrorCode { /** 錯(cuò)誤code */ BASE_ERROR_CODE("10001"), ; private final String code; ErrorCode(String code) { this.code = code; } public String getCode() { return code; } }
增/改
現(xiàn)在開(kāi)始增加和修改表接口編寫(xiě),一般來(lái)說(shuō),新增和修改的方法都定義為同一個(gè)。邏輯為如果傳遞數(shù)據(jù)主鍵,則為改,如果不傳遞,則為增。
定義接口
package com.hy.fmp.service; import com.hy.fmp.fluent.entity.TestFluentMybatisEntity; /** @Author huyi @Date 2021/10/20 17:10 @Description: 基礎(chǔ)操作接口 */ public interface IBaseService { /** * 新增/修改接口 * * @param param 表實(shí)體 * @return 表實(shí)體 */ TestFluentMybatisEntity insertOrUpdate(TestFluentMybatisEntity param); }
接口實(shí)現(xiàn)類(lèi)
package com.hy.fmp.service.Impl; import com.hy.fmp.fluent.dao.intf.TestFluentMybatisDao; import com.hy.fmp.fluent.entity.TestFluentMybatisEntity; import com.hy.fmp.service.IBaseService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** @Author huyi @Date 2021/10/20 17:10 @Description: 基礎(chǔ)操作接口實(shí)現(xiàn) */ @Slf4j @Service public class BaseServiceImpl implements IBaseService { @Autowired private TestFluentMybatisDao testFluentMybatisDao; @Override public TestFluentMybatisEntity insertOrUpdate(TestFluentMybatisEntity param) { testFluentMybatisDao.saveOrUpdate(param); return param; } }
編寫(xiě)control層
package com.hy.fmp.ctrl; import com.hy.fmp.dto.Result; import com.hy.fmp.enm.ErrorCode; import com.hy.fmp.fluent.entity.TestFluentMybatisEntity; import com.hy.fmp.service.IBaseService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; /** @Author huyi @Date 2021/10/20 17:04 @Description: 基礎(chǔ)操作 */ @Slf4j @RestController @RequestMapping("/base") @Api(tags = "基礎(chǔ)操作") public class BaseController { @Autowired private IBaseService baseService; @ApiOperation(value = "插入/更新數(shù)據(jù)", notes = "插入/更新數(shù)據(jù)") @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST) @ResponseBody public Result<TestFluentMybatisEntity> insertOrUpdate(@RequestBody TestFluentMybatisEntity param) { try { return Result.ok(baseService.insertOrUpdate(param)); } catch (Exception exception) { return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null); } } }
啟動(dòng)項(xiàng)目
打開(kāi)Knife4j頁(yè)面:http://localhost:8080/doc.html#/home
點(diǎn)開(kāi)我們剛剛寫(xiě)好的接口進(jìn)行測(cè)試。
OK,插入成功。
修改數(shù)據(jù)
OK,修改成功。
總結(jié)
下一篇繼續(xù),地址:Java Fluent Mybatis 項(xiàng)目工程化與常規(guī)操作詳解流程篇 下
如果本文對(duì)你有幫助,請(qǐng)點(diǎn)個(gè)贊支持一下吧。
到此這篇關(guān)于Java Fluent Mybatis 項(xiàng)目工程化與常規(guī)操作詳解流程篇 上的文章就介紹到這了,更多相關(guān)Java Fluent Mybatis內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java Fluent Mybatis實(shí)戰(zhàn)之構(gòu)建項(xiàng)目與代碼生成篇上
- Java Fluent Mybatis實(shí)戰(zhàn)之構(gòu)建項(xiàng)目與代碼生成篇下
- Fluent Mybatis實(shí)現(xiàn)環(huán)境隔離和租戶(hù)隔離
- Fluent Mybatis 批量更新的使用
- springboot 整合fluent mybatis的過(guò)程,看這篇夠了
- Fluent MyBatis實(shí)現(xiàn)動(dòng)態(tài)SQL
- Fluent Mybatis快速入門(mén)詳細(xì)教程
- Fluent Mybatis零xml配置實(shí)現(xiàn)復(fù)雜嵌套查詢(xún)
- Fluent Mybatis如何做到代碼邏輯和sql邏輯的合一
相關(guān)文章
Springboot使用@Valid 和AOP做參數(shù)校驗(yàn)及日志輸出問(wèn)題
這篇文章主要介紹的Springboot使用@Valid 和AOP做參數(shù)校驗(yàn)及日志輸出問(wèn)題,本文通過(guò)代碼講解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11Springboot與vue實(shí)現(xiàn)文件導(dǎo)入方法具體介紹
文件導(dǎo)入時(shí)大多數(shù)項(xiàng)目無(wú)法回避的問(wèn)題,這兩天深入學(xué)習(xí)了文件導(dǎo)入,在這里進(jìn)行記錄,使用到的技術(shù)是Springboot+Vue,前端組件使用el-upload2023-02-02Java中Lambda表達(dá)式之Lambda語(yǔ)法與作用域解析
這篇文章主要介紹了Java中Lambda表達(dá)式之Lambda語(yǔ)法與作用域解析重點(diǎn)介紹Lambda表達(dá)式基礎(chǔ)知識(shí),需要的朋友可以參考下2017-02-02如何通過(guò)Java生成一個(gè)隨機(jī)數(shù)
當(dāng)我們需要在Java中生成隨機(jī)數(shù)時(shí),可以借助JDK中提供的Random類(lèi)來(lái)實(shí)現(xiàn),通過(guò)使用Random類(lèi),我們可以輕松地生成各種類(lèi)型的隨機(jī)數(shù),下面我們就來(lái)看看如何利用Random類(lèi)生成隨機(jī)數(shù)吧2023-09-09LRU算法及Apache?LRUMap源碼實(shí)例解析
這篇文章主要給大家介紹了關(guān)于LRU算法及Apache?LRUMap源碼解析的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-11-11Spring Bean 依賴(lài)注入常見(jiàn)錯(cuò)誤問(wèn)題
這篇文章主要介紹了Spring Bean 依賴(lài)注入常見(jiàn)錯(cuò)誤問(wèn)題,文中提到value的工作大體分為三個(gè)核心步驟,具體內(nèi)容詳情跟隨小編一起看看吧2021-09-09詳解Kotlin中如何實(shí)現(xiàn)類(lèi)似Java或C#中的靜態(tài)方法
Kotlin中如何實(shí)現(xiàn)類(lèi)似Java或C#中的靜態(tài)方法,本文總結(jié)了幾種方法,分別是:包級(jí)函數(shù)、伴生對(duì)象、擴(kuò)展函數(shù)和對(duì)象聲明。這需要大家根據(jù)不同的情況進(jìn)行選擇。2017-05-05詳解springmvc控制登錄用戶(hù)session失效后跳轉(zhuǎn)登錄頁(yè)面
本篇文章主要介紹了springmvc控制登錄用戶(hù)session失效后跳轉(zhuǎn)登錄頁(yè)面,session一旦失效就需要重新登陸,有興趣的同學(xué)可以了解一下。2017-01-01java 將數(shù)據(jù)加載到內(nèi)存中的操作
這篇文章主要介紹了java 將數(shù)據(jù)加載到內(nèi)存中的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09