spring多数据源配置
来源:互联网 发布:浙江淘宝网络有限公司 编辑:程序博客网 时间:2024/06/08 09:26
前段时间由于公司项目需求,需要多数据源的支持,苦b折腾了两天程序猿,话不多说,直接撸码。
application-context.xml配置:
<!-- 引入jdbc配置文件 --><bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list> <value>classpath:jdbc.properties</value></list></property> </bean><!-- 配置数据源 one--><bean id="dataSourceone" class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName" value="${jdbc.driver}"></property><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.user}"></property><property name="password" value="${jdbc.pass}"></property> <!--initialSize: 初始化连接 --><property name="initialSize" value="5" /> <!--maxIdle: 最大空闲连接 --><property name="maxIdle" value="10" /><property name="minIdle" value="5" /><property name="maxActive" value="15" /><property name="removeAbandoned" value="true" /><property name="removeAbandonedTimeout" value="180" /> <!-- maxWait: 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 --><property name="maxWait" value="3000" /><property name="validationQuery"><value>SELECT 1</value></property><property name="testOnBorrow"><value>true</value></property></bean><!-- 配置数据源 two--><bean id="dataSourcetwo" class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName" value="${jdbc.driver1}"></property><property name="url" value="${jdbc.url1}"></property><property name="username" value="${jdbc.user1}"></property><property name="password" value="${jdbc.pass1}"></property> <!--initialSize: 初始化连接 --><property name="initialSize" value="5" /> <!--maxIdle: 最大空闲连接 --><property name="maxIdle" value="10" /><property name="minIdle" value="5" /><property name="maxActive" value="15" /><property name="removeAbandoned" value="true" /><property name="removeAbandonedTimeout" value="180" /> <!--maxWait: 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 --><property name="maxWait" value="3000" /><property name="validationQuery"><value>SELECT 1</value></property><property name="testOnBorrow"><value>true</value></property></bean> <!-- 通过key-value的形式来关联数据源 --> <bean id="dynamicDataSource" class="com.jeekup.rp.util.DynamicDataSource" > <property name="targetDataSources"> <map> <entry value-ref="dataSourcethree" key="dataSourcethree"></entry> <entry value-ref="dataSourceone" key="dataSourceone"></entry> <entry value-ref="dataSourcetwo" key="dataSourcetwo"></entry> </map> </property><!--默认当前的数据源 --> <property name="defaultTargetDataSource" ref="dataSourceone" /> </bean> <!-- JdbcTemplate使用动态数据源的配置管理 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"> <ref bean="dynamicDataSource" /> </property> </bean> <!-- 配置sessionFactory --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><!-- 读取数据源 --><property name="dataSource" ref="dynamicDataSource" /><!-- 找到实体类的对象 --><property name="packagesToScan" value="com.jeekup.rp.model" /> <!-- 数据库规则 --><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.show_sql">false</prop><prop key="hibernate.hbm2ddl.auto">update</prop><prop key="hibernate.format_sql">true</prop></props></property></bean><!-- 配置sessionFactory事物 --><bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory"><ref bean="sessionFactory" /></property></bean><!-- 注入BaseDao --> <bean id="BaseDaoimp" class="com.base.imp.BaseDaoimp"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- 配置事务的传播特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="save*" propagation="REQUIRED" /><tx:method name="add*" propagation="REQUIRED" /><tx:method name="delete*" propagation="REQUIRED" /><tx:method name="update*" propagation="REQUIRED" /><tx:method name="execute*" propagation="REQUIRED" /><tx:method name="get*" read-only="true"/><tx:method name="*" read-only="true" /></tx:attributes></tx:advice>创建一个动态数据源类:
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;public class DynamicDataSource extends AbstractRoutingDataSource{ @Override protected Object determineCurrentLookupKey() { return DBContextHolder.getDBType(); } }
再创建一个数据源管理控制类:public class DBContext{ public static final String DATA_one = "dataSourceone"; public static final String DATA_tow = "dataSourcetwo"; private static final ThreadLocal<String> context = new ThreadLocal<String>(); public static void setDBType(String dbType) { context.set(dbType); } public static String getDBType() { return context.get(); } public static void clearDBType() { contextHolder.remove(); } }写到这里,配置基本完毕了,接下来就可以读取不同的数据源,来进行持久化操作了:
@Servicepublic class adminService{ @Autowired private BaseDao dao;public List<admin> get_admin() { DBContext.setDBType(DBContext.DATA_one);//这里就可以用来切换了! return (List<admin>) dao.findByHql("from admin",new Object[]{}); }
我dao层封装方法:public List<?> findByHql(String hql, Object[] obj) { return getHibernateTemplate().find(hql, obj); }
上面就是spring多数据源的全部代码,写的不好的地方,技术大神请指出。
1 0
- spring配置多数据源
- Spring多数据源配置
- Spring多数据源配置
- spring多数据源配置
- Spring配置多数据源
- Spring多数据源配置
- Spring配置多数据源
- spring多数据源配置
- spring多数据源配置
- Spring多数据源配置
- spring 多数据源配置
- spring 配置多数据源
- Spring多数据源配置
- spring多数据源配置
- Spring多数据源配置
- Spring多数据源配置
- spring多数据源配置
- spring配置多数据源
- JAVA 学习之标识符
- Android开发注意点及android第三方架构
- 算法-java(2)
- 调用手机打电话发信息等
- java基本数据类型与二进制
- spring多数据源配置
- 宏的应用
- 算法的力量 李开复
- ConcurrentHashMap和HashMap的区别
- 有用的git使用链接
- #每日一题#腾讯笔试题
- c#中Enum 的遍历
- java中IP地址、主机名的获取
- js 字符串转换数字