cn.pomit.mybatis.ProxyHandlerFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mybatis-proxy Show documentation
Show all versions of mybatis-proxy Show documentation
easily use mybatis on non-spring project
The newest version!
package cn.pomit.mybatis;
import java.lang.reflect.Proxy;
import cn.pomit.mybatis.configuration.MybatisConfiguration;
import cn.pomit.mybatis.sqlsession.SqlSessionTemplate;
import cn.pomit.mybatis.transaction.TransactionProxy;
public class ProxyHandlerFactory {
private static SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(MybatisConfiguration.getSqlSessionFactory());;
/**
* 获取mapper对象
* @param cls mapper接口
* @return mapper对象
*/
public static T getMapper(Class cls) {
return sqlSessionTemplate.getMapper(cls);
}
/**
* 获取事务处理代理(使用jdk动态代理)
* @param cls 被代理类
* @return 代理对象
*/
public static T getTransaction(Class extends T> cls){
TransactionProxy transactionProxy = new TransactionProxy(cls, sqlSessionTemplate);
return (T) Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), transactionProxy);
}
}