spring+Ibatis+oracle多数据源配置

  • 格式:pdf
  • 大小:4.65 MB
  • 文档页数:24

Spring多数据源开发张启军,222009*********Spring开发中要使用到数据库ORACLE,DAO层IbatisDAO。

其中多个数据源要进一步开发。

这里先将多个数据源配置的,接着用ActionScript开发界面,就简单多了。

目录Spring多数据源开发 (1)1、开发IDE (2)2、工程预览 (3)3、项目开发 (4)3.1、环境包文件引入 (4)3.2、框架搭建 (7)3.2.1、数据配置文件:db.properties (7)3.2.2、src下面建立包 (7)3.2.3、建立ApplicationContext.xml文件 (7)3.2.4、spring-dao.xml文件配置 (13)3.2.5、BaseSqlMapClientDaoSupport.java文件 (15)3.2.6、sqlMapConfig.xml文件配置 (17)3.3、样例测试 (18)3.3.1、Person.java类 (18)3.3.2、Person_SqlMap.xml数据操作文件 (20)3.3.3、建立数据表 (20)3.3.4、dao目录下配置多个数据源 (21)3.3.5、spring-beans.xml文件配置 (22)4、附录 (24)1、开发IDE2、工程预览新建工程为Web Service Project项目:此处工程名为:【20140109WEB】,修改了WebRoot为web目录,整个项目概览:3、项目开发3.1、环境包文件引入添加Spring依赖包:选中工程工程--右键MyEclipse---Add Spring Runtime Dependencise如下图:下一步之后,弹出对话框,注意去掉勾选,创建libs文件包:点击Finish按钮之后,导入spring相关的文件包。

同时加入外面的依赖包:JAR包作用c3p0-0.9.1.2.jar数据源配置ibatis-2.3.4.726.jar DAO层的数据操作包,Ibatis包ojdbc14.jar数据库连接文件添加方法:选中项目--右键属性--如下图--添加JARS文件3.2、框架搭建3.2.1、数据配置文件:db.properties#JDBC driverdriverClass=oracle.jdbc.driver.OracleDriverjdbcUrl=jdbc:oracle:thin:@127.0.0.1:1521:orclminPoolSize=5maxPoolSize=20initialPoolSize=10maxIdleTime=5000acquireIncrement=12acquireRetryDelay=5acquireRetryAttempts=2idleConnectionTestPeriod=30000checkoutTimeout=100000#custUsercustUser=custUsercustPwd=custPwd#irmsUserirmsUser=irmsUserirmsPwd=irmsPwd3.2.2、src下面建立包在src下面建立包,参照目录,暂且不要引入任何文件。

3.2.3、建立ApplicationContext.xml文件选中config目录--右键“新建”--其他,在弹出的对话框中输入:‘spring’即可:选中“spring bean Definition”下一步选择“浏览”,然后选择要存放的目录,这里选择“src/config”。

文件名自动为applicationContext.xml如果要进一步设置spring属性,可以“下一步”,如果不知道如何选择,可以直接单击Finish即可有默认Spring配置:<?xml version="1.0"encoding="UTF-8"?><beans xmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans.xsd"> </beans>此时的applicationContext.xml建立完成,会有一片绿叶标识显示,很好看吧。

配置文件内容如下,:<?xml version="1.0"encoding="UTF-8"?><beans xmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans.xsd"><!--数据源配置文件--><bean id="propertyConfig"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"><value>classpath*:config/db.properties</value></property></bean><!--连接池配置--><bean id="daoCust"class="boPooledDataSource"destroy-method="close"><property name="driverClass"value="${driverClass}"></property><property name="jdbcUrl"value="${jdbcUrl}"></property><property name="user"value="${custUser}"></property><property name="password"value="${custPwd}"></property><property name="minPoolSize"value="${minPoolSize}"></property><property name="maxPoolSize"value="${maxPoolSize}"></property><property name="initialPoolSize"value="${initialPoolSize}"></property><property name="maxIdleTime"value="${maxIdleTime}"></property><property name="acquireIncrement"value="${acquireIncrement}"></property><property name="acquireRetryDelay"value="${acquireRetryDelay}"></property><property name="acquireRetryAttempts"value="${acquireRetryAttempts}"></property> <property name="idleConnectionTestPeriod"value="${idleConnectionTestPeriod}"></property> <property name="checkoutTimeout"value="${checkoutTimeout}"></property></bean><bean id="daoIrms"class="boPooledDataSource"destroy-method="close"><property name="driverClass"value="${driverClass}"></property><property name="jdbcUrl"value="${jdbcUrl}"></property><property name="user"value="${irmsUser}"></property><property name="password"value="${irmsPwd}"></property><property name="minPoolSize"value="${minPoolSize}"></property><property name="maxPoolSize"value="${maxPoolSize}"></property><property name="initialPoolSize"value="${initialPoolSize}"></property><property name="maxIdleTime"value="${maxIdleTime}"></property><property name="acquireIncrement"value="${acquireIncrement}"></property><property name="acquireRetryDelay"value="${acquireRetryDelay}"></property><property name="acquireRetryAttempts"value="${acquireRetryAttempts}"></property> <property name="idleConnectionTestPeriod"value="${idleConnectionTestPeriod}"></property> <property name="checkoutTimeout"value="${checkoutTimeout}"></property> </bean><!--事务配置--><bean id="transactionManagerCust"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"ref="daoCust"></property></bean><bean id="transactionManagerIrms"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"ref="daoIrms"></property></bean></beans>配置过程简介:(1)首先指出数据源的位置:<!--数据源配置文件--><bean id="propertyConfig"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"><value>classpath*:config/db.properties</value></property></bean>(2)然后配置数据源,这个就是要制定的数据源,这里使用c3p0包作为数据源管理,可以使用其他的。