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
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<?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>
3.hr_checkinMapper.java
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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(); }
4.uc_userMapper.java
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.TestMyBatis; import org.apache.ibatis.annotations.Select; public interface uc_userMapper { @Select("select count(*) from uc_user") public int getUcUserCount(); }
6.SessionManager.java
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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; } }
7.program
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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); } }
Test.class.getresource("") Test 的所在目录
Test.getClassloader.getresource("") classpath目录