多数据源 更新 spring jta java jotm

简介:  多数据源 更新 spring jta java  jotm   代码下载 :http://download.csdn.net/download/knight_black_bob/8816323   对比 下面的 多数据库操作   http://knight-black-bob.

 多数据源 更新 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捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

目录
相关文章
|
7天前
|
监控 Java 应用服务中间件
高级java面试---spring.factories文件的解析源码API机制
【11月更文挑战第20天】Spring Boot是一个用于快速构建基于Spring框架的应用程序的开源框架。它通过自动配置、起步依赖和内嵌服务器等特性,极大地简化了Spring应用的开发和部署过程。本文将深入探讨Spring Boot的背景历史、业务场景、功能点以及底层原理,并通过Java代码手写模拟Spring Boot的启动过程,特别是spring.factories文件的解析源码API机制。
23 2
|
19天前
|
人工智能 前端开发 Java
基于开源框架Spring AI Alibaba快速构建Java应用
本文旨在帮助开发者快速掌握并应用 Spring AI Alibaba,提升基于 Java 的大模型应用开发效率和安全性。
基于开源框架Spring AI Alibaba快速构建Java应用
|
27天前
|
前端开发 Java 数据库连接
Spring 框架:Java 开发者的春天
Spring 框架是一个功能强大的开源框架,主要用于简化 Java 企业级应用的开发,由被称为“Spring 之父”的 Rod Johnson 于 2002 年提出并创立,并由Pivotal团队维护。
43 1
Spring 框架:Java 开发者的春天
|
27天前
|
Java 数据库连接 开发者
Spring 框架:Java 开发者的春天
【10月更文挑战第27天】Spring 框架由 Rod Johnson 在 2002 年创建,旨在解决 Java 企业级开发中的复杂性问题。它通过控制反转(IOC)和面向切面的编程(AOP)等核心机制,提供了轻量级的容器和丰富的功能,支持 Web 开发、数据访问等领域,显著提高了开发效率和应用的可维护性。Spring 拥有强大的社区支持和丰富的生态系统,是 Java 开发不可或缺的工具。
|
27天前
|
JSON Java Maven
实现Java Spring Boot FCM推送教程
本指南介绍了如何在Spring Boot项目中集成Firebase云消息服务(FCM),包括创建项目、添加依赖、配置服务账户密钥、编写推送服务类以及发送消息等步骤,帮助开发者快速实现推送通知功能。
66 2
|
1月前
|
存储 人工智能 Java
将 Spring AI 与 LLM 结合使用以生成 Java 测试
AIDocumentLibraryChat 项目通过 GitHub URL 为指定的 Java 类生成测试代码,支持 granite-code 和 deepseek-coder-v2 模型。项目包括控制器、服务和配置,能处理源代码解析、依赖加载及测试代码生成,旨在评估 LLM 对开发测试的支持能力。
36 1
|
1月前
|
Java BI 调度
Java Spring的定时任务的配置和使用
遵循上述步骤,你就可以在Spring应用中轻松地配置和使用定时任务,满足各种定时处理需求。
125 1
|
14天前
|
安全 Java 测试技术
Java开发必读,谈谈对Spring IOC与AOP的理解
Spring的IOC和AOP机制通过依赖注入和横切关注点的分离,大大提高了代码的模块化和可维护性。IOC使得对象的创建和管理变得灵活可控,降低了对象之间的耦合度;AOP则通过动态代理机制实现了横切关注点的集中管理,减少了重复代码。理解和掌握这两个核心概念,是高效使用Spring框架的关键。希望本文对你深入理解Spring的IOC和AOP有所帮助。
28 0
|
1月前
|
Java Apache Maven
Java/Spring项目的包开头为什么是com?
本文介绍了 Maven 项目的初始结构,并详细解释了 Java 包命名惯例中的域名反转规则。通过域名反转(如 `com.example`),可以确保包名的唯一性,避免命名冲突,提高代码的可读性和逻辑分层。文章还讨论了域名反转的好处,包括避免命名冲突、全球唯一性、提高代码可读性和逻辑分层。最后,作者提出了一个关于包名的问题,引发读者思考。
Java/Spring项目的包开头为什么是com?
|
1月前
|
存储 Java API
如何使用 Java 记录简化 Spring Data 中的数据实体
如何使用 Java 记录简化 Spring Data 中的数据实体
36 9
下一篇
无影云桌面