// 配置文件
// 第一个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
this.batchInsert("insertTestinfo", list);
}
public void insertTestinfo(TestInfo instance) {
this.getSqlSession().insert("insertTestinfo", instance);
}
public List
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
this.batchInsert("insertTestinfo", list);
}
public void insertTestinfo(TestInfo instance) {
this.getSqlSession().insert("insertTestinfo", instance);
}
public List
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 INTO TESTINFO (
TESTID,
TESTNM,
PASSWD,
ROLEID,
EMAIL,
MOBILE,
REMARK
) VALUES (
#{testid},
#{testnm},
#{passwd},
#{roleid},
#{email},
#{mobile},
#{remark}
)
// 这样的话不同的业务场合就可以访问不同数据库的方法了,如下:
public class TestServiceImpl extends TestService {
@Autowired
private TestDaoOne testDaoOne;
@Autowired
private void TestDaoTwo testDaoTwo;
//满足业务条件
if(true){
testDaoOne.deleteTestinfo();
}else{
testDaoTwo.deleteTestinfo();
}
}