一、技术背景
在项目中需要操作多个数据库,同时,需要保证操作的原子性,保证对多个数据库的操作一致性,而传统的JDBC事务被限定在一个单一的数据库连接中,故引用JTA,即Java Transaction API。JTA事务比JDBC事务更强大,一个JTA事务可以有多个数据源参与者。
现在,我们用spring+jta+mybatis来完成基本的测试框架。
二、技术实现
第一步:导入JOTM Jar包
JOTM (Java Open Transaction Manager),一个开源JTA实现,是由ObjectWeb协会开发的功能完整的独立的事务管理器。JOTM(ow2-jotm-dist-2.2.1-bin.tar.gz)下载地址:(自己百度jotm,老是被误封),解压tar包导入lib目录下的Jar包。或在Maven pom.xml中添加:
<dependency>
<groupId>org.ow2.jotm</groupId>
<artifactId>ow2-jotm-dist</artifactId>
<version>2.2.1</version>
<type>pom</type>
<scope>compile</scope>
</dependency>
第二步:创建JotmFactoryBean
spring为JOTM提供了一个org.springframework.transaction.jta.JotmFactoryBean支持类,通过该支持类可以方便地创建JOTM本地实例。但是spring3以后spring-tx.jar 没有JotmFactoryBean的支持,可以反编译spring2.5的spring-tx.jar,copy新建一个类。
import javax.naming.NamingException;
import javax.transaction.SystemException;
import org.objectweb.jotm.Current;
import org.objectweb.jotm.Jotm;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
public class JotmFactoryBean implements FactoryBean, DisposableBean {
private Current jotmCurrent;
private Jotm jotm;
public JotmFactoryBean() throws NamingException {
jotmCurrent = Current.getCurrent();
if (jotmCurrent == null) {
jotm = new Jotm(true, false);
jotmCurrent = Current.getCurrent();
}
}
public void setDefaultTimeout(int defaultTimeout) {
jotmCurrent.setDefaultTimeout(defaultTimeout);
try {
jotmCurrent.setTransactionTimeout(defaultTimeout);
} catch (SystemException ex) {
}
}
public Jotm getJotm() {
return jotm;
}
public Object getObject() {
return jotmCurrent;
}
public Class<? extends Current> getObjectType() {
return jotmCurrent.getClass();
}
public boolean isSingleton() {
return true;
}
public void destroy() {
if (jotm != null)
jotm.stop();
}
}
第三步:创建DAO
创建基于mybatis框架的DAO接口和实现类。
IBaseDao
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
/**
* Title:
* Description: Dao 接口类
*/
public interface IBaseDao {
/**
* 根据Id获取对象
*
* @param id 主键
* @param sqlId 脚本编号
* @return Object对象
*/
public Object queryForObject(int id, String sqlId);
/**
* 根据Id获取对象
*
* @param id 主键
* @param cls 返回的对象Class
* @param sqlId 脚本编号
* @return cls对应的类
*/
public <T> T queryForObject(int id, Class<T> cls, String sqlId);
/**
* 根据条件获取对象
*
* @param params 参数
* @param sqlId 脚本编号
* @return Object对象
*/
public Object queryForObject(Map<String, String> params, String sqlId);
/**
* 根据条件获取对象
*
* @param params 参数
* @param cls 返回的对象Class
* @param sqlId 脚本编号
* @return cls对应的类
*/
public <T> T queryForObject(Map<String, String> params, Class<T> cls, String sqlId);
/**
* 获取数据总条数
*
* @param params 参数
* @param sqlId 脚本编号
* @return 条数
*/
public int getTotalCount(Map<String, String> params, String sqlId);
/**
* 查询列表
*
* @param params 参数
* @param cls 返回的对象Class
* @param sqlId 脚本编号
* @return 列表<cls对应的类>
*/
public <T> List<T> queryForList(Map<String, String> params, Class<T> cls, String sqlId);
/**
* 查询列表
*
* @param params 参数
* @param sqlId 脚本编号
* @return 列表
*/
public List<Map<String, String>> queryForList(Map<String, String> params, String sqlId);
/**
* 修改数据
*
* @param object 待更新对象
* @param sqlId 脚本编号
* @return 条数
*/
public int update(Object object, String sqlId);
/**
* 插入数据
*
* @param sqlId 脚本编号
* @param object 待插入的对象
* @return 条数
*/
public int insert(Object object, String sqlId);
/**
* 删除数据
*
* @param id 主键
* @param sqlId 脚本编号
* @return 条数
*/
public int delete(int id, String sqlId);
/**
* 删除数据
*
* @param map 待删除的对象
* @param sqlId 脚本编号
* @return 条数
*/
public int delete(Map<String, String> map, String sqlId);
/**
* 获取sqlSession
* @return
*/
public SqlSession getSqlSession();
/**
* 设置sqlSession
* @param sqlSession
*/
public void setSqlSession(SqlSession sqlSession);
}
BaseDaoImpl
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import com.ai.dimensyn.dao.common.IBaseDao;
import com.ai.frame.util.ConvertUtil;
/**
* Title:
* Description:Dao接口实现类
*/
public class BaseDaoImpl implements IBaseDao {
private SqlSession sqlSession;
@Override
public SqlSession getSqlSession() {
return sqlSession;
}
@Override
public void setSqlSession(SqlSession sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public Object queryForObject(int id, String sqlId) {
return getSqlSession().selectOne(sqlId, id);
}
@Override
@SuppressWarnings("unchecked")
public <T> T queryForObject(int id, Class<T> cls, String sqlId) {
return (T) getSqlSession().selectOne(sqlId, id);
}
@Override
public Object queryForObject(Map<String, String> params, String sqlId) {
return getSqlSession().selectOne(sqlId, params);
}
@Override
@SuppressWarnings("unchecked")
public <T> T queryForObject(Map<String, String> params, Class<T> cls, String sqlId) {
return (T) getSqlSession().selectOne(sqlId, params);
}
@Override
public int getTotalCount(Map<String, String> params, String sqlId) {
return (Integer) getSqlSession().selectOne(sqlId, params);
}
@Override
public <T> List<T> queryForList(Map<String, String> params, Class<T> cls, String sqlId) {
return getSqlSession().selectList(sqlId, params);
}
@Override
public List<Map<String, String>> queryForList(Map<String, String> params, String sqlId) {
List<Map<String, Object>> list = getSqlSession().selectList(sqlId, params);
List<Map<String, String>> beans = ConvertUtil.convertSqlMap2JavaMap(list);
return beans;
}
@Override
public int update(Object object, String sqlId) {
return getSqlSession().update(sqlId, object);
}
@Override
public int insert(Object object, String sqlId) {
return (Integer) getSqlSession().insert(sqlId, object);
}
@Override
public int delete(int id, String sqlId) {
return getSqlSession().delete(sqlId, id);
}
@Override
public int delete(Map<String, String> params, String sqlId) {
return getSqlSession().delete(sqlId, params);
}
}
IBaseMDao
import java.util.List;
import java.util.Map;
/**
* Title:
* Description: 多数据源Dao接口类
*/
public interface IBaseMDao {
/**
* 根据Id获取对象
*
* @param id 主键
* @param sqlIds 脚本编号
* @return Object对象
*/
public Object[] queryForObject(int id, String... sqlIds);
/**
* 根据Id获取对象
*
* @param id 主键
* @param cls 返回的对象Class
* @param sqlIds 脚本编号
* @return cls对应的类
*/
public <T> T[] queryForObject(int id, Class<T> cls, String... sqlIds);
/**
* 根据条件获取对象
*
* @param params 参数
* @param sqlIds 脚本编号
* @return Object对象
*/
public Object[] queryForObject(Map<String, String> params, String... sqlIds);
/**
* 根据条件获取对象
*
* @param params 参数
* @param cls 返回的对象Class
* @param sqlIds 脚本编号
* @return cls对应的类
*/
public <T> T[] queryForObject(Map<String, String> params, Class<T> cls, String... sqlIds);
/**
* 获取数据总条数
*
* @param params 参数
* @param sqlIds 脚本编号
* @return 条数
*/
public int[] getTotalCount(Map<String, String> params, String... sqlIds);
/**
* 查询列表
*
* @param params 参数
* @param cls 返回的对象Class
* @param sqlIds 脚本编号
* @return 列表<cls对应的类>
*/
public <T> List<T>[] queryForList(Map<String, String> params, Class<T> cls, String... sqlIds);
/**
* 查询列表
*
* @param params 参数
* @param sqlIds 脚本编号
* @return 列表
*/
public List<Map<String, String>>[] queryForList(Map<String, String> params, String... sqlIds);
/**
* 修改数据
*
* @param object 待更新对象
* @param sqlIds 脚本编号
* @return 条数
*/
public int[] update(Object object, String... sqlIds);
/**
* 插入数据
*
* @param object 待插入的对象
* @param sqlIds 脚本编号
* @return 条数
*/
public int[] insert(Object object, String... sqlIds);
/**
* 删除数据
*
* @param id 主键
* @param sqlIds
* @return 条数
*/
public int[] delete(int id, String... sqlIds);
/**
* 删除数据
*
* @param params 参数
* @param sqlIds 脚本编号
* @return 条数
*/
public int[] delete(Map<String, String> params, String... sqlIds);
/**
* 先删数据,后插入
* @param params
* @param delSqlId
* @param insertSqlId
* @return
*/
public int[] executeDeleteInsert(Map<String, String> params, String delSqlId, String insertSqlId);
}
BaseMDaoImpl
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import javax.transaction.UserTransaction;
import org.apache.ibatis.session.SqlSession;
import org.springframework.transaction.jta.JtaTransactionManager;
/**
* Title:
* Description:多数据源Dao接口实现类
*/
public class BaseMDaoImpl implements IBaseMDao {
private static ResourceBundle resources;
private JtaTransactionManager transactionManager;
private IBaseDao baseDao = new BaseDaoImpl();
static {
resources = ResourceBundle.getBundle("config/mybaties");
}
/**
* dmcWeb SqlSession
*/
private SqlSession dmcWebSession;
/**
* dmcWeb DataSource key
*/
private String dmcWebSrcKey;
/**
* rpt_web SqlSession
*/
private SqlSession rptWebSession;
/**
* rpt_web DataSource key
*/
private String rptWebSrcKey;
/**
* idxWeb SqlSession
*/
private SqlSession idxWebSession;
/**
* idxWeb DataSource key
*/
private String idxWebSrcKey;
/**
* default DataSource key
*/
private String defaultSrcKey;
public JtaTransactionManager getTransactionManager() {
return transactionManager;
}
public void setTransactionManager(JtaTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
public SqlSession getDmcWebSession() {
return dmcWebSession;
}
public void setDmcWebSession(SqlSession dmcWebSession) {
this.dmcWebSession = dmcWebSession;
}
public String getDmcWebSrcKey() {
return dmcWebSrcKey;
}
public void setDmcWebSrcKey(String dmcWebSrcKey) {
this.dmcWebSrcKey = dmcWebSrcKey;
}
public SqlSession getIdxWebSession() {
return idxWebSession;
}
public void setIdxWebSession(SqlSession idxWebSession) {
this.idxWebSession = idxWebSession;
}
public String getIdxWebSrcKey() {
return idxWebSrcKey;
}
public void setIdxWebSrcKey(String idxWebSrcKey) {
this.idxWebSrcKey = idxWebSrcKey;
}
public SqlSession getRptWebSession() {
return rptWebSession;
}
public void setRptWebSession(SqlSession rptWebSession) {
this.rptWebSession = rptWebSession;
}
public String getRptWebSrcKey() {
return rptWebSrcKey;
}
public void setRptWebSrcKey(String rptWebSrcKey) {
this.rptWebSrcKey = rptWebSrcKey;
}
public String getDefaultSrcKey() {
return defaultSrcKey;
}
public void setDefaultSrcKey(String defaultSrcKey) {
this.defaultSrcKey = defaultSrcKey;
}
/**
* 获取sqlSession
* @param sqlId
* @return
*/
private SqlSession getSqlSession(String sqlId) {
String srcKey = null;
try {
srcKey = resources.getString(sqlId);
} catch (Exception e) {
}
if (null == srcKey) {
srcKey = defaultSrcKey;
}
if (dmcWebSrcKey.equals(srcKey)) {
return dmcWebSession;
} else if (idxWebSrcKey.equals(srcKey)) {
return idxWebSession;
} else if (rptWebSrcKey.equals(srcKey)) {
return rptWebSession;
} else {
throw new NullPointerException("SqlSession null: " + sqlId);
}
}
@Override
public Object[] queryForObject(int id, String... sqlIds) {
Object[] results = new Object[sqlIds.length];
for (int i = 0; i < sqlIds.length; i++) {
baseDao.setSqlSession(getSqlSession(sqlIds[i]));
results[i] = baseDao.queryForObject(id, sqlIds[i]);
}
return results;
}
@Override
@SuppressWarnings("unchecked")
public <T> T[] queryForObject(int id, Class<T> cls, String... sqlIds) {
T[] results = (T[])new Object[sqlIds.length];
for (int i = 0; i < sqlIds.length; i++) {
baseDao.setSqlSession(getSqlSession(sqlIds[i]));
results[i] = baseDao.queryForObject(id, cls, sqlIds[i]);
}
return results;
}
@Override
public Object[] queryForObject(Map<String, String> params, String... sqlIds) {
Object[] results = new Object[sqlIds.length];
for (int i = 0; i < sqlIds.length; i++) {
baseDao.setSqlSession(getSqlSession(sqlIds[i]));
results[i] = baseDao.queryForObject(params, sqlIds[i]);
}
return results;
}
@Override
@SuppressWarnings("unchecked")
public <T> T[] queryForObject(Map<String, String> params, Class<T> cls, String... sqlIds) {
T[] results = (T[])new Object[sqlIds.length];
for (int i = 0; i < sqlIds.length; i++) {
baseDao.setSqlSession(getSqlSession(sqlIds[i]));
results[i] = baseDao.queryForObject(params, cls, sqlIds[i]);
}
return results;
}
@Override
public int[] getTotalCount(Map<String, String> params, String... sqlIds) {
int[] results = new int[sqlIds.length];
for (int i = 0; i < sqlIds.length; i++) {
baseDao.setSqlSession(getSqlSession(sqlIds[i]));
results[i] = baseDao.getTotalCount(params, sqlIds[i]);
}
return results;
}
@Override
@SuppressWarnings("unchecked")
public <T> List<T>[] queryForList(Map<String, String> params, Class<T> cls, String... sqlIds) {
List<T>[] results = new ArrayList[sqlIds.length];
for (int i = 0; i < sqlIds.length; i++) {
baseDao.setSqlSession(getSqlSession(sqlIds[i]));
results[i] = baseDao.queryForList(params, cls, sqlIds[i]);
}
return results;
}
@Override
@SuppressWarnings("unchecked")
public List<Map<String, String>>[] queryForList(Map<String, String> params, String... sqlIds) {
List<Map<String, String>>[] results = new ArrayList[sqlIds.length];
for (int i = 0; i < sqlIds.length; i++) {
baseDao.setSqlSession(getSqlSession(sqlIds[i]));
//单独改造,满足根据 table&方法 共同决定切换数据源
System.out.println(sqlIds[i].substring(0, sqlIds[i].indexOf("-")));
results[i] = baseDao.queryForList(params, sqlIds[i].substring(0, sqlIds[i].indexOf("-")));
}
return results;
}
@Override
public int[] update(Object object, String... sqlIds) {
int[] results = new int[sqlIds.length];
UserTransaction transaction = this.transactionManager.getUserTransaction();
try {
transaction.begin();
for (int i = 0; i < sqlIds.length; i++) {
baseDao.setSqlSession(getSqlSession(sqlIds[i]));
results[i] = baseDao.update(object, sqlIds[i]);
}
transaction.commit();
} catch (Exception e1) {
try {
transaction.rollback();
} catch (Exception e2) {
throw new RuntimeException(e2);
}
throw new RuntimeException(e1);
}
return results;
}
@Override
public int[] insert(Object object, String... sqlIds) {
int[] results = new int[sqlIds.length];
UserTransaction transaction = this.transactionManager.getUserTransaction();
try {
transaction.begin();
for (int i = 0; i < sqlIds.length; i++) {
baseDao.setSqlSession(getSqlSession(sqlIds[i]));
results[i] = baseDao.insert(object, sqlIds[i]);
}
transaction.commit();
} catch (Exception e1) {
try {
transaction.rollback();
} catch (Exception e2) {
throw new RuntimeException(e2);
}
throw new RuntimeException(e1);
}
return results;
}
@Override
public int[] delete(int id, String... sqlIds) {
int[] results = new int[sqlIds.length];
UserTransaction transaction = this.transactionManager.getUserTransaction();
try {
transaction.begin();
for (int i = 0; i < sqlIds.length; i++) {
baseDao.setSqlSession(getSqlSession(sqlIds[i]));
results[i] = baseDao.delete(id, sqlIds[i]);
}
transaction.commit();
} catch (Exception e1) {
try {
transaction.rollback();
} catch (Exception e2) {
throw new RuntimeException(e2);
}
throw new RuntimeException(e1);
}
return results;
}
@Override
public int[] delete(Map<String, String> params, String... sqlIds) {
int[] results = new int[sqlIds.length];
UserTransaction transaction = this.transactionManager.getUserTransaction();
try {
transaction.begin();
for (int i = 0; i < sqlIds.length; i++) {
baseDao.setSqlSession(getSqlSession(sqlIds[i]));
results[i] = baseDao.delete(params, sqlIds[i]);
}
transaction.commit();
} catch (Exception e1) {
try {
transaction.rollback();
} catch (Exception e2) {
throw new RuntimeException(e2);
}
throw new RuntimeException(e1);
}
return results;
}
@Override
public int[] executeDeleteInsert(Map<String, String> params, String delSqlId, String insertSqlId) {
UserTransaction transaction = this.transactionManager.getUserTransaction();
try {
transaction.begin();
baseDao.setSqlSession(getSqlSession(delSqlId));
baseDao.delete(params, delSqlId.substring(0, delSqlId.indexOf("-")));
baseDao.setSqlSession(getSqlSession(insertSqlId));
//对sql进行;分隔 迭代插入
String[] strsOfSql = params.get(Constants.SQL).split(";");
Map<String, String> sqlMap = new HashMap<String, String>();
for (String sql : strsOfSql) {
sqlMap.put(Constants.SQL, sql);
baseDao.insert(sqlMap, insertSqlId.substring(0,insertSqlId.indexOf("-")));
sqlMap.clear();
}
transaction.commit();
} catch (Exception e1) {
try {
transaction.rollback();
} catch (Exception e2) {
throw new RuntimeException(e2);
}
throw new RuntimeException(e1);
}
return null;
}
}
第四步:XML配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!-- 配置注解扫描目录 -->
<!-- <context:component-scan base-package="com" /> -->
<!-- 引入注解 -->
<!-- <context:annotation-config /> -->
<bean id="SpringFactory" class="com.ai.dimensyn.util.SpringFactory" />
<bean id="jotm" class="com.ai.dimensyn.dynamicDataSoure.JotmFactoryBean" />
<!-- JTA事务管理器 -->
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction" ref="jotm"></property>
</bean>
<!-- -->
<bean id="dmcWebSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="dmcWebSessionFactory" />
</bean>
<bean id="idxWebSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="idxWebSessionFactory" />
</bean>
<bean id="rptWebSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="rptWebSessionFactory" />
</bean>
<bean id="dmcWebSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:orm/Configuration.xml"></property>
<property name="dataSource" ref="dataSourceDmcWeb"></property>
</bean>
<bean id="idxWebSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:orm/Configuration.xml"></property>
<property name="dataSource" ref="dataSourceIdxWeb"></property>
</bean>
<bean id="rptWebSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:orm/Configuration.xml"></property>
<property name="dataSource" ref="dataSourceRptWeb"></property>
</bean>
<!-- XAPool配置,内部包含了一个XA数据源 -->
<bean id="dataSourceDmcWeb" 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="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://ip:port/dmc?useUnicode=true&characterEncoding=utf-8" />
<property name="user" value="xxx" />
<property name="password" value="xxx" />
</bean>
</property>
<property name="user" value="xxx" />
<property name="password" value="xxx" />
</bean>
<!-- XAPool配置,内部包含了一个XA数据源 -->
<bean id="dataSourceIdxWeb" 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="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://ip:port/idx_web?useUnicode=true&characterEncoding=utf-8" />
<property name="user" value="xxx" />
<property name="password" value="xxx" />
</bean>
</property>
<property name="user" value="xxx" />
<property name="password" value="xxx" />
</bean>
<!-- XAPool配置,内部包含了一个XA数据源 -->
<bean id="dataSourceRptWeb" 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="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://v/rpt_web?useUnicode=true&characterEncoding=utf-8" />
<property name="user" value="xxx" />
<property name="password" value="xxx" />
</bean>
</property>
<property name="user" value="xxx" />
<property name="password" value="xxx" />
</bean>
<bean id="baseMDao" class="com.ai.dimensyn.dao.common.impl.BaseMDaoImpl" >
<property name="transactionManager" ref="transactionManager"/>
<property name="dmcWebSession" ref="dmcWebSession"/>
<property name="dmcWebSrcKey" value="dmcWeb"></property>
<property name="idxWebSession" ref="idxWebSession"/>
<property name="idxWebSrcKey" value="indexWeb"></property>
<property name="rptWebSession" ref="rptWebSession"/>
<property name="rptWebSrcKey" value="reportWeb"></property>
<property name="defaultSrcKey" value="dmcWeb "></property>
</bean>
</beans>
SpringFactory:
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
public class SpringFactory implements BeanFactoryAware {
private static BeanFactory beanFactory;
// private static ApplicationContext context;
public void setBeanFactory(BeanFactory factory) throws BeansException {
this.beanFactory = factory;
}
/**
* 根据beanName名字取得bean
*
* @param beanName
* @return
*/
public static <T> T getBean(String beanName) {
if (null != beanFactory) {
return (T) beanFactory.getBean(beanName);
}
return null;
}
}
第五步:测试
在后台测试分布式事务管理:
private IBaseMDao baseMDao = SpringFactory.getBean("baseMDao");
public void testBaseMDao() {
try {
Map<String, String> params = new HashMap<String, String>();
params.put("table_tb",table);
params.put(Constants.SQL,sql);
baseMDao.executeDeleteInsert(params,"dimensyn.deleteDimenTable"+"-"+table, "dimensyn.insertDimenTable"+"-"+table);
} catch (Exception e) {
e.printStackTrace();
}
}
public IBaseMDao getBaseMDao() {
return baseMDao;
}
public void setBaseMDao(IBaseMDao baseMDao) {
this.baseMDao = baseMDao;
}
在indexWeb、dmcWeb库中创建测试表,并插入测试数据,配置:
<mapper namespace="dimensyn">
<select id="queryDimenColName" parameterType="java.util.Map" resultType="java.util.Map">
select DISTINCT column_name from information_schema.columns where table_name = #{table_tb,jdbcType=VARCHAR}
</select>
<select id="queryDimenTable" parameterType="java.util.Map" resultType="java.util.Map">
select * from ${table_tb}
</select>
<delete id="deleteDimenTable" parameterType="java.util.Map">
delete from ${table_tb}
</delete>
<insert id="insertDimenTable" parameterType="java.util.Map">
<![CDATA[${sql}]]>
</insert>
</mapper>
Configuration.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 配置映射文件 -->
<mappers>
<mapper resource="orm/mapping/dimensynMapper.xml"/>
</mappers>
</configuration>
mybaties.properties:
dimensyn.deleteDimenTable-testtable_xuchun=indexWeb
dimensyn.insertDimenTable-testtable_xuchun=dmcWeb