如何进行Java Mybatis中的Mapper原理分析
发表于:2025-11-15 作者:千家信息网编辑
千家信息网最后更新 2025年11月15日,如何进行Java Mybatis中的Mapper原理分析,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。准备1.pom文件
千家信息网最后更新 2025年11月15日如何进行Java Mybatis中的Mapper原理分析
如何进行Java Mybatis中的Mapper原理分析,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
准备
1.pom文件
org.mybatis mybatis 3.4.5 mysql mysql-connector-java 5.1.6 runtime junit junit 4.12 test log4j log4j 1.2.12 org.projectlombok lombok 1.18.2 provided
2.user类-数据库

3.实体类
@Getter@Setter@ToString@NoArgsConstructorpublic class user { private int id; private String username; private String password;}4.dao 层
public interface userDao { List findAll();} 5.Mapper 文件
核心配置文件
核心代码
import dao.userDao;import mode.user;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;import java.util.List;public class TestMapper { public static void main(String[] args) throws IOException { //加载核心配置文件 InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml"); //获得sqlSession工厂对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); //获得sqlSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(); //执行sql语句 userDao mapper = sqlSession.getMapper(userDao.class); List userList = mapper.findAll(); //打印结果 System.out.println(userList); //释放资源 sqlSession.close(); }} 源码分析
1.断点

2.查看源码
DefaultSqlSession:
Configuration:
这是Configuration 类。我们主要看getMapper()方法
MapperRegistry:
关键代码:mapperProxyFactory.newInstance(sqlSession);
MapperProxyFactory:
返回的是MapperProxy 对象
MapperProxy:
源码:
// Source code recreated from a .class file by IntelliJ IDEA// (powered by FernFlower decompiler)//package org.apache.ibatis.binding;import java.io.Serializable;import java.lang.invoke.MethodHandles.Lookup;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.util.Map;import org.apache.ibatis.lang.UsesJava7;import org.apache.ibatis.reflection.ExceptionUtil;import org.apache.ibatis.session.SqlSession;public class MapperProxyimplements InvocationHandler, Serializable { private static final long serialVersionUID = -6424540398559729838L; private final SqlSession sqlSession; private final Class mapperInterface; private final Map methodCache; public MapperProxy(SqlSession sqlSession, Class mapperInterface, Map methodCache) { this.sqlSession = sqlSession; this.mapperInterface = mapperInterface; this.methodCache = methodCache; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { if (Object.class.equals(method.getDeclaringClass())) { return method.invoke(this, args); } if (this.isDefaultMethod(method)) { return this.invokeDefaultMethod(proxy, method, args); } } catch (Throwable var5) { throw ExceptionUtil.unwrapThrowable(var5); } MapperMethod mapperMethod = this.cachedMapperMethod(method); return mapperMethod.execute(this.sqlSession, args); } private MapperMethod cachedMapperMethod(Method method) { MapperMethod mapperMethod = (MapperMethod)this.methodCache.get(method); if (mapperMethod == null) { mapperMethod = new MapperMethod(this.mapperInterface, method, this.sqlSession.getConfiguration()); this.methodCache.put(method, mapperMethod); } return mapperMethod; } @UsesJava7 private Object invokeDefaultMethod(Object proxy, Method method, Object[] args) throws Throwable { Constructor constructor = Lookup.class.getDeclaredConstructor(Class.class, Integer.TYPE); if (!constructor.isAccessible()) { constructor.setAccessible(true); } Class> declaringClass = method.getDeclaringClass(); return ((Lookup)constructor.newInstance(declaringClass, 15)).unreflectSpecial(method, declaringClass).bindTo(proxy).invokeWithArguments(args); } private boolean isDefaultMethod(Method method) { return (method.getModifiers() & 1033) == 1 && method.getDeclaringClass().isInterface(); }}
在invoke方法中可以看到,如果我们调用的是Object中的方法,不做任何处理,直接调用,否则执行:
mapperMethod.execute(this.sqlSession, args);
MapperMethod:
在MapperMethod 中对SQL语句进行分类反射
MapperProxyFactory中,使用JDK的动态代理生成Mapper接口的代理代理类
由动态处理器MapperProxy中调用MapperMethod中的方法处理执行SQL
最后,在MapperMethod中根据执行的方法返回值决定调用SqlSession中的对应方法执行SQL
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。
方法
文件
对象
核心
源码
代理
分析
代码
动态
语句
处理
帮助
配置
原理
清楚
关键
内容
处理器
实体
对此
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
普陀区信息软件开发服务电话
hive如何获取增量数据库
数据库主要安全威胁
辽宁盘位机架式服务器云服务器
国家网络安全宣传周辽宁
sql数据库复制到其他地方
数据库系统高中信息技术
网络安全制度华为
数据库俄罗斯
福建系统软件开发管理
怎么登陆linux服务器
软件开发部年工作计划
奥丁神叛台服服务器角色满了
微信小程序数据库更新
数据库行锁什么时候会释放
数据库投影选择的嵌套运算
金融网络安全防诈主题
赛博昆仑网络安全大会
宿城区个性化网络技术是什么
阿拉伯语数据库
网络安全重点笔记
网络技术安全负责制度
服务商数据库设计
网上服务器协议是什么
数据库系统高中信息技术
三台服务器价格表
英语四级网络安全
次新股网络安全概念股
开发技术数据库有哪些
本地邮箱服务器