主配置文件
<?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>
<!-- 加载jdbc属性文件 -->
<properties resource="jdbc.properties"></properties>
<!-- 定义别名 ,用于查询返回值-->
<typeAliases>
<typeAlias type="com.bjpn.domain.Student" alias="student"/>
</typeAliases>
<!-- 配置数据库环境 -->
<environments default="mysql">
<environment id="mysql">
<transactionManager type="jdbc"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<!-- 配置映射文件 -->
<mappers>
<mapper resource="com/bjpn/dao/studentDao.xml"/>
</mappers>
</configuration>
sql映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="student">
<insert id="insertStudent">
insert into t_student(name,age) values(#{name},#{age})
</insert>
<delete id="deleteStudent">
delete from t_student where id = #{id}
</delete>
<select id="selectStudent" resultType="student">
select * from t_student
</select>
<select id="selectOneStudent" resultType="student">
select * from t_student where id = #{id}
</select>
<update id="updateStudent">
update t_student set name = #{name},age = #{age} where id = #{id}
</update>
</mapper>
获得事务工具类
package com.bjpn.util;
import java.io.IOException;
import java.io.InputStream;
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 SqlSessionUtil {
private static SqlSessionFactory factory = null;
private static ThreadLocal<SqlSession> local = new ThreadLocal<>();
static {
InputStream in = null;
try {
in = Resources.getResourceAsStream("mybatis.xml");
} catch (IOException e) {
e.printStackTrace();
}
factory = new SqlSessionFactoryBuilder().build(in);
}
public static SqlSession getSession() {
SqlSession session = local.get();
if (session == null) {
// 开启手动提交的事务
session = factory.openSession();
local.set(session);
}
return session;
}
public static void close(SqlSession session) {
if (session != null) {
local.remove();
session.close();
}
}
}
动态代理工具类
package com.bjpn.util;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.apache.ibatis.session.SqlSession;
public class ServiceProxyUtil implements InvocationHandler {
private Object targetObj;
//创建动态代理对象
//参数1 目标对象的类加载器
//参数2 目标对象的所有父接口的Class
//参数3 某个InvocationHandler 的接口对象,作用是:通知虚拟机这个代理对象在执行方法时要调用哪个类中的invoke方法
public Object newProxyObject(Object targetObj) {
this.targetObj = targetObj;
return Proxy.newProxyInstance(targetObj.getClass().getClassLoader(), targetObj.getClass().getInterfaces(),
this);
}
/*
* 动态代理的通用代理方法,当代理对象调用任何一个方法时都会自动执行这个invoke方法.JVM完成自动调用这个方法
*
* 参数1 代理对象的引用,注意:这个对象最好不要使用
* 参数2 目标方法的反射引用对象
* 参数3 执行目标方法时所需要的参数列表,如果目标方法没有参数那么这个数组的长度为0
* 返回值Object 为目标方法的返回值数据,如果目标方法返回值类型为void 那么这个方法应该返回 null
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = null;
SqlSession session = SqlSessionUtil.getSession();
result = method.invoke(targetObj, args);
session.commit();
SqlSessionUtil.close(session);
return result;
}
}