Java Fluent Mybatis 項目工程化與常規(guī)操作詳解流程篇 上
前言
接著上一篇,上篇已經(jīng)測試通過,成功添加了數(shù)據(jù)。那么這篇主要是繼續(xù)上一個項目,將項目進行工程化包裝,增加一些必要配置,并且生成增刪改查接口。
GitHub代碼倉庫:GitHub倉庫
Maven依賴
增加了druid數(shù)據(jù)庫連接池,所以之前的配置文件也需要調(diào)整,下面會發(fā)出來。
<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)整
原來的application.properties就不用了,換成yml,看得清楚一點。
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ù)源
# 下面為連接池的補充設(shè)置,應(yīng)用到上面所有數(shù)據(jù)源中
# 初始化大小,最小,最大
initialSize: 10
minIdle: 10
maxActive: 200
# 配置獲取連接等待超時的時間
maxWait: 6000
# 配置間隔多久才進行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一個連接在池中最小生存的時間,單位是毫秒
minEvictableIdleTimeMillis: 100000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打開PSCache,并且指定每個連接上PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# 配置監(jiān)控統(tǒng)計攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計,'wall'用于防火墻
filters: stat,wall,slf4j
# 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多個DruidDataSource的監(jiān)控數(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測試服務(wù)接口"))
.pathMapping(serverProperties.getServlet().getContextPath())
.groupName("FluentMybatis測試")
.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"))
// 版本號
.version("1.0.0")
.build();
}
}
添加必要實體
增加control層接口返回實體以及錯誤碼枚舉類。
結(jié)果實體
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 = "請求成功";
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;
}
/**
* 針對異常返回響應(yīng)體
*
* @param success 是否成功
* @param code 錯誤碼
* @param message 錯誤信息
*/
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);
}
}
錯誤碼枚舉
package com.hy.fmp.enm;
/** @Author huyi @Date 2021/10/20 17:19 @Description: 報錯code */
public enum ErrorCode {
/** 錯誤code */
BASE_ERROR_CODE("10001"),
;
private final String code;
ErrorCode(String code) {
this.code = code;
}
public String getCode() {
return code;
}
}
增/改
現(xiàn)在開始增加和修改表接口編寫,一般來說,新增和修改的方法都定義為同一個。邏輯為如果傳遞數(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 表實體
* @return 表實體
*/
TestFluentMybatisEntity insertOrUpdate(TestFluentMybatisEntity param);
}
接口實現(xiàn)類
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ǔ)操作接口實現(xiàn) */
@Slf4j
@Service
public class BaseServiceImpl implements IBaseService {
@Autowired private TestFluentMybatisDao testFluentMybatisDao;
@Override
public TestFluentMybatisEntity insertOrUpdate(TestFluentMybatisEntity param) {
testFluentMybatisDao.saveOrUpdate(param);
return param;
}
}
編寫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);
}
}
}
啟動項目

打開Knife4j頁面:http://localhost:8080/doc.html#/home
點開我們剛剛寫好的接口進行測試。


OK,插入成功。
修改數(shù)據(jù)


OK,修改成功。
總結(jié)
下一篇繼續(xù),地址:Java Fluent Mybatis 項目工程化與常規(guī)操作詳解流程篇 下
如果本文對你有幫助,請點個贊支持一下吧。

到此這篇關(guān)于Java Fluent Mybatis 項目工程化與常規(guī)操作詳解流程篇 上的文章就介紹到這了,更多相關(guān)Java Fluent Mybatis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java Fluent Mybatis實戰(zhàn)之構(gòu)建項目與代碼生成篇上
- Java Fluent Mybatis實戰(zhàn)之構(gòu)建項目與代碼生成篇下
- Fluent Mybatis實現(xiàn)環(huán)境隔離和租戶隔離
- Fluent Mybatis 批量更新的使用
- springboot 整合fluent mybatis的過程,看這篇夠了
- Fluent MyBatis實現(xiàn)動態(tài)SQL
- Fluent Mybatis快速入門詳細(xì)教程
- Fluent Mybatis零xml配置實現(xiàn)復(fù)雜嵌套查詢
- Fluent Mybatis如何做到代碼邏輯和sql邏輯的合一
相關(guān)文章
Springboot使用@Valid 和AOP做參數(shù)校驗及日志輸出問題
這篇文章主要介紹的Springboot使用@Valid 和AOP做參數(shù)校驗及日志輸出問題,本文通過代碼講解的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11
Springboot與vue實現(xiàn)文件導(dǎo)入方法具體介紹
文件導(dǎo)入時大多數(shù)項目無法回避的問題,這兩天深入學(xué)習(xí)了文件導(dǎo)入,在這里進行記錄,使用到的技術(shù)是Springboot+Vue,前端組件使用el-upload2023-02-02
詳解Kotlin中如何實現(xiàn)類似Java或C#中的靜態(tài)方法
Kotlin中如何實現(xiàn)類似Java或C#中的靜態(tài)方法,本文總結(jié)了幾種方法,分別是:包級函數(shù)、伴生對象、擴展函數(shù)和對象聲明。這需要大家根據(jù)不同的情況進行選擇。2017-05-05
詳解springmvc控制登錄用戶session失效后跳轉(zhuǎn)登錄頁面
本篇文章主要介紹了springmvc控制登錄用戶session失效后跳轉(zhuǎn)登錄頁面,session一旦失效就需要重新登陸,有興趣的同學(xué)可以了解一下。2017-01-01
java 將數(shù)據(jù)加載到內(nèi)存中的操作
這篇文章主要介紹了java 將數(shù)據(jù)加載到內(nèi)存中的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09

