(文章轉(zhuǎn)自霍格沃茲測(cè)試學(xué)院優(yōu)先學(xué)員-月關(guān))
有時(shí)候因需求,要同時(shí)使用同一個(gè)地址中的多個(gè)數(shù)據(jù)庫(kù),這里以?xún)蓚€(gè)數(shù)據(jù)庫(kù)為例:
1.jdbc.properties文件中配置好不同的數(shù)據(jù)源連接參數(shù)
test.jdbc.driver=com.MySQL.jdbc.Driver test.jdbc.url=jdbc:mysql://192.168.x.10x:3306/Test1?characterEncoding=utf-8 test.jdbc.username=root test.jdbc.password=123 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://192.168.x.10x:3306/Test2?characterEncoding=utf-8 jdbc.username=root jdbc.password=123
2.分別創(chuàng)建兩個(gè)數(shù)據(jù)庫(kù)配置xml文件,修改各自對(duì)應(yīng)的property,最重要的一點(diǎn)是兩個(gè)xml文件的注冊(cè)對(duì)象的空間命名要不同
databaseConfig.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> <!-- 先加載內(nèi)部標(biāo)簽,再加載外部文件,名稱(chēng)一致時(shí),會(huì)替換內(nèi)容 --> <properties resource="jdbc.properties"/> <!-- 注冊(cè)對(duì)象的空間命名 --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <!-- 1.加載數(shù)據(jù)庫(kù)驅(qū)動(dòng) --> <property name="driver" value="${jdbc.driver}"/> <!-- 2.數(shù)據(jù)庫(kù)連接地址 --> <property name="url" value="${jdbc.url}"/> <!-- 數(shù)據(jù)庫(kù)用戶(hù)... --> <property name="username" value="${jdbc.username}"/> <!-- 數(shù)據(jù)庫(kù)密碼... --> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <!-- 注冊(cè)映射文件:JAVA對(duì)象與數(shù)據(jù)庫(kù)之間的xml文件路徑! --> <mAppers> <mapper resource="mapper/SQLMapper.xml"/> </mappers> </configuration>
databaseConfigTest.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> <!-- 先加載內(nèi)部標(biāo)簽,再加載外部文件,名稱(chēng)一致時(shí),會(huì)替換內(nèi)容 --> <properties resource="jdbc.properties"/> <!-- 注冊(cè)對(duì)象的空間命名 --> <environments default="test_development"> <environment id="test_development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <!-- 1.加載數(shù)據(jù)庫(kù)驅(qū)動(dòng) --> <property name="driver" value="${test.jdbc.driver}"/> <!-- 2.數(shù)據(jù)庫(kù)連接地址 --> <property name="url" value="${test.jdbc.url}"/> <!-- 數(shù)據(jù)庫(kù)用戶(hù)... --> <property name="username" value="${test.jdbc.username}"/> <!-- 數(shù)據(jù)庫(kù)密碼... --> <property name="password" value="${test.jdbc.password}"/> </dataSource> </environment> </environments> <!-- 注冊(cè)映射文件:java對(duì)象與數(shù)據(jù)庫(kù)之間的xml文件路徑! --> <mappers> <mapper resource="mapper/SQLMapper.xml"/> </mappers> </configuration>
3.根據(jù)業(yè)務(wù)需求分別創(chuàng)建不同的SqlsessionFactory對(duì)象就可以了,只需要將xml配置文件進(jìn)行替換即可
public static SqlSession getSqlSession(){ Reader reader; //獲取配置的資源文件 try { reader = Resources.getResourceAsReader("databaseConfig.xml"); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("找不到databaseConfig.xml文件"); } SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader); //sqlSession能夠執(zhí)行配置文件中的SQL語(yǔ)句 SqlSession sqlSession = factory.openSession(); return sqlSession; }
(文章來(lái)源于霍格沃茲測(cè)試學(xué)院)