SpringMVC hibernate增加多數據源 (SSHE/SYPRO增加多數據源為例)
阿新 • • 發佈:2017-07-24
ram idle tasks 一個 pen san t_sql rem 一次
C.事物管理器的對應更改
E.Spring切面配置的對應更改
保存文件
保存文件
其它地方都不用改動
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>
<!-- 配置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>
<!-- 攔截器方式配置事物 ①用上邊新創建的事物管理器來管理事物規則,原來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; } @Autowired public 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; } @Autowired public void setTdao(AiteBaseDaoI<Ttask> tdao) { this.tdao = tdao; }
到這裏,從配置到使用都完畢了,接下來就來完畢你的應用吧
SpringMVC hibernate增加多數據源 (SSHE/SYPRO增加多數據源為例)