Spring多数据源配置方式一
来源:互联网 发布:tower windows 破解版 编辑:程序博客网 时间:2024/06/11 17:36
spring多数据源配置方式一,为每个数据源单独创建数据源、sessionFactory、事物管理等
一、数据源配置
1. 数据源1配置文件
<?xml version="1.0" encoding="UTF-8"?> <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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 数据库连接池配置 --> <bean id="dataSourceTask" name="dataSourceTask" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 指定连接数据库的驱动--> <property name="driverClass" value="${db.task.jdbc.driverClassName}"/> <!-- 指定连接数据库的URL--> <property name="jdbcUrl" value="${db.task.jdbc.url}"/> <!-- 指定连接数据库的用户名--> <property name="user" value="${db.task.jdbc.user}"/> <!-- 指定连接数据库的密码--> <property name="password" value="${db.task.jdbc.password}"/> <!-- 指定连接池中保留的最大连接数. Default:15--> <property name="maxPoolSize" value="${db.task.jdbc.maxPoolSize}"/> <!-- 指定连接池中保留的最小连接数--> <property name="minPoolSize" value="${db.task.jdbc.minPoolSize}"/> <!-- 指定连接池的初始化连接数 取值应在minPoolSize 与 maxPoolSize 之间.Default:3--> <property name="initialPoolSize" value="${db.task.jdbc.initialPoolSize}"/> <!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。 Default:0--> <property name="maxIdleTime" value="${db.task.jdbc.maxIdleTime}"/> <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数. Default:3--> <property name="acquireIncrement" value="${db.task.jdbc.acquireIncrement}"/> <!-- JDBC的标准,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements属于单个connection 而不是整个连接池所以设置这个参数需要考虑到多方面的因数.如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:0--> <property name="maxStatements" value="${db.task.jdbc.maxStatements}"/> <!-- 每60秒检查所有连接池中的空闲连接.Default:0 --> <property name="idleConnectionTestPeriod" value="${db.task.jdbc.idleConnectionTestPeriod}"/> <!--连接池获取新连接的时间,超时后将 抛出SQLException,如设为0则无限期等待。单位毫秒。Default: 0 --><property name="checkoutTimeout" value="${db.task.jdbc.checkoutTimeout}"/> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactoryTask" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSourceTask" /> <!-- 自动扫描mapping.xml文件 --> <property name="mapperLocations" value="classpath:/mapper_task/*.xml"/> </bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.johnfnash.learn.pro.dashboardserver.dao.task" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryTask"/> </bean> <!-- 配置事物管理器 --> <bean id="transactionManagerTask" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSourceTask" /> </bean> <!-- 拦截器方式配置事物 --> <tx:advice id="transactionAdviceTask" transaction-manager="transactionManagerTask"><tx:attributes><tx:method name="add*" propagation="REQUIRED" /><tx:method name="insert*" propagation="REQUIRED" /><tx:method name="update*" propagation="REQUIRED" /><tx:method name="delete*" propagation="REQUIRED" /><tx:method name="get*" propagation="SUPPORTS" read-only="true" /><tx:method name="find*" propagation="SUPPORTS" read-only="true" /><tx:method name="select*" propagation="SUPPORTS" read-only="true" /></tx:attributes> </tx:advice> <!-- spring aop事物管理 --> <aop:config> <aop:pointcut id="transactionPointcutTask" expression="execution(* com.johnfnash.learn.pro.dashboardserver.service..*Impl.*(..))" /> <aop:advisor pointcut-ref="transactionPointcutTask" advice-ref="transactionAdviceTask" /> </aop:config></beans>2. 数据源2配置文件 datasource_environ.xml
<?xml version="1.0" encoding="UTF-8"?> <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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 数据库连接池配置 --> <bean id="dataSourceLocal" name="dataSourceEnv" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 指定连接数据库的驱动--> <property name="driverClass" value="${db.lcmyw.jdbc.driverClassName}"/> <!-- 指定连接数据库的URL--> <property name="jdbcUrl" value="${db.lcmyw.jdbc.url}"/> <!-- 指定连接数据库的用户名--> <property name="user" value="${db.lcmyw.jdbc.user}"/> <!-- 指定连接数据库的密码--> <property name="password" value="${db.lcmyw.jdbc.password}"/> <!-- 指定连接池中保留的最大连接数. Default:15--> <property name="maxPoolSize" value="${db.lcmyw.jdbc.maxPoolSize}"/> <!-- 指定连接池中保留的最小连接数--> <property name="minPoolSize" value="${db.lcmyw.jdbc.minPoolSize}"/> <!-- 指定连接池的初始化连接数 取值应在minPoolSize 与 maxPoolSize 之间.Default:3--> <property name="initialPoolSize" value="${db.lcmyw.jdbc.initialPoolSize}"/> <!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。 Default:0--> <property name="maxIdleTime" value="${db.lcmyw.jdbc.maxIdleTime}"/> <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数. Default:3--> <property name="acquireIncrement" value="${db.lcmyw.jdbc.acquireIncrement}"/> <!-- JDBC的标准,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements属于单个connection 而不是整个连接池所以设置这个参数需要考虑到多方面的因数.如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:0--> <property name="maxStatements" value="${db.lcmyw.jdbc.maxStatements}"/> <!-- 每60秒检查所有连接池中的空闲连接.Default:0 --> <property name="idleConnectionTestPeriod" value="${db.lcmyw.jdbc.idleConnectionTestPeriod}"/> <!--连接池获取新连接的时间,超时后将 抛出SQLException,如设为0则无限期等待。单位毫秒。Default: 0 --><property name="checkoutTimeout" value="${db.lcmyw.jdbc.checkoutTimeout}"/> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSourceLocal" /> <!-- 自动扫描mapping.xml文件 --> <property name="mapperLocations" value="classpath:/mapper_env/*.xml"/> </bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.johnfnash.learn.pro.dashboardserver.dao.env" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!-- 配置事物管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSourceLocal" /> </bean> <!-- 拦截器方式配置事物 --> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="add*" propagation="REQUIRED" /><tx:method name="insert*" propagation="REQUIRED" /><tx:method name="update*" propagation="REQUIRED" /><tx:method name="delete*" propagation="REQUIRED" /><tx:method name="get*" propagation="SUPPORTS" read-only="true" /><tx:method name="find*" propagation="SUPPORTS" read-only="true" /><tx:method name="select*" propagation="SUPPORTS" read-only="true" /></tx:attributes> </tx:advice> <!-- spring aop事物管理 --> <aop:config> <aop:pointcut id="transactionPointcut" expression="execution(* com.johnfnash.learn.pro.dashboardserver.service..*Impl.*(..))" /> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" /> </aop:config></beans>
注意:使用这种方式时,设置sessionFactory时,为不同的数据库设置不同的 mapperLocations, spring扫描mapper接口的 MapperScannerConfigurer设置成不同的包名
示例如下:
二、优缺点
优点:同一service层中可以直接使用不同数据源的Mapper,灵活;数据源为不同类型时,可以为每一个数据源单独设置数据库方言
缺点:需要为每一个数据源创建sessionFactory、事物管理等,比较麻烦
0 0
- Spring多数据源配置方式一
- spring多数据源配置方式
- spring 多数据源配置方式
- spring多数据源配置+aop注解方式属性注入
- Spring多数据源配置系列(一)——多数据源配置
- spring配置多数据源
- Spring多数据源配置
- Spring多数据源配置
- spring多数据源配置
- Spring配置多数据源
- Spring多数据源配置
- Spring配置多数据源
- spring多数据源配置
- spring多数据源配置
- Spring多数据源配置
- spring 多数据源配置
- spring 配置多数据源
- Spring多数据源配置
- 文章标题
- 【jzoj3737】【挖宝藏】【斯坦纳树】【状态压缩动态规划】
- 我的H5生涯2
- 鸟哥的linux私房菜学习笔记《二十六》ACL权限管理
- git 配置user和email信息
- Spring多数据源配置方式一
- 5016. 数列
- sort对二维字符数组排序
- React Native 中POST请求参数传递
- new写在循环内部和外部的区别
- Android Dialog圆角问题
- CCLuaStack.h详解
- 北航计算机机试07字符串统计
- 内存空间