搭建springboot+mybatis+druid+sqlite/mysql/oracle附带测试

 

1.版本

springboot2.1.6

jdk1.8

2.最简springboot环境

https://www.cnblogs.com/SmilingEye/p/11422536.html

3.pom(sqlite配置)

spring-boot-starter与spring-boot-starter-test

mybatis-spring-boot-starter

druid

sqlite-jdbc

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.1.1</version>
  </dependency>
  <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.9</version>
  </dependency>
  <dependency>
    <groupId>org.xerial</groupId>
    <artifactId>sqlite-jdbc</artifactId>
    <version>3.21.0.1</version>
  </dependency>
</dependencies>

4.配置application.properties

注意1:sqlite不支持spring.datasource.filters的wall,请去掉

注意2:spring.datasource.type=com.alibaba.druid.pool.DruidDataSource这一行以下的连接池的配置无法被druid加载,需要自己将配置设置到druid里,按照官网配置也不行。

官网配置https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter

spring.datasource.driver-class-name=org.sqlite.JDBC
spring.datasource.url=jdbc:sqlite:F:/epgis/program/gis-server/dataserver.pak
spring.datasource.username=
spring.datasource.password= 

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# 初始化大小,最小,最大
spring.datasource.initialSize=1
spring.datasource.minIdle=3
spring.datasource.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=30000
spring.datasource.validationQuery=select 'x'
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize= 20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
# sqlite不支持wall,使用sqlite请去掉
spring.datasource.filters=stat,slf4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
#useGlobalDataSourceStat=true

mybatis.mapper-locations=classpath:mybatis/*.xml

5.将application.properties配置设置到druid里

如果不做第5步,数据库连接池也可以正常使用,但是它是使用的默认配置,非application.properties配置。

注意1:@configurationProperties的prefix里的设置,spring.datasource代表application.properties里的spring.datasource下的值,将自动调用DruidDataSource相同名称的set方法。

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
public class DruidConfiguration {
 
    private static final Logger logger = LoggerFactory.getLogger(DruidConfiguration.class);
 
    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource1() {
        return new DruidDataSource();
    }
 
    /*@Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }*/
}

6.编写测试类,验证数据库连接池设置成功

package com.epgis.gisserver;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.alibaba.druid.pool.DruidDataSource;

@SpringBootTest
@RunWith(SpringRunner.class)
public class DataSouceTest {

    @Autowired
    DataSource dataSource1;

    @Test
    public void contextDuridLoads() throws SQLException {
        Connection con = dataSource1.getConnection();
        System.err.println("**************");
        System.err.println(dataSource1);
        DruidDataSource dss = (DruidDataSource)dataSource1;
        System.err.println(dss.getName());
        System.err.println(dss.getValidationQuery());
        System.err.println(dss.getTimeBetweenEvictionRunsMillis());
        System.err.println(dss.getMinEvictableIdleTimeMillis());
        // 数据源例如:HikariDataSource(springboot2.x默认),DruidDataSource,dbcp2DataSource
        System.err.println(dataSource1.getClass().getName());
        // 连接例如org.sqlite.SQLiteConnection
        System.err.println(con);
        System.err.println("**************");
        con.close();
    }
}

检查1:dataSource1对象为DruidDataSource即为数据库连接池设置成功。

检查2:dss.getValidationQuery()的值为application.properties里spring.datasource.validationQuery的值"select 'x'",即为其他数据库连接池配置成功。

 7.编写及配置mybatis

很多人对这一步骤很熟悉了,在此简写。

7.1在开始类上加入@MapperScan("com.epgis.gisserver.**.dao")

7.2编写dao接口

7.3application.properties里加入xml文件扫描路径

路径:mybatis.mapper-locations=classpath:mybatis/*.xml

xml对应位置:

springboot+mybatis+druid+sqlite/mysql/oracle

 

7.4编写xml文件

7.5编写测试方法

8.替换sqlite为oracle或mysql

8.1pom

<dependency>
    <groupId>org.xerial</groupId>
    <artifactId>sqlite-jdbc</artifactId>
    <version>3.21.0.1</version>
</dependency>
<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>11.2.0.2</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.6</version>
</dependency>

8.2application.properties文件

spring.datasource.driver-class-name=org.sqlite.JDBC
spring.datasource.url=jdbc:sqlite:F:/epgis/program/gis-server/dataserver.pak
spring.datasource.username=
spring.datasource.password= 
springboot+mybatis+druid+sqlite/mysql/oracle
springboot+mybatis+druid+sqlite/mysql/oracle
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.url = jdbc:oracle:thin:@127.0.0.1:1521:orcl
spring.datasource.username = test
spring.datasource.password = test
springboot+mybatis+druid+sqlite/mysql/oracle
springboot+mybatis+druid+sqlite/mysql/oracle
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mrbird?useUnicode=true&characterEncoding=utf8
spring.datasource.username=test
spring.datasource.password= test