写点什么

MyBatis 初级实战之三:springboot 集成 druid,java 实用教程第五版

用户头像
极客good
关注
发布于: 刚刚

<artifactId>mybatis-spring-boot-starter</artifactId>


</dependency>


<dependency>


<groupId>mysql</groupId>


<artifactId>mysql-connector-java</artifactId>


<scope>runtime</scope>


</dependency>


<dependency>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-starter-test</artifactId>


<scope>test</scope>


<exclusions>


<exclusion>


<groupId>org.junit.vintage</groupId>


<artifactId>junit-vintage-engine</artifactId>


</exclusion>


</exclusions>


</dependency>


<dependency>


<groupId>io.springfox</groupId>


<artifactId>springfox-swagger2</artifactId>


</dependency>


<dependency>


<groupId>io.springfox</groupId>


<artifactId>springfox-swagger-ui</artifactId>


</dependency>


<dependency>


<groupId>com.alibaba</groupId>


<artifactId>druid-spring-boot-starter</artifactId>


</dependency>


<dependency>


<groupId>junit</groupId>


<artifactId>junit</artifactId>


<scope>test</scope>


</dependency>


<dependency>


<groupId>com.google.code.gson</groupId>


<artifactId>gson</artifactId>


</dependency>


</dependencies>


<build>


<plugins>


<plugin>


<groupId>org.springframework.boot</groupId>


<artifactId>spring-boot-maven-plugin</artifactId>


</plugin>


</plugins>


</build>


</project>


  1. 新建 mybatis-config.xml:


<!DOCTYPE configuration


PUBLIC "-//mybatis.org//DTD Config 3.0//EN"


"http://mybatis.org/dtd/mybatis-3-config.dtd">


<configuration>


<typeAliases>


<package name="com.bolingcavalry.druidonesource.entity"/>


</typeAliases>


</configuration>


  1. 配置 application.yml,请重点关注 druid 的配置,还请注意 stat-view-servlet,这里是 druid 监控页面的登录配置:


server:


port: 8080


spring:


#1.JDBC 数据源


datasource:


username: root


password: 123456


url: jdbc:mysql://192.168.50.43:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC


driver-class-name: com.mysql.cj.jdbc.Driver


#2.连接池配置


druid:


#初始化连接池的连接数量 大小,最小,最大


initial-size: 5


min-idle: 5


max-active: 20


#配置获取连接等待超时的时间


max-wait: 60000


#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒


time-between-eviction-runs-millis: 60000

配置一个连接在池中最小生存的时间,单位是毫秒

min-evictable-idle-time-millis: 30000

配置一个连接在池中最大生存的时间,单位是毫秒

max-evictable-idle-time-millis: 300000


validation-query: SELECT 1 FROM user


test-while-idle: true


test-on-borrow: true


test-on-return: false

是否缓存 preparedStatement,也就是 PSCache 官方建议 MySQL 下建议关闭 个人建议如果想用 SQL 防火墙 建议打开

pool-prepared-statements: true


max-pool-prepared-statement-per-connection-size: 20

配置监控统计拦截的 filters,去掉后监控界面 sql 无法统计,'wall'用于防火墙

filters: stat,wall,slf4j


filter:


stat:


merge-sql: true


slow-sql-millis: 5000


#3.基础监控配置


web-stat-filter:


enabled: true


url-pattern: /*


#设置不统计哪些 URL


exclusions: ".js,.gif,.jpg,.png,.css,.ico,/druid/*"


session-stat-enable: true


session-stat-max-count: 100


stat-view-servlet:


enabled: true


url-pattern: /druid/*


reset-enable: true


#设置监控页面的登录名和密码


login-username: admin


login-password: admin


allow: 127.0.0.1


#deny: 192.168.1.100

mybatis 配置

mybatis:

配置文件所在位置

config-location: classpath:mybatis-config.xml

映射文件所在位置

mapper-locations: classpath:mappers/*Mapper.xml

日志配置

logging:


level:


root: INFO


com:


bolingcavalry:


druidonesource:


mapper: debug


  1. 创建启动类,要带上 MapperScan 注解:


package com.bolingcavalry.druidonesource;


import org.mybatis.spring.annotation.MapperScan;


import org.springframework.boot.SpringApplication;


import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication


@MapperScan("com.bolingcavalry.druidonesource.mapper")


public class DuridOneSourceApplication {


public static void main(String[] args) {


SpringApplication.run(DuridOneSourceApplication.class, args);


}


}


  1. 创建 swagger 配置类:


package com.bolingcavalry.druidonesource;


import springfox.documentation.service.Contact;


import org.springframework.context.annotation.Bean;


import org.springframework.context.annotation.Configuration;


import springfox.documentation.builders.ApiInfoBuilder;


import springfox.documentation.builders.PathSelectors;


import springfox.documentation.builders.RequestHandlerSelectors;


import springfox.documentation.service.ApiInfo;


import springfox.documentation.service.Tag;


import springfox.documentation.spi.DocumentationType;


import spri


【一线大厂Java面试题解析+核心总结学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


ngfox.documentation.spring.web.plugins.Docket;


import springfox.documentation.swagger2.annotations.EnableSwagger2;


/**


  • @Description: swagger 配置类

  • @author: willzhao E-mail: zq2599@gmail.com

  • @date: 2020/8/11 7:54


*/


@Configuration


@EnableSwagger2


public class SwaggerConfig {


@Bean


public Docket createRestApi() {


return new Docket(DocumentationType.SWAGGER_2)


.apiInfo(apiInfo())


.tags(new Tag("UserController", "用户服务"), new Tag("LogController", "日志服务"))


.select()


// 当前包路径


.apis(RequestHandlerSelectors.basePackage("com.bolingcavalry.druidonesource.controller"))


.paths(PathSelectors.any())


.build();


}


//构建 api 文档的详细信息函数,注意这里的注解引用的是哪个


private ApiInfo apiInfo() {


return new ApiInfoBuilder()


//页面标题


.title("MyBatis CURD 操作")


//创建人


.contact(new Contact("程序员欣宸", "https://github.com/zq2599/blog_demos", "zq2599@gmail.com"))


//版本号


.version("1.0")


//描述


.description("API 描述")


.build();


}


}


  1. 创建 druid 配置类,如下,可见是通过使用 application.yml 中配置的参数对 DruidDataSource 进行配置:


package com.bolingcavalry.druidonesource;


import com.alibaba.druid.pool.DruidDataSource;


import com.alibaba.druid.support.http.StatViewServlet;


import org.slf4j.Logger;


import org.slf4j.LoggerFactory;


import org.springframework.beans.factory.annotation.Value;


import org.springframework.boot.web.servlet.ServletRegistrationBean;


import org.springframework.context.annotation.Bean;


import org.springframework.context.annotation.Configuration;


/**


  • @Description: druid 配置类

  • @author: willzhao E-mail: zq2599@gmail.com

  • @date: 2020/8/18 08:12


*/


@Configuration


public class DruidConfig {


private static final Logger logger = LoggerFactory.getLogger(DruidConfig.class);


@Value("${spring.datasource.url}")


private String dbUrl;


@Value("${spring.datasource.username}")


private String username;


@Value("${spring.datasource.password}")


private String password;


@Value("${spring.datasource.driver-class-name}")


private String driverClassName;


@Value("${spring.datasource.druid.initial-size}")


private int initialSize;


@Value("${spring.datasource.druid.max-active}")


private int maxActive;


@Value("${spring.datasource.druid.min-idle}")


private int minIdle;


@Value("${spring.datasource.druid.max-wait}")


private int maxWait;


@Value("${spring.datasource.druid.pool-prepared-statements}")


private boolean poolPreparedStatements;


@Value("${spring.datasource.druid.max-pool-prepared-statement-per-connection-size}")


private int maxPoolPreparedStatementPerConnectionSize;


@Value("${spring.datasource.druid.time-between-eviction-runs-millis}")


private int timeBetweenEvictionRunsMillis;


@Value("${spring.datasource.druid.min-evictable-idle-time-millis}")


private int minEvictableIdleTimeMillis;


@Value("${spring.datasource.druid.max-evictable-idle-time-millis}")


private int maxEvictableIdleTimeMillis;


@Value("${spring.datasource.druid.validation-query}")


private String validationQuery;


@Value("${spring.datasource.druid.test-while-idle}")


private boolean testWhileIdle;


@Value("${spring.datasource.druid.test-on-borrow}")


private boolean testOnBorrow;


@Value("${spring.datasource.druid.test-on-return}")


private boolean testOnReturn;


@Value("${spring.datasource.druid.filters}")


private String filters;


@Value("{spring.datasource.druid.connection-properties}")


private String connectionProperties;


/**

用户头像

极客good

关注

还未添加个人签名 2021.03.18 加入

还未添加个人简介

评论

发布
暂无评论
MyBatis初级实战之三:springboot集成druid,java实用教程第五版