1. 程式人生 > >spring boot + mybatis + druid 的整合

spring boot + mybatis + druid 的整合

Druid首先是一個數據庫連線池,但它不僅僅是一個數據庫連線池,它還包含一個ProxyDriver(代理),一系列內建的JDBC元件庫,一個SQL Parser(SQL解析器)。

廢話多說不宜下面看Demo:

1.pom.xml

        <!--jdbc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>

        <!--tomcat-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <!--test-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--alibaba druid (德魯伊)-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.6</version>
        </dependency>

2.application.yml

spring:
  datasource:
    druid:
      #   資料來源基本配置
      username: root
      password: root
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/db_mybatis?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
      type: com.alibaba.druid.pool.DruidDataSource

      #   資料來源其他配置
      initialSize: 10
      minIdle: 5
      maxActive: 20
      maxWait: 60000
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 300000
      validationQuery: SELECT 1 FROM DUAL
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      poolPreparedStatements: true

      #   配置監控統計攔截的filters,去掉後監控介面sql無法統計,'wall'用於防火牆
      filters: stat,wall,slf4j
      maxPoolPreparedStatementPerConnectionSize: 20
      useGlobalDataSourceStat: true
      connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

#mybatis
mybatis:
  mapper-locations: classpath:mappers/*.xml  #注意:一定要對應mapper對映xml檔案的所在路徑
  type-aliases-package: xyz.askway.spring_boot_integration_mybatis.pojo  # 注意:對應實體類的路徑

3.專案結構

4.程式碼

建立DruidDataSourceConfig類

package xyz.askway.spring_boot_integration_mybatis.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * author:wlm
 * name:druid配置類
 * function:
 * 2018/11/10 10:08
 */
@Configuration //告訴spring boot 這是一個配置類
public class DruidDataSourceConfig {

    //引用application.yml中的配置
    @ConfigurationProperties(prefix = "spring.datasource.druid")
    @Bean
    public DataSource druid(){
        return new DruidDataSource();
    }

    /**
     * 配置Druid的監控
     *  1、配置一個管理後臺的Servlet
     * @return
     */
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean bean=new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        Map<String,String> initParams=new HashMap<>();
        //白名單:
        initParams.put("allow","");//預設就是允許所有訪問
        //IP黑名單 (存在共同時,deny優先於allow) : 如果滿足deny的話提示:Sorry, you are not permitted to view this page.
        initParams.put("deny","192.20.10.5");
        //Druid監控後臺登入賬號密碼
        initParams.put("loginUsername","admin");
        initParams.put("loginPassword","123456");
        bean.setInitParameters(initParams);
        return bean;
    }

    /**
     *  2、配置一個web監控的filter
     * @return
     */
    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        //新增過濾規則.
        filterRegistrationBean.addUrlPatterns("/*");
        //新增不需要忽略的格式資訊.
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid2/*");
        return filterRegistrationBean;
    }

}

注意:在spring boot 啟動類中加上@MapperScan("dao包的全路徑名")            

mybatis的操作和以前一樣的寫法 dao、mapper.xml 、service、serviceImpl

5.測試

通過spring boot 的啟動類啟動專案