zoukankan      html  css  js  c++  java
  • 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目录

  • 相关阅读:
    Oracle 实现自动递增的功能
    解决升级chrome导致“Silverlight ”插件失效
    解决笔记:转载篇
    MSP432在CCS上新建工程,导入固件库
    操作系统复习文档
    数据结构
    数据结构
    蓝桥
    ACM山东工商 数据结构与算法 第3章 双向栈的操作
    ACM山东工商 栈和队列算法练习
  • 原文地址:https://www.cnblogs.com/tiancai/p/9810773.html
Copyright © 2011-2022 走看看