ibatis如何连接多少个数据库?

情况是这样的,我有数据库1和数据库2,我现在要连接这两个数据库
给数据库1中的表A插入一条数据,然后再往数据库2中的表B插入一条数据
哪位高手能指导小弟如何配置多个数据库啊?先谢谢了
[b]问题补充:[/b]
蔡华江 (高级程序员):不用spring就不能解决这个问题吗?

3个回答

在程序中需要连接多个数据源

解决方案有几个

使用ibatis自带的Dao Framework来构件

一个dao.xml对应多个sql-map-config.xml,但是对应一个数据源

sql-map-config1.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">











sql-map-config2.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">











dao1.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE daoConfig
PUBLIC "-//ibatis.apache.org//DTD DAO Configuration 2.0//EN"
"http://ibatis.apache.org/dtd/dao-2.dtd">



value="com/ibatis/nicholas/persistence/sqlmap/sql-map-config1.xml" />

implementation="com.ibatis.nicholas.persistence.daoimpl.UserDaoImpl" />

dao2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE daoConfig
PUBLIC "-//ibatis.apache.org//DTD DAO Configuration 2.0//EN"
"http://ibatis.apache.org/dtd/dao-2.dtd">



value="com/ibatis/nicholas/persistence/sqlmap/sql-map-config2.xml" />

implementation="com.ibatis.nicholas.persistence.daoimpl.UserDaoImpl" />

DaoConfig.java:

import com.ibatis.common.resources.Resources;
import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.DaoManagerBuilder;
import java.io.Reader;
import java.util.Properties;
public class DaoConfig {
private static final String resource1 = "com/ibatis/nicholas/persistence/dao1.xml";
private static final String resource2 = "com/ibatis/nicholas/persistence/dao1.xml";
private static final DaoManager daoManager1;
private static final DaoManager daoManager2;
static {
try {
daoManager1 = newDaoManager1(null);
daoManager2 = newDaoManager2(null);
Properties props = Resources
.getResourceAsProperties("properties/database.properties");
} catch (Exception e) {
throw new RuntimeException("Description. Cause: " + e, e);
}
}
public static DaoManager getDaoManager1() {
return daoManager1;
}
public static DaoManager getDaoManager2() {
return daoManager2;
}
public static DaoManager newDaoManager1(Properties props) {
try {
Reader reader = Resources.getResourceAsReader(resource1);
return DaoManagerBuilder.buildDaoManager(reader, props);
} catch (Exception e) {
throw new RuntimeException(
"Could not initialize DaoConfig. Cause: " + e, e);
}
}
public static DaoManager newDaoManager2(Properties props) {
try {
Reader reader = Resources.getResourceAsReader(resource2);
return DaoManagerBuilder.buildDaoManager(reader, props);
} catch (Exception e) {
throw new RuntimeException(
"Could not initialize DaoConfig. Cause: " + e, e);
}
}
}

然后在你的service中通过DaoConfig.java来取到相对应的daoManager...
实现多数据源

不过ibatis已经停止对DaoFramework的更新,并且建议我们使用spring的daoframework,所以下一篇将是将ibatis和spring的结合。

扩展 iBatis 以透明支持多种数据库
http://blog.163.com/xinu_com/blog/static/1415875200822911135813/

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问