Springboot整合mybatis通用mapper以及Druid数据源

版本springboot 2.1.1

pom.xml引入的依赖


        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        
        
        
            tk.mybatis
            mapper-spring-boot-starter
            2.0.3
        
        
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.2.5
        

        
        
            mysql
            mysql-connector-java
            8.0.11
        
        
        
            com.alibaba
            druid
            1.1.10
        
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
        
            log4j
            log4j
            1.2.17
        

        
        
            tk.mybatis
            mapper
            4.0.3
        

对应application.properties

server.port=9999
server.servlet.context-path=/

spring.thymeleaf.prefix=classpath:/views/
spring.thymeleaf.suffix=.html



#mybatis&&通用Mapper
mybatis.type-aliases-package=com.jie.entity
mybatis.mapper-locations=classpath:mapper/*.xml
mapper.mappers=com.jie.utils.MyMapper
mapper.identity=MYSQL

#分页插件
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql

#log
logging.file=logger.log
logging.level.*=debug

#数据库Start----------------------------数据库Start
#数据库
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/szlp_risk?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#连接池配置
#初始化大小、最小空闲、最大激活
spring.datasource.initial-size=5
spring.datasource.min-idle=5
spring.datasource.max-active=10
#配置获取连接等待超时的时间
spring.datasource.max-wait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.time-between-eviction-runs-millis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.min-evictable-idle-time-millis=300000
spring.datasource.validation-query=SELECT 1 FROM DUAL
spring.datasource.test-while-idle=true
spring.datasource.test-on-borrow=false
spring.datasource.test-on-return=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.pool-prepared-statements=true
spring.datasource.max-pool-prepared-statement-per-connection-size=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.filters=stat,wall,log4j
# 合并多个DruidDataSource的监控数据
spring.datasource.use-global-data-source-stat=true
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
#数据库end----------------------------数据库end






  • 一,数据源配置
  • 1.druid bean配置  
@Configuration
public class DruidConfig {

    /**
     * 配置指定
     * 如果maven已经引入jpa则不需要加上初始化以及销毁方法,如果使用的是mybatis并且没有引入jpa,                               
     * 则需要加上(destroyMethod = "close", initMethod = "init")
     * @return datasource
     */
    @Bean(destroyMethod = "close", initMethod = "init")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DruidDataSource druid() {
        return new DruidDataSource();
    }
}
  • 2.过滤器配置
    // 2. 配置一个监控的filter
    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());

        Map initParams = new HashMap<>();
        //忽略资源
        initParams.put("exclusions","*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");

        bean.setInitParameters(initParams);
        bean.setUrlPatterns(Arrays.asList("/*"));

        return bean;
    }
  • 3.监控视图配置 
    //配置Druid的监控
    //1. 配置一个管理后台的servlet
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean bean =  new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        Map initParams = new HashMap<>();

        initParams.put("loginUsername","admin");//账号
        initParams.put("loginPassword","admin");//密码
        initParams.put("allow","");//默认允许所有
        initParams.put("deny","192.168.123.22");//不允许的黑名单ip

        bean.setInitParameters(initParams);
        return bean;
    }

完整类

package com.jie.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 java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: wsj
 * @Date: 2019/11/5 17
 * @Description:
 */

@Configuration
public class DruidConfig {

    /**
     * 配置指定
      * @return
     */
    @Bean(destroyMethod = "close", initMethod = "init")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DruidDataSource druid() {
        return new DruidDataSource();
    }

    //配置Druid的监控
    //1. 配置一个管理后台的servlet
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean bean =  new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        Map initParams = new HashMap<>();

        initParams.put("loginUsername","admin");//账号
        initParams.put("loginPassword","admin");//密码
        initParams.put("allow","");//默认允许所有
        initParams.put("deny","192.168.123.22");//不允许的黑名单ip

        bean.setInitParameters(initParams);
        return bean;
    }

    // 2. 配置一个监控的filter
    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());

        Map initParams = new HashMap<>();
        //忽略资源
        initParams.put("exclusions","*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");

        bean.setInitParameters(initParams);
        bean.setUrlPatterns(Arrays.asList("/*"));

        return bean;
    }

}

 

 

  • 4.启动 测试 完成
  • Springboot整合mybatis通用mapper以及Druid数据源_第1张图片
  • Springboot整合mybatis通用mapper以及Druid数据源_第2张图片

 二.通用mapper配置

写一个mapper继承通用mapper,以后其他只需要继承这个就可以了,该mapper不要被扫描到

请在启动类上加上要扫描的目标包@MapperScan(basePackages = {"com.jie.mapper"})
package com.jie.utils;

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

/**
 * 通用Mapper
 * @param 
 */
public interface MyMapper extends Mapper,MySqlMapper {

}

附一张目录树

Springboot整合mybatis通用mapper以及Druid数据源_第3张图片

参考博客1:https://blog.csdn.net/ajlzhu/article/details/81009845 

参考博客2:https://blog.csdn.net/CoffeeAndIce/article/details/78707819

你可能感兴趣的:(Springboot整合mybatis通用mapper以及Druid数据源)