spring+mybatis 根据业务场景访问不同数据库,读写分离

// 配置文件

<bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean">  

    <property name="dataSource" ref="DataSource1" />  
    <property name="configLocation" value="classpath:SqlMapConfig.xml" />  
</bean>  
<bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">  
    <property name="dataSource" ref="DataSource2" />  
    <property name="configLocation" value="classpath:SqlMapConfig.xml" />  
</bean>


// 第一个basedao
public class BaseDaoOne extends SqlSessionDaoSupport {  
      
    private static final Logger logger = Logger.getLogger(Thread  
            .currentThread().getStackTrace()[1].getClassName());  
      
    @Resource(name = "sqlSessionFactory1")  
    private SqlSessionFactory sqlSessionFactory;  
  
    private SqlSession batchSession;  
      
    @PostConstruct  
    public void SqlSessionFactory() {  
        super.setSqlSessionFactory(sqlSessionFactory);  
    }  
      
    public int batchInsert(String statement, List<?> list) {  
        batchSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);  
        int i = 0;  
        for(int cnt = list.size(); i < cnt; i++) {  
            batchSession.insert(statement, list.get(i));  
            if((i + 1) % Constants.BATCH_DEAL_NUM == 0) {//Constants.BATCH_DEAL_NUM为批量提交的条数  
                batchSession.flushStatements();  
            }  
        }  
        batchSession.flushStatements();  
        batchSession.close();  
        return i;  
    }  
      
    public int batchUpdate(String statement, List<?> list) {  
        batchSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);  
        int i = 0;  
        for(int cnt = list.size(); i < cnt; i++) {  
            batchSession.update(statement, list.get(i));  
            if((i + 1) % Constants.BATCH_DEAL_NUM == 0) {  
                batchSession.flushStatements();  
            }  
        }  
        batchSession.flushStatements();  
        batchSession.close();  
        return i;  
    }  
      
    public int batchDelete(String statement, List<?> list) {  
        batchSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);  
        int i = 0;  
        for(int cnt = list.size(); i < cnt; i++) {  
            batchSession.delete(statement, list.get(i));  
            if((i + 1) % Constants.BATCH_DEAL_NUM == 0) {  
                batchSession.flushStatements();  
            }  
        }  
        batchSession.flushStatements();  
        batchSession.close();  
        return i;  
    }  





// 第二个basedao
public class BaseDaoTwo extends SqlSessionDaoSupport {  
      
    private static final Logger logger = Logger.getLogger(Thread  
            .currentThread().getStackTrace()[1].getClassName());  
      
    @Resource(name = "sqlSessionFactory2")  
    private SqlSessionFactory sqlSessionFactory;  
  
    private SqlSession batchSession;  
      
    @PostConstruct  
    public void SqlSessionFactory() {  
        super.setSqlSessionFactory(sqlSessionFactory);  
    }  
      
    public int batchInsert(String statement, List<?> list) {  
        batchSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);  
        int i = 0;  
        for(int cnt = list.size(); i < cnt; i++) {  
            batchSession.insert(statement, list.get(i));  
            if((i + 1) % Constants.BATCH_DEAL_NUM == 0) {//Constants.BATCH_DEAL_NUM为批量提交的条数  
                batchSession.flushStatements();  
            }  
        }  
        batchSession.flushStatements();  
        batchSession.close();  
        return i;  
    }  
      
    public int batchUpdate(String statement, List<?> list) {  
        batchSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);  
        int i = 0;  
        for(int cnt = list.size(); i < cnt; i++) {  
            batchSession.update(statement, list.get(i));  
            if((i + 1) % Constants.BATCH_DEAL_NUM == 0) {  
                batchSession.flushStatements();  
            }  
        }  
        batchSession.flushStatements();  
        batchSession.close();  
        return i;  
    }  
      
    public int batchDelete(String statement, List<?> list) {  
        batchSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);  
        int i = 0;  
        for(int cnt = list.size(); i < cnt; i++) {  
            batchSession.delete(statement, list.get(i));  
            if((i + 1) % Constants.BATCH_DEAL_NUM == 0) {  
                batchSession.flushStatements();  
            }  
        }  
        batchSession.flushStatements();  
        batchSession.close();  
        return i;  
    }  



// dao1的实现类
@Service
public class TestDaoOneImpl extends BaseDaoOne implements TestDaoOne {  
  
    public void deleteTestinfo(Map<String, Object> index) {  
        this.getSqlSession().delete("deleteTestinfo", index);  
    }  
    // 批量插入  
    public void insertBatchTestinfo(List<TestInfo> list) {  
        this.batchInsert("insertTestinfo", list);  
    }  
  
    public void insertTestinfo(TestInfo instance) {  
        this.getSqlSession().insert("insertTestinfo", instance);  
    }  
  
    public List<TestInfo> selectTestinfos(Map<String, Object> index) {  
        return this.getSqlSession().selectList("selectTestinfos", index);  
    }  
    // 查询条数  
    public int selectTestinfosCount(Map<String, Object> index) {  
        return this.getSqlSession().selectOne("selectTestinfosCount", index);  
    }  
  
    public void updateTestinfo(Map<String, Object> index) {  
        this.getSqlSession().update("updateTestinfo", index);  
    }  
  
}  


// dao2的实现类
@Service
public class TestDaoTwoImpl extends BaseDaoTwo implements TestDaoTwo {  
  
    public void deleteTestinfo(Map<String, Object> index) {  
        this.getSqlSession().delete("deleteTestinfo", index);  
    }  
    // 批量插入  
    public void insertBatchTestinfo(List<TestInfo> list) {  
        this.batchInsert("insertTestinfo", list);  
    }  
  
    public void insertTestinfo(TestInfo instance) {  
        this.getSqlSession().insert("insertTestinfo", instance);  
    }  
  
    public List<TestInfo> selectTestinfos(Map<String, Object> index) {  
        return this.getSqlSession().selectList("selectTestinfos", index);  
    }  
    // 查询条数  
    public int selectTestinfosCount(Map<String, Object> index) {  
        return this.getSqlSession().selectOne("selectTestinfosCount", index);  
    }  
  
    public void updateTestinfo(Map<String, Object> index) {  
        this.getSqlSession().update("updateTestinfo", index);  
    }  
  
}  




// sql就举一个例子
<insert id="insertTestinfo" parameterType="TestInfo">  
    INSERT INTO TESTINFO (  
        TESTID,  
        TESTNM,  
        PASSWD,  
        ROLEID,  
        EMAIL,  
        MOBILE,  
        REMARK  
    ) VALUES (  
        #{testid},  
        #{testnm},  
        #{passwd},  
        #{roleid},  
        #{email},  
        #{mobile},  
        #{remark}  
    )  
</insert> 




// 这样的话不同的业务场合就可以访问不同数据库的方法了,如下:
public class TestServiceImpl extends TestService {


@Autowired
private TestDaoOne testDaoOne;

@Autowired
private void TestDaoTwo testDaoTwo;

//满足业务条件
if(true){
testDaoOne.deleteTestinfo();
}else{
testDaoTwo.deleteTestinfo();
}


}

转载于:https://my.oschina.net/sunchenbin/blog/632977

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值