首页 技术 正文
技术 2022年11月12日
0 收藏 548 点赞 2,322 浏览 2538 个字

本篇文章,可乐将为大家介绍通过接口代理的方式去执行SQL操作。话不多说,直接上图:

其实无论哪种方式,我们最终是需要找到对应的 SQL 语句,接口代理的方式就是通过 【包名.方法名】 的方式,去找到 xxxMapper.xml 文件中的 SQL 语句。

很明显,通过动态代理的方式,我们能够实现该功能。下面,可乐将为大家手撸一个 Mybatis 的接口代理。

1、创建接口

package com.itcoke.mapperproxy;import com.itcoke.bean.Person;public interface PersonMapper {    Person selectPersonById(Long pid);
}

2、创建代理类

package com.itcoke.mapperproxy;import org.apache.ibatis.session.SqlSession;import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;public class MapperProxyHandler implements InvocationHandler {
private SqlSession sqlSession;
private Class<?> targetInterface; public MapperProxyHandler(SqlSession sqlSession,Class<?> targetInterface){
this.sqlSession = sqlSession;
this.targetInterface = targetInterface;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String className = targetInterface.getName();
String methodName = method.getName();
String statement = className + "." + methodName; return sqlSession.selectOne(statement,args[0]);
}
}

3、创建代理工厂类

package com.itcoke.mapperproxy;import java.lang.reflect.Proxy;public class MapperProxyFactory {
private Class<?> targetInterface; public MapperProxyFactory(Class<?> targetInterface){
this.targetInterface = targetInterface;
} public Object newInstance(MapperProxyHandler handler){
return Proxy.newProxyInstance(targetInterface.getClassLoader(),
new Class[]{targetInterface},
handler);
}
}

4、创建测试类

package com.itcoke.mapperproxy;import com.itcoke.bean.Person;
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;public class MapperProxyTest { public static void main(String[] args) {
// 1、获取目标接口对象
Class<?> targetInterface = PersonMapper.class;
// 2、获取 SqlSession 对象
SqlSession sqlSession = getSqlSession();
MapperProxyHandler proxyHandler = new MapperProxyHandler(sqlSession,targetInterface);
MapperProxyFactory mapperProxyFactory = new MapperProxyFactory(PersonMapper.class);
PersonMapper personMapper = (PersonMapper)mapperProxyFactory.newInstance(proxyHandler);
Person person = personMapper.selectPersonById(1L);
System.out.println(person);
} public static SqlSession getSqlSession() {
//定义mybatis全局配置文件
String resource = "mybatis-config.xml";
//加载 mybatis 全局配置文件
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
//构建sqlSession的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
return sessionFactory.openSession();
}
}

5、总结

其实 Mybatis 内部实现方式大体上和上面差不多,在加入一些类型处理器,其实就是一个简易版本的 Mybatis

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,997
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,356
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,139
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848