最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java Fluent Mybatis 項(xiàng)目工程化與常規(guī)操作詳解流程篇 上

 更新時(shí)間:2021年10月28日 10:24:26   作者:劍客阿良_ALiang  
Java中常用的ORM框架主要是mybatis, hibernate, JPA等框架。國(guó)內(nèi)又以Mybatis用的多,基于mybatis上的增強(qiáng)框架,又有mybatis plus和TK mybatis等。今天我們介紹一個(gè)新的mybatis增強(qiáng)框架 fluent mybatis

前言

接著上一篇,上篇已經(jīng)測(cè)試通過(guò),成功添加了數(shù)據(jù)。那么這篇主要是繼續(xù)上一個(gè)項(xiàng)目,將項(xiàng)目進(jìn)行工程化包裝,增加一些必要配置,并且生成增刪改查接口。

GitHub代碼倉(cāng)庫(kù):GitHub倉(cāng)庫(kù)

Maven依賴

增加了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ò)誤碼枚舉類。

結(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)類

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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • IntelliJ IDEA中Scala、sbt、maven配置教程

    IntelliJ IDEA中Scala、sbt、maven配置教程

    這篇文章主要介紹了IntelliJ IDEA中Scala、sbt、maven配置教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java報(bào)錯(cuò)Java.net.SocketTimeoutException的幾種解決方法

    Java報(bào)錯(cuò)Java.net.SocketTimeoutException的幾種解決方法

    在 Java 網(wǎng)絡(luò)編程中,SocketTimeoutException 通常表示在進(jìn)行網(wǎng)絡(luò)操作時(shí),等待響應(yīng)的時(shí)間超過(guò)了設(shè)定的超時(shí)時(shí)間,本文將深入探討 Java.net.SocketTimeoutException 的問(wèn)題,并為開(kāi)發(fā)者和環(huán)境配置者提供詳細(xì)的解決方案,需要的朋友可以參考下
    2024-10-10
  • Java中的Timer與TimerTask源碼及使用解析

    Java中的Timer與TimerTask源碼及使用解析

    這篇文章主要介紹了Java中的Timer與TimerTask源碼及使用解析,在Java中,經(jīng)常使用Timer來(lái)定時(shí)調(diào)度任務(wù),Timer調(diào)度任務(wù)有一次性調(diào)度和循環(huán)調(diào)度,循環(huán)調(diào)度有分為固定速率調(diào)度(fixRate)和固定時(shí)延調(diào)度(fixDelay),需要的朋友可以參考下
    2023-10-10
  • Spring Cloud 配置中心多環(huán)境配置bootstrap.yml的實(shí)現(xiàn)方法

    Spring Cloud 配置中心多環(huán)境配置bootstrap.yml的實(shí)現(xiàn)方法

    spring cloud用上了配置中心,就一個(gè)boostrap.yml,本文就來(lái)介紹一下Spring Cloud 配置中心多環(huán)境配置bootstrap.yml的實(shí)現(xiàn)方法,感興趣的可以了解一下
    2024-03-03
  • 詳解在Spring中如何自動(dòng)創(chuàng)建代理

    詳解在Spring中如何自動(dòng)創(chuàng)建代理

    這篇文章主要介紹了詳解在Spring中如何自動(dòng)創(chuàng)建代理,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • 簡(jiǎn)單談?wù)刯ava的異常處理(Try Catch Finally)

    簡(jiǎn)單談?wù)刯ava的異常處理(Try Catch Finally)

    在程序設(shè)計(jì)中,進(jìn)行異常處理是非常關(guān)鍵和重要的一部分。一個(gè)程序的異常處理框架的好壞直接影響到整個(gè)項(xiàng)目的代碼質(zhì)量以及后期維護(hù)成本和難度。
    2016-03-03
  • idea顯示springboot多服務(wù)啟動(dòng)界面service操作

    idea顯示springboot多服務(wù)啟動(dòng)界面service操作

    這篇文章主要介紹了idea顯示springboot多服務(wù)啟動(dòng)界面service操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-09-09
  • postman?如何實(shí)現(xiàn)傳遞?ArrayList?給后臺(tái)

    postman?如何實(shí)現(xiàn)傳遞?ArrayList?給后臺(tái)

    這篇文章主要介紹了postman?如何實(shí)現(xiàn)傳遞?ArrayList給后臺(tái),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • java用字節(jié)數(shù)組解決FileInputStream讀取漢字出現(xiàn)亂碼問(wèn)題

    java用字節(jié)數(shù)組解決FileInputStream讀取漢字出現(xiàn)亂碼問(wèn)題

    這篇文章主要介紹了java用字節(jié)數(shù)組解決FileInputStream讀取漢字出現(xiàn)亂碼問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • mybatis自動(dòng)建表的實(shí)現(xiàn)方法

    mybatis自動(dòng)建表的實(shí)現(xiàn)方法

    這篇文章主要介紹了mybatis自動(dòng)建表的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11

最新評(píng)論

海晏县| 福州市| 三亚市| 瑞金市| 萨嘎县| 宕昌县| 诏安县| 抚顺市| 大安市| 石林| 灵寿县| 清水河县| 醴陵市| 襄城县| 上林县| 普兰县| 大悟县| 赣州市| 太保市| 皮山县| 黄梅县| 天门市| 讷河市| 思茅市| 嘉荫县| 个旧市| 南京市| 文安县| 大埔区| 丰原市| 兴宁市| 阿拉善右旗| 榆社县| 社旗县| 莲花县| 蚌埠市| 科技| 綦江县| 察哈| 乌海市| 南陵县|