多数据源 更新 spring jta java jotm

 多数据源 更新 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()});
	}
	
}

  

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(java,spring,jta,多数据源,jotm)