SpringMVC hibernate加入多数据源 (SSHE/SYPRO加入多数据源为例)

来源:互联网 发布:淘宝pc端怎么看微淘 编辑:程序博客网 时间:2024/04/29 19:13

SpringMVC hibernate加入多数据源 


(以类SSHE/SYPRO加入多数据源为例作说明)


注:适用与SpringMVC + Hibernate的项目,其他框架的只能说作参考用


配置Spring


新建一个Spring的数据源配置文件,如spring-hibernate-aite.xml

怎么新建文件就不说了


新建的Spring的数据源配置文件的内容:

内容可以先将原有的spring-hibernate.xml完全copy过来,然后做相应修改

A.dataSource的相应修改

<!-- 配置数据源①bean的name属性,原来是dataSource 现在改成DataSourceAite --><bean name="dataSourceAite" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"><property name="url" value="${jdbc_url_aite}" /><property name="username" value="${jdbc_username_aite}" /><property name="password" value="${jdbc_password_aite}" /><!-- 初始化连接大小 --><property name="initialSize" value="0" /><!-- 连接池最大使用连接数量 --><property name="maxActive" value="20" /><!-- 连接池最大空闲 --><property name="maxIdle" value="20" /><!-- 连接池最小空闲 --><property name="minIdle" value="0" /><!-- 获取连接最大等待时间 --><property name="maxWait" value="60000" /><property name="validationQuery" value="${validationQuery}" /><property name="testOnBorrow" value="false" /><property name="testOnReturn" value="false" /><property name="testWhileIdle" value="true" /><!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --><property name="timeBetweenEvictionRunsMillis" value="60000" /><!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --><property name="minEvictableIdleTimeMillis" value="25200000" /><!-- 打开removeAbandoned功能 --><property name="removeAbandoned" value="true" /><!-- 1800秒,也就是30分钟 --><property name="removeAbandonedTimeout" value="1800" /><!-- 关闭abanded连接时输出错误日志 --><property name="logAbandoned" value="true" /><!-- 监控数据库 --><!-- <property name="filters" value="stat" /> --><property name="filters" value="mergeStat" /></bean>


B.sessionFactory的相应修改

<!-- 配置hibernate session工厂,①bean的id属性,原来的id是sessionFactory 现在改成sessionFactoryAite;②dataSource的ref,原来是dataSource 现在改成上边配好新 数据源名字dataSourceAite--><bean id="sessionFactoryAite" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><property name="dataSource" ref="dataSourceAite" /><property name="hibernateProperties"><props><prop key="hibernate.hbm2ddl.auto">none</prop><prop key="hibernate.dialect">${hibernate.dialect}</prop><prop key="hibernate.show_sql">${hibernate.show_sql}</prop><prop key="hibernate.format_sql">${hibernate.format_sql}</prop></props></property><!-- 自动扫描注解方式配置的hibernate类文件 --><property name="packagesToScan"><list><value>sy.*.model</value></list></property></bean>

C.事物管理器的相应更改

<!-- <span style="white-space:pre"></span>配置事务管理器 <span style="white-space:pre"></span>①bean的name属性,原来是transactionManager 现在改成transactionManagerAite<span style="white-space:pre"></span>②sessionFactory的ref,原来是sessionFactory 现在改成上边配好的新名字sessionFactoryAite<span style="white-space:pre"></span>--><span style="white-space:pre"></span><bean name="transactionManagerAite" class="org.springframework.orm.hibernate4.HibernateTransactionManager"><span style="white-space:pre"></span><property name="sessionFactory" ref="sessionFactoryAite"></property><span style="white-space:pre"></span></bean>


D.事物拦截规则的相应更改

<!-- 拦截器方式配置事物①用上边新创建的事物管理器来管理事物规则,原来id是transactionAdvice 现在改成transactionAdviceAite②修改id,原来id是 transactionAdvice 现在改成 transactionAdviceAite --><tx:advice id="transactionAdviceAite" transaction-manager="transactionManagerAite"><tx:attributes><tx:method name="add*" propagation="REQUIRED" /><tx:method name="append*" propagation="REQUIRED" /><tx:method name="save*" propagation="REQUIRED" /><tx:method name="update*" propagation="REQUIRED" /><tx:method name="modify*" propagation="REQUIRED" /><tx:method name="edit*" propagation="REQUIRED" /><tx:method name="del*" propagation="REQUIRED" /><tx:method name="delete*" propagation="REQUIRED" /><tx:method name="remove*" propagation="REQUIRED" /><tx:method name="repair" propagation="REQUIRED" /><tx:method name="delAndRepair" propagation="REQUIRED" /><tx:method name="get*" propagation="REQUIRED" read-only="true" /><tx:method name="find*" propagation="REQUIRED" read-only="true" /><tx:method name="load*" propagation="REQUIRED" read-only="true" /><tx:method name="search*" propagation="REQUIRED" read-only="true" /><tx:method name="datagrid*" propagation="REQUIRED" read-only="true" /><tx:method name="*" propagation="REQUIRED" read-only="true" /></tx:attributes></tx:advice>

E.Spring切面配置的相应更改
<!-- 将新的规则加入Spring的aop ①修改advice-ref,原来是transactionAdvice 先改成上边新名字transactionAdviceAite--><aop:config><aop:pointcut id="transactionPointcut" expression="execution(* sy.*.service..*Impl.*(..))" /><aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdviceAite" /></aop:config>

保存文件

到这儿,Spring要做的配置基本完事儿了


修改web.xml

要使新建的数据源配置文件spring-hibernate-aite.xml生效,需要在web.xml里边将加入classpath中

<context-param><param-name>contextConfigLocation</param-name><!-- 加入classpath:spring-hibernate-aite.xml --><param-value>classpath:spring.xml,classpath:spring-hibernate.xml,classpath:spring-hibernate-aite.xml</param-value></context-param>

保存文件

这个时候,可以启动运行一下项目,看看是否报错,在做下一步操作


新建dao使用新增的一个数据源

有几个dao就有几个数据源

新建daoI

这里可以直接将原来的sy.dao.BaseDaoI.java复制一个到同一包目录下,重命名为sy.dao.AiteBaseDaoI.java


新建daoImpl

这里可以直接将原来的sy.dao.impl.BaseDaoImpl.java复制一个到同一包目录下,重命名为sy.dao.AiteBaseDaoImpl.java


然后做一些小调整:

/**Repository原来是baseDao 这里修改成aiteBaseDao**/@Repository("aiteBaseDao")public class AiteBaseDaoImpl<T> implements AiteBaseDaoI<T> {private static final Logger logger=Logger.getLogger(AiteBaseDaoImpl.class);/**sessionFactory名字原来是sessionFactory 现在修改成sessionFactoryAite**/private SessionFactory sessionFactoryAite;public SessionFactory getSessionFactoryAite() {return sessionFactoryAite;}@Autowiredpublic void setAiteSessionFactory(SessionFactory sessionFactoryAite) {this.sessionFactoryAite = sessionFactoryAite;}private Session getCurrentSession() {return this.sessionFactoryAite.getCurrentSession();}

其他地方都不用修改

dao层写好了之后,可以启动运行下项目,看看有没有异常,没有异常则继续下一步操作


service层的调用

@Service("taskService")public class TaskServiceImpl implements TaskServiceI {/** 这里用新数据源的Dao就好了,其他的都和原来一致 **/private AiteBaseDaoI<Ttask> tdao;public AiteBaseDaoI<Ttask> getTdao() {return tdao;}@Autowiredpublic void setTdao(AiteBaseDaoI<Ttask> tdao) {this.tdao = tdao;}


到这里,从配置到使用都完成了,接下来就来完成你的应用吧











1 0
原创粉丝点击