多資料源 更新 spring jta java jotm
代碼下載下傳 :
http://download.csdn.net/download/knight_black_bob/8816323 對比 下面的 多資料庫操作 http://knight-black-bob.iteye.com/blog/2212872大家 可以 感受一下 優缺點………
applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-autowire="byName"
>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:db-config.properties"></property>
</bean>
<!-- 引用Spring内部所提供的對JOTM支援的工廠類 -->
<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" />
<!-- 配置JTA事務管理器, 并在管理器中使用上面所配置的JOTM -->
<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction" ref="jotm" />
</bean>
<!-- 配置多個資料源 -->
<!--第一個資料源,采用XAPool連結池-->
<bean id="dataSourceA" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
<property name="dataSource">
<bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
<property name="transactionManager" ref="jotm" />
<property name="driverName" value="${driverClassName}" />
<property name="url" value="${url1}" />
</bean>
</property>
<property name="user" value="${username1}" />
<property name="password" value="${password1}" />
</bean>
<!--第二個資料源,采用XAPool連結池-->
<bean id="dataSourceB" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown">
<property name="dataSource">
<bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown">
<property name="transactionManager" ref="jotm" />
<property name="driverName" value="${driverClassName}" />
<property name="url" value="${url2}" />
</bean>
</property>
<property name="user" value="${username2}" />
<property name="password" value="${password2}" />
</bean>
<!-- 根據不同的資料源配置兩個jdbcTemplate -->
<bean id="jdbcTemplateA" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSourceA" />
</bean>
<bean id="jdbcTemplateB" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSourceB" />
</bean>
<!-- JTA事務傳播特性 -->
<tx:advice id="txAdviceJTA" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="create*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="insert*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="del*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor pointcut="execution(* com.baoy.service..*(..))" advice-ref="txAdviceJTA" />
</aop:config>
</beans>
applicationContext-dao.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-autowire="byName"
>
<bean id="userDao" class="com.baoy.daoImpl.UserDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplateA" />
</bean>
<bean id="providerDao" class="com.baoy.daoImpl.ProviderDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplateB" />
</bean>
</beans>
applicationContext-service.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-autowire="byName"
>
<bean id="userService" class="com.baoy.serviceImpl.UserServiceImpl"></bean>
</beans>
applicationContext-action.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-autowire="byName"
>
<bean id="userAction" class="com.baoy.action.UserAction" scope="prototype"></bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee"
xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
id="WebApp_9" version="2.4">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>/common/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
package com.baoy.action;
import com.baoy.base.action.BaseAction;
import com.baoy.bean.User;
import com.baoy.service.UserService;
public class UserAction extends BaseAction{
private UserService userService;
public String doRegister(){
User user = new User();
user.setUserid(888888);
user.setUsername(888888+"");
user.setUserpswd(888888+"");
user.setNickname(888888+"");
user.setContacts(888888+"") ;
user.setTelephone(888888+"");
user.setProvinceid(888888);
user.setEnterprise(888888+"");
user.setRegtime("2015-02-17 16:49:00");
user.setRoleid(1);
user.setSpid(888888+"");
user.setSpkey(888888+"");
userService.regist(user);
return "success";
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
}
package com.baoy.bean;
public class Provider {
//spid,spkey,user_id,company,address,province_code,insert_time
private String spid; // '廠商ID',
private String spkey; //'廠商密鑰',
private int userId ; //'使用者名',
private String company; //'廠商名稱',
private String address; // '廠商位址',
private int provinceCode; // '所在省份,外鍵',
private String insertTime;
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append (getClass().getName().substring(getClass().getName().lastIndexOf(".")+1, getClass().getName().length()));
sb.append(":[");
sb.append("\"spid\":\"" +spid+"\"," );
sb.append("\"spkey\":\"" +spkey+"\"," );
sb.append("\"userId\":\"" +userId+"\"," );
sb.append("\"company\":\"" +company+"\"" );
sb.append("\"address\":\"" +address+"\"," );
sb.append("\"provinceCode\":\"" +provinceCode+"\"," );
sb.append("\"insertTime\":\"" +insertTime+"\"" );
sb.append("]");
return sb.toString() ;
}
public String getSpid() {
return spid;
}
public void setSpid(String spid) {
this.spid = spid;
}
public String getSpkey() {
return spkey;
}
public void setSpkey(String spkey) {
this.spkey = spkey;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getProvinceCode() {
return provinceCode;
}
public void setProvinceCode(int provinceCode) {
this.provinceCode = provinceCode;
}
public String getInsertTime() {
return insertTime;
}
public void setInsertTime(String insertTime) {
this.insertTime = insertTime;
}
}
package com.baoy.bean;
public class User {
//userid,username,userpswd,nickname,contacts,telephone,enterprise,regtime,provinceid,roleid,spid,spkey
private int userid;
private String username;
private String userpswd;
private String nickname;
private String contacts;
private String telephone;
private String enterprise;
private int provinceid;
private String regtime;
private int roleid;
private String spid;
private String spkey;
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append (getClass().getName().substring(getClass().getName().lastIndexOf(".")+1, getClass().getName().length()));
sb.append(":[");
sb.append("\"userid\":\"" +userid+"\"," );
sb.append("\"username\":\"" +username+"\"," );
sb.append("\"userpswd\":\"" +userpswd+"\"," );
sb.append("\"nickname\":\"" +nickname+"\"" );
sb.append("\"contacts\":\"" +contacts+"\"," );
sb.append("\"telephone\":\"" +telephone+"\"," );
sb.append("\"enterprise\":\"" +enterprise+"\"," );
sb.append("\"provinceid\":\"" +provinceid+"\"" );
sb.append("\"regtime\":\"" +regtime+"\"," );
sb.append("\"roleid\":\"" +roleid+"\"," );
sb.append("\"spid\":\"" +spid+"\"," );
sb.append("\"spkey\":\"" +spkey+"\"" );
sb.append("]");
return sb.toString() ;
}
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpswd() {
return userpswd;
}
public void setUserpswd(String userpswd) {
this.userpswd = userpswd;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getContacts() {
return contacts;
}
public void setContacts(String contacts) {
this.contacts = contacts;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getEnterprise() {
return enterprise;
}
public void setEnterprise(String enterprise) {
this.enterprise = enterprise;
}
public int getProvinceid() {
return provinceid;
}
public void setProvinceid(int provinceid) {
this.provinceid = provinceid;
}
public String getRegtime() {
return regtime;
}
public void setRegtime(String regtime) {
this.regtime = regtime;
}
public int getRoleid() {
return roleid;
}
public void setRoleid(int roleid) {
this.roleid = roleid;
}
public String getSpid() {
return spid;
}
public void setSpid(String spid) {
this.spid = spid;
}
public String getSpkey() {
return spkey;
}
public void setSpkey(String spkey) {
this.spkey = spkey;
}
}
package com.baoy.daoImpl;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import com.baoy.bean.Provider;
import com.baoy.dao.ProviderDao;
public class ProviderDaoImpl extends JdbcDaoSupport implements ProviderDao{
@Override
public int saveProvider(Provider provider) {
String sql = "insert into provider(spid,spkey,user_id,company,address,province_code,insert_time) values (?,?,?,?,?,?,?)";
return this.getJdbcTemplate().update(sql,new Object[] {provider.getSpid(),provider.getSpkey(),provider.getUserId(),provider.getCompany(),provider.getAddress(),provider.getProvinceCode(),provider.getInsertTime()});
}
}
package com.baoy.daoImpl;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import com.baoy.bean.User;
import com.baoy.dao.UserDao;
public class UserDaoImpl extends JdbcDaoSupport implements UserDao{
@Override
public int saveUser(User user) {
String sql = "insert into user(userid,username,userpswd,nickname,contacts,telephone,enterprise,regtime,provinceid,roleid,spid,spkey) values (?,?,?,?,?,?,?,?,?,?,?,?)";
return this.getJdbcTemplate().update(sql, new Object[]
{user.getUserid(),
user.getUsername(),
user.getUserpswd(),
user.getNickname(),
user.getContacts() ,
user.getTelephone(),
user.getEnterprise(),
user.getRegtime(),
user.getProvinceid(),
user.getRoleid(),
user.getSpid(),
user.getSpkey()});
}
}
捐助開發者
在興趣的驅動下,寫一個
免費
的東西,有欣喜,也還有汗水,希望你喜歡我的作品,同時也能支援一下。 當然,有錢捧個錢場(右上角的愛心标志,支援支付寶和PayPal捐助),沒錢捧個人場,謝謝各位。

謝謝您的贊助,我會做的更好!