首先在mysql数据库中建立数据库test,表functions;在sqlserver2000中建立数据库test,表sfunctions
连个表字段都一样为id,pid,url,functionname.
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 定义数据源Bean,使用C3P0数据源实现 -->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- 指定连接数据库的驱动 -->
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<!-- 指定连接数据库的URL -->
<property name="jdbcUrl" value="jdbc:mysql://localhost/lams" />
<!-- 指定连接数据库的用户名 -->
<property name="user" value="root" />
<!-- 指定连接数据库的密码 -->
<property name="password" value="root" />
<!-- 指定连接数据库连接池的最大连接数 -->
<property name="maxPoolSize" value="20" />
<!-- 指定连接数据库连接池的最小连接数 -->
<property name="minPoolSize" value="1" />
<!-- 指定连接数据库连接池的初始化连接数 -->
<property name="initialPoolSize" value="1" />
<!-- 指定连接数据库连接池的连接的最大空闲时间 -->
<property name="maxIdleTime" value="20" />
</bean>
<!-- 定义Hibernate的SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 依赖注入上面定义的数据源dataSource -->
<property name="dataSource" ref="dataSource" />
<!-- 注册Hibernate的ORM映射文件 -->
<property name="mappingResources">
<list>
<value>com/eportal/ORM/Functions.hbm.xml</value>
</list>
</property>
<!-- 设置Hibernate的相关属性 -->
<property name="hibernateProperties">
<props>
<!-- 设置Hibernate的数据库方言 -->
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<!-- 设置Hibernate是否在控制台输出SQL语句,开发调试阶段通常设为true -->
<prop key="hibernate.show_sql">true</prop>
<!-- 设置Hibernate一个提交批次中的最大SQL语句数 -->
<prop key="hibernate.jdbc.batch_size">50</prop>
</props>
</property>
</bean>
<!--定义Hibernate的事务管理器HibernateTransactionManager -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- 依赖注入上面定义的sessionFactory -->
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--定义Spring的事务拦截器TransactionInterceptor -->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<!-- 依赖注入上面定义的事务管理器transactionManager -->
<property name="transactionManager" ref="transactionManager" />
<!-- 定义需要进行事务拦截的方法及所采用的事务控制类型 -->
<property name="transactionAttributes">
<props>
<!-- 以browse、list、load、get及is开头的所有方法采用只读型事务控制类型 -->
<prop key="browse*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="is*">PROPAGATION_REQUIRED,readOnly</prop>
<!-- 所有方法均进行事务控制,如果当前没有事务,则新建一个事务 -->
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 定义BeanNameAutoProxyCreatorf进行Spring的事务处理-->
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<!-- 针对指定的bean自动生成业务代理 -->
<property name="beanNames">
<list>
<value>functionService</value>
</list>
</property>
<!-- 这个属性为true时,表示被代理的是目标类本身而不是目标类的接口 -->
<property name="proxyTargetClass">
<value>true</value>
</property>
<!-- 依赖注入上面定义的事务拦截器transactionInterceptor -->
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<!-- 定义连接 GPS 服务器数据库 -->
<bean id="dataSourceGPS"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass"
value="net.sourceforge.jtds.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:jtds:sqlserver://127.0.0.1:1433:DatabaseName=test" />
<property name="user" value="sa" />
<property name="password" value="123" />
<property name="maxPoolSize" value="20" />
<property name="minPoolSize" value="1" />
<property name="initialPoolSize" value="1" />
<property name="maxIdleTime" value="20" />
</bean>
<!--定义了Hibernate的SessionFactoryGPS -->
<bean id="sessionFactoryGPS"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSourceGPS" />
<property name="mappingResources">
<list>
<value>com/eportal/ORM/Sfunctions.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
</bean>
<bean id="transactionManagerGPS"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactoryGPS" />
</bean>
<bean id="transactionInterceptorGPS"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<!-- 事务拦截器bean需要依赖注入一个事务管理器 -->
<property name="transactionManager" ref="transactionManagerGPS" />
<property name="transactionAttributes">
<!-- 下面定义事务传播属性-->
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="functionDao" class="com.eportal.DAO.FunctionDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="functionService"
class="com.eportal.service.FunctionServiceImpl">
<property name="functionDao" ref="functionDao" />
</bean>
<bean id="sfunctionDao" class="com.eportal.DAO.sFunctionDaoImpl">
<property name="sessionFactory" ref="sessionFactoryGPS" />
</bean>
<bean id="sfunctionService"
class="com.eportal.service.sFunctionServiceImpl">
<property name="sfunctionDao" ref="sfunctionDao" />
</bean>
<bean id="userAction" class="com.eportal.struts.action.UserAction"
scope="prototype">
<property name="functionService" ref="functionService" />
<property name="sfunctionService" ref="sfunctionService" />
</bean>
</beans>
UserAction.java中的方法如下:
public String getFunctionList(){
List list1 = functionService.getFunctionList();
System.out.println("AAAAAAAAAAAAA"+list1.size());
Functions function = (Functions)list1.get(0);
Sfunctions sfunctions = new Sfunctions();
sfunctions.setPid(function.getPid());
sfunctions.setUrl(function.getUrl());
sfunctions.setFunctionname(function.getFunctionname());
sfunctionService.insert(sfunctions);
List list2 = sfunctionService.getFunctionList();
System.out.println("BBBBBBBBBBBBB"+list2.size());
return SUCCESS;
}