web项目配置数据库连接的方式汇总
CreateTime--2016年12月25日11:13:00
Author:Marydon
方式一:tomcat配置数据库连接
1.在spring的XML文件中添加配置
(1)jndi数据源
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="xb_xnh"/> <property name="resourceRef" value="true"/> </bean>
(2)创建ibatis的sqlMapClient对象
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocations"> <list> <value>classpath:com/xyhsoft/demo/conf/ibatis/ibatisConfig.xml</value> </list> </property> <property name="dataSource" ref="dataSource" /> </bean>
2.tomcat配置
<Context docBase="D:WrokSpaceseclipse2016xyhcxybweb" path="/jmyb" reloadable="false"> <!-- 数据源配置,项目如果是热部署发布方式,Resource标签需放在Context标签体内;否则,放到<Host>标签体内 --> <Resource auth="Container" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@127.0.0.1:1521:orcl" name="xb_xnh" username="soctt" password="soctt" maxActive="50" maxIdle="10" maxWait="60000" testOnBorrow="true" testOnReturn="true" testWhileIdle="true" type="javax.sql.DataSource" validationQuery="SELECT COUNT(*) FROM DUAL" /> </Context>
方式二:ibatis/mybatis配置数据库连接
1.properties文件配置
(1)加载properties文件
<!--此bean告诉Spring去哪找数据库的配置信息,因为有此Bean才出现下面用${}标记来取变量的语句 --> <bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:db.properties</value><!-- classpath:必须加上 --> </property> </bean>
(2)properties文件内容:
platform.driverClassName=oracle.jdbc.driver.OracleDriver
platform.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
platform.username=soctt
platform.password=soctt
2.在spring的XML文件中添加配置
iBATIS提供数据库连接池
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${platform.driverClassName}" /> <property name="url" value="${platform.url}" /> <property name="username" value="${platform.username}" /> <property name="password" value="${platform.password}" /> <property name="maxActive" value="300"/> <property name="maxIdle" value="30"/> <property name="removeAbandoned" value="true"></property> <property name="removeAbandonedTimeout" value="60"></property> <property name="logAbandoned" value="true"></property> <property name="validationQuery" value=""></property> </bean>
3.创建ibatis的sqlMapClient对象
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocations"> <list> <value>classpath:com/xyhsoft/demo/conf/ibatis/ibatisConfig.xml</value> </list> </property> <property name="dataSource" ref="dataSource" /> </bean>
方式三:
1.spring配置数据库连接
(1)加载properties文件
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="classpath:db.properties"> </bean>
(2)properties文件内容:
#web项目数据库配置
xnh_oracle_driver=oracle.jdbc.driver.OracleDriver
xnh_oracle_url=jdbc:oracle:thin:@127.0.0.1:orcl
xnh_oracle_user=scott
xnh_oracle_pass=scott
2.在spring的XML文件中添加配置
(1)spring提供数据库连接池
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClassName" value="${xnh_oracle_driver}" /> <property name="url" value="${xnh_oracle_url}" /> <property name="username" value="${xnh_oracle_user}" /> <property name="password" value="${xnh_oracle_pass}" />> <property name="testConnectionOnCheckin" value="true"></property> <property name="idleConnectionTestPeriod" value="60"></property> </bean>
(2)创建ibatis的sqlMapClient对象
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocations"> <list> <value>classpath:com/xyhsoft/demo/conf/ibatis/ibatisConfig.xml</value> </list> </property> <property name="dataSource" ref="dataSource" /> </bean>
注:这种加载数据库的方式,控制台显示的是数据库连接地址的内存地址