一、设置Mybatis配置文件
mybatis-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>
<!-- 这个配置使全局的映射器启用或禁用缓存。默认true -->
<setting name="cacheEnabled" value="true"/>
<!-- 全局启用或禁用延迟加载。默认true -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 允许或不允许多种结果集从一个单独的语句中返回。默认true -->
<setting name="multipleResultSetsEnabled" value="true"/>
</settings>
<!-- 类型别名是为Java类型命名一个短的名字。它只和XML配置有关,只用来减少类完全限定名的多余部分。 -->
<typeAliases>
<typeAlias alias="Student" type="cn.test.entity.Student"/>
<typeAlias alias="Group" type="cn.test.entity.Group"/>
</typeAliases>
<environments default="mySql">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!-- 在任意时间存在的活动(也就是正在使用)连接的数量。默认值:10 -->
<property name="poolMaximumActiveConnections" value="10"/>
<!-- 任意时间存在的空闲连接数 -->
<property name="poolMaximumIdleConnections" value="5"/>
<!-- 在被强制返回之前,池中连接被检查的时间。默认值:20000毫秒(也就是20秒) -->
<property name="poolMaximumCheckoutTime" value="20000"/>
<property name="poolTimeToWait" value="20000"/>
</dataSource>
</environment>
<environment id="mySql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
<environment id="oracle">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL"/>
<property name="username" value="mm"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="cn/test/data/StudentMapper.xml"/>
<mapper resource="cn/test/data/GroupMapper.xml"/>
</mappers>
</configuration>
二、写工具类MybatisUtils
package cn.test.utils;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MyBatisUtils {
private static final String RESOURCE = "mybatis-config.xml";
private static SqlSessionFactory sqlSessionFactory;
private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();
static{
try {
Reader reader = Resources.getResourceAsReader(RESOURCE);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
sqlSessionFactory = builder.build(reader);
} catch (IOException e) {
e.printStackTrace();
throw new ExceptionInInitializerError("初始化MyBatis错误!请检查配置文件或者数据库");
}
}
//得到一个SqlSession
public static SqlSession getSession(){
SqlSession session = threadLocal.get();
if(session==null){
session = sqlSessionFactory.openSession();
threadLocal.set(session);
}
return session;
}
//关闭当前线程的SqlSession
public static void closeSession(){
SqlSession session = threadLocal.get();
threadLocal.set(null);
if(session!=null){
session.close();
}
}
}