1. 新建SpringBoot项目

这里使用 IntelliJ IDEA 2022.1.2 演示
个人认为IDEA 用新不用旧,老有人说新版本bug多,其实呢,正在用的时候我是感知不到,相反他会在新版本修复老版本的bug,我觉得新版更好用更智能。如果你也深有其感,去拥抱新版本吧!

1.1 新建

springboot druid 整合mysql集群 springboot druid mybatis_mybatis

1.2 勾选依赖

springboot druid 整合mysql集群 springboot druid mybatis_spring boot_02

2. 导入 Druid 依赖

<!-- druid数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.11</version>
        </dependency>

3. 配置application.yml

一般我习惯把数据库相关的配置单独写一个配置文件中,然后使用spring.profiles.active 配置,启用只能配置文件,比如 application-druid.yml。

3.1 application.yml 中如下配置

# Spring配置
spring:
  # 启用指定配置文件
  #(文件名需满足application-{profile}.yml的格式)
  profiles:
    active: druid

3.2 application-druid.yml 中如下配置

spring:
    datasource:
        #1.JDBC
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/test_db?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8
        username: root
        password: ok
        druid:
            #2.连接池配置
            #初始化连接池的连接数量 大小,最小,最大
            initial-size: 5
            min-idle: 5
            max-active: 20
            #配置获取连接等待超时的时间
            max-wait: 60000
            #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
            time-between-eviction-runs-millis: 60000
            # 配置一个连接在池中最小生存的时间,单位是毫秒
            min-evictable-idle-time-millis: 30000
            validation-query: SELECT 1 FROM DUAL
            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'用于防火墙
            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

4. 尝试访问数据监控

地址即是druid.yml中配置的地址:http://localhost:8080/druid/login.html

springboot druid 整合mysql集群 springboot druid mybatis_mybatis_03


账号密码即是druid.yml中配置的账号密码:admin,admin

springboot druid 整合mysql集群 springboot druid mybatis_spring boot_04


到此其实关于整合Druid就结束了,正常的去写业务代码即可。

接下来再演示下整合Mybatis吧。

5. Mybatis配置

5.1 启动类@MapperScan注解

在启动类上添加@MapperScan注解,精确到mapper的位置。

如果不添加这个注解,则需要每个Mapper接口类都添加@Mapper注解比较麻烦。所以直接在启动类上配置批量扫描即可。

springboot druid 整合mysql集群 springboot druid mybatis_配置文件_05

5.2 application.yml配置

# MyBatis配置
mybatis:
  # 搜索指定包别名
  typeAliasesPackage: com.example.**.domain
  # 配置mapper的扫描,找到所有的mapper.xml映射文件
  mapperLocations: classpath*:mapper/**/*Mapper.xml
  # 加载全局的配置文件(没有可注释掉或删除)
  #configLocation: classpath:mybatis/mybatis-config.xml

5.3 demo大概结构:

springboot druid 整合mysql集群 springboot druid mybatis_配置文件_06

5.4 controller:

package com.example.druid.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.druid.demo.domain.UserTest;
import com.example.druid.demo.service.IUserTestService;


/**
 * demoController
 * 
 * @author 肥猫
 * @date 2022-07-25
 */
@RestController
@RequestMapping("/user/demo")
public class UserTestController
{
    @Autowired
    private IUserTestService userTestService;

    /**
     * 获取用户详细信息demo (仅演示功能,返回类型随便写的,不要照抄!)
     */
    @GetMapping(value = "/{id}")
    public UserTest getInfo(@PathVariable("id") Long id)
    {
        return userTestService.selectUserTestById(id);
    }
}

5.5 测试结果

ApiPost测试结果

ApiPost国人研发的类似PostMan又比PostMan更智能的工具。很喜爱。

springboot druid 整合mysql集群 springboot druid mybatis_mybatis_07


具体的业务代码就不拿出来了。都太简单了。主要的配置讲清楚就可以了。

算了有朋友需要,我上传到了网盘一份,需要下载: https://www.aliyundrive.com/s/ThxsHb76yEr