SpringBoot 集成FluentMyBatis 框架之完善

Posted 在奋斗的大道

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot 集成FluentMyBatis 框架之完善相关的知识,希望对你有一定的参考价值。

本文基于上一篇:SpringBoot 集成FluentMybatis 实现简单增删改查功能

 SpringBoot 集成FluentMyBatis 框架之完善主要添加:alibaba druid 数据库连接池接口文档swagger3

pom.xml 文件添加alibab driud 和swagger3依赖

<!--druid 数据库连接池 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>1.2.3</version>
		</dependency>
		<!-- swagger3 接口文档生成器 -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-boot-starter</artifactId>
			<version>3.0.0</version>
			<exclusions>
				<exclusion>
					<groupId>org.springframework.plugin</groupId>
					<artifactId>spring-plugin-core</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.springframework.plugin</groupId>
					<artifactId>spring-plugin-metadata</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.plugin</groupId>
			<artifactId>spring-plugin-core</artifactId>
			<version>2.0.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.plugin</groupId>
			<artifactId>spring-plugin-metadata</artifactId>
			<version>2.0.0.RELEASE</version>
		</dependency>

application.properties 添加alibaba druid 数据库连接池配置和swagger3 配置

# 指定服务端口
server.port=9092
# 指定日志文件配置
logging.config=classpath:logback.xml
# MyBatis mysql8 配置
spring.datasource.druid.url=jdbc:mysql://192.168.1.73:3306/banan_test?serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true&allowMultiQueries=true
spring.datasource.druid.username=root
spring.datasource.druid.password=digipower
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
# druid 数据库连接池配置
# 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
spring.datasource.druid.initialSize=10
spring.datasource.druid.minIdle=10
spring.datasource.druid.maxActive=200
# 配置获取连接等待超时的时间
spring.datasource.druid.maxWait=6000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.druid.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.druid.minEvictableIdleTimeMillis=100000
spring.datasource.druid.validationQuery=SELECT 1 FROM DUAL
spring.datasource.druid.testWhileIdle=true
spring.datasource.druid.testOnBorrow=false
spring.datasource.druid.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.druid.poolPreparedStatements=true
spring.datasource.druid.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.druid.filters=stat,wall,slf4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.druid.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

# 生产环境需设置为false
springfox.documentation.swagger-ui.enabled=true

Config 包添加Swagger3Config 配置对象

package com.single.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;

import io.swagger.v3.oas.annotations.Operation;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Response;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
@EnableOpenApi
public class Swagger3Config {
	 @Bean
	    public Docket createRestApi() {
	        //返回文档摘要信息
	        return new Docket(DocumentationType.OAS_30)
	                .apiInfo(apiInfo())
	                .select()
	                .apis(RequestHandlerSelectors.withMethodAnnotation(Operation.class))
	                .paths(PathSelectors.any())
	                .build()
	                .globalResponses(HttpMethod.GET, getGlobalResponseMessage())
	                .globalResponses(HttpMethod.POST, getGlobalResponseMessage());
	    }

	    /**
	     * 生成接口信息,包括标题、联系人等
	     */
	    private ApiInfo apiInfo() {
	        return new ApiInfoBuilder()
	                .title("Swagger3接口文档")
	                .description("如有疑问,可联系《在奋斗的大道上》")
	                .contact(new Contact("在奋斗的大道上", "https://blog.csdn.net/zhouzhiwengang", ""))
	                .version("1.0")
	                .build();
	    }

	    

	    /**
	     * 封装通用响应信息
	     */
	    private List<Response> getGlobalResponseMessage() {
	        List<Response> responseList = new ArrayList<>();
	        responseList.add(new ResponseBuilder().code("404").description("未找到资源").build());
	        return responseList;
	    }

}

Controller层添加参数注解说明

package com.single.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.single.entity.UcasAuthUserEntity;
import com.single.entity.mapper.UcasAuthUserMapper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 
 * @ClassName: UserController
 * @Description: 用户管理
 */
@Controller
@RequestMapping("/api/user")
@Api(tags = "用户管理")
public class UserController {
	@Resource
	UcasAuthUserMapper userMapper;

	/**
	 * 根据ID查询数据1
	 * 
	 * @param id
	 * @return
	 */
	@Operation(summary = "主键查询")
	@RequestMapping(value = "/getByIdOne", method = RequestMethod.GET)
	@ResponseBody
	public UcasAuthUserEntity getByIdOne(
			@Parameter(description = "用户Sid,字符串") @RequestParam(value = "sid", required = true, defaultValue = "1") String sid) {
		return userMapper.findById(sid);
	}

	/**
	 * 根据ID删除
	 * 
	 * @param id
	 */

	@Operation(summary = "主键删除")
	@RequestMapping(value = "/deleteById", method = RequestMethod.GET)
	@ResponseBody
	public void deleteById(@Parameter(description = "用户Sid,字符串") @RequestParam(value = "sid", required = true, defaultValue = "1") String sid) {
		userMapper.deleteById(sid);
	}
	
	 /*
    @ApiImplicitParams: 用在请求的方法上,说明方法的用途,作用
        @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
            name:参数名
            value:参数的汉字说明,解释
            required:参数是否必须传
            paramType:参数放在哪个地方
                · header --> 请求参数的获取:@RequestHeader
                · query --> 请求参数的获取:@RequestParam
                · path(用于restful接口)--> 请求参数的获取:@PathVariable
                · body(不常用)
                · form(不常用)
            dataType:参数类型,默认String,其他值dataType="Integer"
            defaultValue:参数的默认值
     */


	/**
	 * 根据ID进行更新
	 * 
	 * @param userEntity
	 * @return
	 */
	@Operation(summary = "主键更新")
	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	@ResponseBody
	 @ApiImplicitParams({
         @ApiImplicitParam(name="sid",value="主键",dataTypeClass = String.class),
         @ApiImplicitParam(name="userName",value="用户姓名",dataTypeClass = String.class)
	 })
	public UcasAuthUserEntity updateById(@RequestBody UcasAuthUserEntity userEntity) {
		int b = userMapper.updateById(userEntity);
		if (b > 0) {
			return userMapper.findById(userEntity.getSid());
		}
		return null;
	}

	/**
	 * 新增
	 * 
	 * @param userEntity
	 * @return
	 */
	@Operation(summary = "新增/更新")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	@ResponseBody
	 @ApiImplicitParams({
         @ApiImplicitParam(name="sid",value="主键",dataTypeClass = String.class),
         @ApiImplicitParam(name="userName",value="用户姓名",dataTypeClass = String.class)
	 })
	public String insert(@RequestBody UcasAuthUserEntity userEntity) {
		boolean target = userMapper.saveOrUpdate(userEntity);
		if (target) {
			return "success";
		}
		return "error";
	}

}

打开Swagger3页面:http://127.0.0.1:9092/swagger-ui/index.html

点开我们刚刚写好的接口进行测试

 

 

 

以上是关于SpringBoot 集成FluentMyBatis 框架之完善的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot集成Kafka

SpringBoot.03.SpringBoot集成jsp

SpringBoot.03.SpringBoot集成jsp

SpringBoot.03.SpringBoot集成jsp

SpringBoot使用·上篇(SpringBoot注解+集成JDBC+集成druid(德鲁伊)+sql监控)

SpringBoot使用·上篇(SpringBoot注解+集成JDBC+集成druid(德鲁伊)+sql监控)