spring 配置数据源
方式一:DBCP数据源
第一,beans标签需要引入的spring头部信息
< beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-instance.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
第二,准备properties数据源配置文件
#等号=前面的值可以随意定义相当于map中的key,=后设置数据库相关信息 #数据库驱动 base.driverClassName=oracle.jdbc.driver.OracleDriver #数据库地址 base.url=jdbc:oracle:thin:@192.168.0.21:1521:orcl #用户名 base.username=jmjkk #密码 base.password=jmjkk
第三,引入properties文件
< context:property-placeholder ignore-unresolvable="true" location="classpath:conf.properties" />
第四,将读取的properties文件中的数据配置进行指定
< bean id="dataSourceOracle" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> < property name="driverClassName" value="${base.driverClassName}" /> < property name="url" value="${base.url}" /> < property name="username" value="${base.username}" /> < property name="password" value="${base.password}" /> < property name="defaultAutoCommit" value="true" /> < property name="defaultReadOnly" value="false" /> < property name="initialSize" value="0" /> < property name="maxActive" value="10" /> < property name="maxIdle" value="1" /> < property name="maxWait" value="6000" /> < property name="testWhileIdle" value="true"/> < property name="timeBetweenEvictionRunsMillis" value="60000" /> < property name="numTestsPerEvictionRun" value="1" /> < property name="validationQuery" value="select 1 from dual" />
BasicDataSource提供了close()方法关闭数据源,所以必须设定destroy-method=”close”属性, 以便Spring容器关闭时,数据源能够正常关闭。
属性介绍:
defaultAutoCommit:设置从数据源中返回的连接是否采用自动提交机制,默认值为 true;
defaultReadOnly:设置数据源是否仅能执行只读操作, 默认值为 false;
maxActive:最大连接数据库连接数,设置为0时,表示没有限制;
maxIdle:最大等待连接中的数量,设置为0时,表示没有限制;
maxWait:最大等待秒数,单位为毫秒, 超过时间会报出错误信息;
validationQuery:用于验证连接是否成功的查询SQL语句,SQL语句必须至少要返回一行数据, 如你可以简单地设置为:select 1 from dual;
removeAbandoned:是否自我中断,默认是 false ;
removeAbandonedTimeout:几秒后数据连接会自动断开,在removeAbandoned为true,提供该值;
logAbandoned:是否记录中断事件, 默认为 false;
下面可以再添加事务配置等。
所需jar包,对应的数据库驱动以及dbcpjar包
比如:ojdbc7.jar和commons-dbcp-1.4.jar
方式二:DruidDataSource连接池配置
第一,beans标签引入需要的spring头部信息;
第二,准备properties数据源配置文件;
第三,引入properties文件;
< bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> < property name="location" value="classpath:conf.properties" />
第四,数据源连接池配置
< bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> < property name="url" value="${jdbc.url}" /> < property name="username" value="${jdbc.username}" /> < property name="password" value="${jdbc.password}" /> < property name="filters" value="stat" /> < property name="maxActive" value="100" /> < property name="initialSize" value="10" /> < property name="maxWait" value="60000" /> < property name="maxIdle" value="100" /> < property name="minIdle" value="30" /> < property name="timeBetweenEvictionRunsMillis" value="3000" /> < property name="minEvictableIdleTimeMillis" value="300000" /> < property name="validationQuery" value="SELECT 'x'" /> < property name="testWhileIdle" value="true" /> < property name="testOnBorrow" value="false" /> < property name="testOnReturn" value="false" /> < property name="poolPreparedStatements" value="true" /> < property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
所需jar包,对应的数据库驱动以及dbcpjar包
比如:org.springframework.beans-3.0.5.RELEASE.jar和classes12.jar
方式三:JNDI数据源
写在最后
哪位大佬如若发现文章存在纰漏之处或需要补充更多内容,欢迎留言!!!
相关推荐:
本文来自博客园,作者:Marydon,转载请注明原文链接:https://www.cnblogs.com/Marydon20170307/p/11653192.html