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目录

 

posted @ 2018-10-18 15:28  甜菜波波  阅读(373)  评论(0编辑  收藏  举报