1. 程式人生 > >Mybatis根據配置檔案獲取session(多資料來源)

Mybatis根據配置檔案獲取session(多資料來源)

1.config.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>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <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://192.168.15.10:3306/db" /> <property name="username" value="123" /> <property name="password" value="123" /> </dataSource> </environment> </environments> <mappers> <mapper class
="com.TestMyBatis.hr_checkinMapper"></mapper> </mappers> </configuration>

2.config-wx.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
> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <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://192.168.37.1:3306/DB" /> <property name="username" value="root" /> <property name="password" value="mysql" /> </dataSource> </environment> </environments> <mappers> <mapper class="com.TestMyBatis.userMapper"></mapper> </mappers> </configuration>
View Code

3.hr_checkinMapper.java

package com.TestMyBatis;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;


public interface hr_checkinMapper {

    @Select(" select count(*) from kq_checkin")
    public  int getCount();
}
View Code

4.uc_userMapper.java

package com.TestMyBatis;

import org.apache.ibatis.annotations.Select;

public interface uc_userMapper {

    @Select("select count(*) from uc_user")
    public  int getUcUserCount();
}
View Code

6.SessionManager.java

package com.TestMyBatis;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class SessionManager {

    public static   SqlSession  getSession(String configName){
//        String resource = Program.class.getResource("")+"config.xml";
//        System.out.println(resource);
        InputStream inputStream = Program.class.getResourceAsStream(configName);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session=sqlSessionFactory.openSession();
        return  session;
    }
}
View Code

7.program

package com.TestMyBatis;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;

public class Program {

    public static  void main(String []args) throws IOException,InterruptedException,SQLException {

        //xml方式生成sqlsessionfactory

        SqlSession session=SessionManager.getSession("config.xml");
       hr_checkinMapper mapper=session.getMapper(hr_checkinMapper.class);
       int count=mapper.getCount();
       System.out.println(count);

        SqlSession sessionUc=SessionManager.getSession("config-wx.xml");
        uc_userMapper userMapper=sessionUc.getMapper(uc_userMapper.class);
        int count2=userMapper.getUcUserCount();
        System.out.println(count2);
    }
}
View Code

Test.class.getresource("")  Test 的所在目錄

Test.getClassloader.getresource("")  classpath目錄