java druid sqlite_springboot+mybatis+druid+sqlite/mysql/oracle

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

1.版本

springboot2.1.6

jdk1.8

2.最简springboot环境

参考这个博客搭建

最新的初始化地址

3.pom(sqlite配置)

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

mybatis-spring-boot-starter

druid

sqlite-jdbc

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-test

test

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.1.1

com.alibaba

druid

1.1.9

org.xerial

sqlite-jdbc

3.21.0.1

4.配置application.properties

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

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

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=1spring.datasource.minIdle=3spring.datasource.maxActive=20# 配置获取连接等待超时的时间

spring.datasource.maxWait=60000# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒

spring.datasource.timeBetweenEvictionRunsMillis=60000# 配置一个连接在池中最小生存的时间,单位是毫秒

spring.datasource.minEvictableIdleTimeMillis=30000spring.datasource.validationQuery=select 'x'spring.datasource.testWhileIdle=truespring.datasource.testOnBorrow=falsespring.datasource.testOnReturn=false# 打开PSCache,并且指定每个连接上PSCache的大小

spring.datasource.poolPreparedStatements=truespring.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=truemybatis.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;

@Configurationpublic classDruidConfiguration {private static final Logger logger = LoggerFactory.getLogger(DruidConfiguration.class);

@Bean

@Primary

@ConfigurationProperties(prefix= "spring.datasource")publicDataSource dataSource1() {return newDruidDataSource();

}/*@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 classDataSouceTest {

@Autowired

DataSource dataSource1;

@Testpublic voidcontextDuridLoads() 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.4application.properties里加入xml文件扫描路径

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

xml对应位置:

7.3编写xml文件

7.4编写测试方法

8.替换sqlite为oracle或mysql

8.1pom

org.xerial

sqlite-jdbc

3.21.0.1

com.oracle

ojdbc6

11.2.0.2

mysql

mysql-connector-java

5.1.6

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=

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

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

你可能感兴趣的:(java,druid,sqlite)