1、配置文件
<?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>
<typeAliases>
<typeAlias type="xxx.xxx.xxx.WeixinPay" alias="WeixinPay" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.131:3306/weixin_pay" />
<property name="username" value="username" />
<property name="password" value="password" />
</dataSource>
</environment>
<environment id="sellid">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/flow?useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="username" />
<property name="password" value="password" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="xxx/xxx/xxx/WeixinPay.xml" />
<mapper resource="xxx/xxx/xxx/SellId.xml" />
</mappers>
</configuration>
2、使用方法:可以通过environment id来使用不同的数据源。
public static SqlSessionFactory getSessionFactory(String abc) {
String resource = "config.xml";
try {
if("development".equals(abc)){
Reader readerFrom;
readerFrom = getReader(resource);
SqlSessionFactory sessionFactoryFrom = getSqlSessionFactory(readerFrom, "development");
return sessionFactoryFrom;
}else if("sellid".equals(abc)){
Reader readerTo = getReader(resource);
SqlSessionFactory sessionFactoryTo = getSqlSessionFactory(readerTo, "sellid");
return sessionFactoryTo;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}