SpringBoot 整合 Mybatis+Druid

本文详细介绍如何在SpringBoot项目中整合MyBatis和Druid,包括依赖配置、数据库连接池设置及监控,实现高效数据库操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

此博客简单介绍springboot项目整合Mybstis + Druid 的项目使用经验。

项目结构大致如下:
        projectName
              - service
              - controller
                    - config
                    - resources
              - model

首先依赖包:

        <!--mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.2.0</version>
            <exclusions>
                <exclusion>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
            </exclusions>
        </dependency>    
    
        <!-- Druid数据库连接池组件 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.18</version>
        </dependency>

        <!-- oracle 驱动 -->
        <dependency>
            <groupId>oracle</groupId>
            <artifactId>ojdbc</artifactId>
            <version>5</version>
        </dependency>

其次在application.yml中加上mybatis与druid配置,需要在启动类上增加:@MapperScan("com.baoming.mapper"):

#服务名称
spring:
  application:
      name: projectName
#数据源地址
  datasource:
     url: jdbc:oracle:thin:@IP:1521:***
     username: username
     password: password
     driver-class-name: oracle.jdbc.driver.OracleDriver
     #druid连接池驱动配置信息
     type: com.alibaba.druid.pool.DruidDataSource
     #连接池配置
     initialSize: 5
     minIdle: 5
     maxActive: 20
     #连接等待超时时间
     maxWait: 60000
     #配置隔多久进行一次检测(检测可以关闭的空闲连接)
     timeBetweenEvictionRunsMillis: 60000
     #配置连接在池中的最小生存时间
     minEvictableIdleTimeMillis: 300000
     validationQuery: SELECT 1 FROM DUAL
     testWhileIdle: true
     testOnBorrow: false
     testOnReturn: false
     # 打开PSCache,并且指定每个连接上PSCache的大小
     poolPreparedStatements: true
     maxPoolPreparedStatementPerConnectionSize: 20
     # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
     filters: stat,wall,log4j
     # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
     connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

#Mybatis自动识别数据库驼峰字段
mybatis:
  configuration:
      map-underscore-to-camel-case: true
  #Mybatis实体与XML地址
  type-aliases-package: com.baoming.entity
  mapper-locations: classpath:mapper/*.xml

然后需要在Config中配置druid的相关信息,比如帐号密码,黑白名单等,具体释意请另行百度:

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * 连接池使用的数据库配置
 *
 * @Author: XiongMao
 * @Date: 2019-8-13
 */
@Configuration
public class DruidConfig {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        return druidDataSource;
    }
}
import com.alibaba.druid.support.http.WebStatFilter;

import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;

/**
 * 拦截器
 *
 * @Author: XiongMao
 * @Date: 2019-8-13
 */
@WebFilter(filterName = "druidWebStatFilter", urlPatterns = "/*",
        initParams = {
                @WebInitParam(name = "exclusions", value = "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")// 忽略资源
        })
public class DruidStatFilter extends WebStatFilter {

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

import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;

/**
 * servlet配置
 *
 * @Author: XiongMao
 * @Date: 2019-8-13
 */
@SuppressWarnings("serial")
@WebServlet(urlPatterns = "/druid/*",
        initParams={
                @WebInitParam(name="allow",value=""),// IP白名单 (没有配置或者为空,则允许所有访问)
                @WebInitParam(name="deny",value=""),// IP黑名单 (存在共同时,deny优先于allow)
                @WebInitParam(name="loginUsername",value=""),// 用户名
                @WebInitParam(name="loginPassword",value=""),// 密码
                @WebInitParam(name="resetEnable",value="false")// 禁用HTML页面上的“Reset All”功能
        })
public class DruidStatViewServlet extends StatViewServlet {

}

到此配置完成,就可以愉快的使用了。druid 访问地址为  IP:PORT/druid

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

熊猫猫猫猫猫猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值