1 package edu.nf.utils;
2
3 import edu.nf.dao.UserDao;
4 import edu.nf.dao.UserInvocationhandler;
5 import org.apache.ibatis.session.SqlSession;
6
7 import java.lang.reflect.InvocationHandler;
8 import java.lang.reflect.Proxy;
9
10 /**
11 * 代理工具
12 * @Author LQY
13 * @Date 2018/10/15
14 */
15 public class ProxyUtil {
16 /**
17 * 在使用mybatis前提下,获取操作数据库dao的实现类的代理对象
18 * @param clazz dao接口的class对象
19 * @return
20 */
21 public static Object getProxy(Class<?> clazz){
22 SqlSession sqlSession = MyBatisUtil.getSqlSession();
23 Object dao = sqlSession.getMapper(clazz);
24 try {
25 InvocationHandler handler = new DaoInvocationhandler(dao);
26 return Proxy.newProxyInstance(clazz.getClassLoader(), dao.getClass().getInterfaces(), handler);
27 } catch (Exception e) {
28 e.printStackTrace();
29 }
30 return null;
31 }
32 }
![]()
package edu.nf.dao;
import edu.nf.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
/**
* 回调处理器
* @Author LQY
* @Date 2018/10/15
*/
public class DaoInvocationhandler implements InvocationHandler{
private Object target ;
public UserInvocationhandler(Object target){
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object obj = null;
SqlSession sqlSession = MyBatisUtil.getSqlSession();
try {
obj = method.invoke(target,args);
//提交事务
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
//回滚事务
sqlSession.rollback();
}finally {
//释放SqlSession
sqlSession.close();
}
return obj;
}
}
View Code