MyBatis mapper的理解
来源:互联网 发布:店铺怎么开通淘宝客 编辑:程序博客网 时间:2024/06/07 06:23
MyBatis由2部分组成(标准应用模式)
1.XML
2.Mapper
XML不用说了,用来定义SQL语句
Mapper的作用是用来绑定XML和程序之间的关系
Mapper中必须提供与XML中id名称相同的接口方法,这个定义我想你已经知道了
根据MyBatis的日志显示,程序被加载时
MyBatis从XML中读取出各个SQL语句,然后根据XML指定的MAPPER位置绑定相应的接口
然后MyBatis会在自身内容进行动态代理,将各Mapper接口进行动态实现,所以虽然你没有写任何的具体JDBC代码,但实际上MyBatis已经为你做好了这些事情
然后你在Mapper.XXXXX具体调用的时候,就可以操作数据库了
简单的来说,XML保存了你的SQL逻辑
1.XML
2.Mapper
XML不用说了,用来定义SQL语句
Mapper的作用是用来绑定XML和程序之间的关系
Mapper中必须提供与XML中id名称相同的接口方法,这个定义我想你已经知道了
根据MyBatis的日志显示,程序被加载时
MyBatis从XML中读取出各个SQL语句,然后根据XML指定的MAPPER位置绑定相应的接口
然后MyBatis会在自身内容进行动态代理,将各Mapper接口进行动态实现,所以虽然你没有写任何的具体JDBC代码,但实际上MyBatis已经为你做好了这些事情
然后你在Mapper.XXXXX具体调用的时候,就可以操作数据库了
简单的来说,XML保存了你的SQL逻辑
MAPPER则起到了沟通程序和SQL之间的相互访问
http://bbs.csdn.net/topics/390100335
mybatis Dao 接口 没有实现类的源码实现
DAOSQLJDKXML
注意:TestTable 为POJO,TestTableMapper为DAO接口,mappingXml 为sql 配置文档,专业点叫sql mapper
Mybatis 有一个调用dao的方式 ,如下:
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
SqlSession session= sqlMapper.openSession();
TestTableMapper t= session.getMapper(TestTableMapper.class);
TestTable tt=(TestTable)t.selectByPrimaryKey("wwww");
TestTableMapper是一个inteface,不是一个class,并且我查看了用mybatis genetator产生的code,没有该接口的实现的..冷,我当时以为我用generator产生的code有问题,可是,用上面一段code跑一下,竟然有从DB query到值. 恩,有玄机,哥哥于是准备吃点亏,翻源码来瞧瞧.
玄机就在session.getMapper()
session.getMapper()使用了代理,当调用一次此方法,都会产生一个代理class的instance,看看这个代理class的实现.
public class MapperProxy implements InvocationHandler {
...
public static <T> T newMapperProxy(Class<T> mapperInterface, SqlSession sqlSession) {
ClassLoader classLoader = mapperInterface.getClassLoader();
Class<?>[] interfaces = new Class[]{mapperInterface};
MapperProxy proxy = new MapperProxy(sqlSession);
return (T) Proxy.newProxyInstance(classLoader, interfaces, proxy);
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (!OBJECT_METHODS.contains(method.getName())) {
final Class<?> declaringInterface = findDeclaringInterface(proxy, method);
final MapperMethod mapperMethod = new MapperMethod(declaringInterface, method, sqlSession);
final Object result = mapperMethod.execute(args);
if (result == null && method.getReturnType().isPrimitive()) {
throw new BindingException("Mapper method '" + method.getName() + "' (" + method.getDeclaringClass() + ") attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");
}
return result;
}
return null;
}
这里是用到了JDK的代理Proxy。
newMapperProxy()可以取得实现interfaces 的class的代理类的实例,当执行interfaces中的方法的时候,会自动执行invoke()方法,其中public Object invoke(Object proxy, Method method, Object[] args)中 method参数就代表你要执行的方法.
MapperMethod类会使用method方法的methodName 和declaringInterface去取 sqlMapxml 取得对应的sql,也就是拿declaringInterface的类全名加上 sqlid..
贴一点sqlMap xml 看看..
<mapper namespace="com.whisper.dao.TestTableMapper">
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
嘿嘿,总结一下,其实就是用了 jdk的代理类而已....
Mybatis 有一个调用dao的方式 ,如下:
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
SqlSession session= sqlMapper.openSession();
TestTableMapper t= session.getMapper(TestTableMapper.class);
TestTable tt=(TestTable)t.selectByPrimaryKey("wwww");
TestTableMapper是一个inteface,不是一个class,并且我查看了用mybatis genetator产生的code,没有该接口的实现的..冷,我当时以为我用generator产生的code有问题,可是,用上面一段code跑一下,竟然有从DB query到值. 恩,有玄机,哥哥于是准备吃点亏,翻源码来瞧瞧.
玄机就在session.getMapper()
session.getMapper()使用了代理,当调用一次此方法,都会产生一个代理class的instance,看看这个代理class的实现.
public class MapperProxy implements InvocationHandler {
...
public static <T> T newMapperProxy(Class<T> mapperInterface, SqlSession sqlSession) {
ClassLoader classLoader = mapperInterface.getClassLoader();
Class<?>[] interfaces = new Class[]{mapperInterface};
MapperProxy proxy = new MapperProxy(sqlSession);
return (T) Proxy.newProxyInstance(classLoader, interfaces, proxy);
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (!OBJECT_METHODS.contains(method.getName())) {
final Class<?> declaringInterface = findDeclaringInterface(proxy, method);
final MapperMethod mapperMethod = new MapperMethod(declaringInterface, method, sqlSession);
final Object result = mapperMethod.execute(args);
if (result == null && method.getReturnType().isPrimitive()) {
throw new BindingException("Mapper method '" + method.getName() + "' (" + method.getDeclaringClass() + ") attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");
}
return result;
}
return null;
}
这里是用到了JDK的代理Proxy。
newMapperProxy()可以取得实现interfaces 的class的代理类的实例,当执行interfaces中的方法的时候,会自动执行invoke()方法,其中public Object invoke(Object proxy, Method method, Object[] args)中 method参数就代表你要执行的方法.
MapperMethod类会使用method方法的methodName 和declaringInterface去取 sqlMapxml 取得对应的sql,也就是拿declaringInterface的类全名加上 sqlid..
贴一点sqlMap xml 看看..
<mapper namespace="com.whisper.dao.TestTableMapper">
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
嘿嘿,总结一下,其实就是用了 jdk的代理类而已....
http://sxk4429.iteye.com/blog/837199
阅读全文
0 0
- MyBatis mapper的理解
- mybatis的mapper
- mybatis mapper的配置
- mybatis的mapper XML
- Mybatis的Mapper配置
- mybatis的mapper文件注意事项
- mybatis 的mapper代理小结
- mybatis的mapper.xml 小结
- mybatis通用mapper的使用
- mybatis中mapper的属性
- Mybatis的配置Mapper方法
- Mybatis批量添加的mapper
- mybatis之mapper的配置
- mybatis-mapper的复杂映射
- mybatis的mapper配置方式
- Mybatis通用Mapper的使用
- Mybatis的mapper配置文件(一)
- 【MyBatis-05】mybatis开发mapper的方式
- Ubuntu16.04下载安装wine使用TIM/QQ
- Thumbnailator--google图片处理项目
- 关于类和对象的进一步讨论
- CS231n官方笔记授权翻译总集篇发布
- 初识HTML&sublime
- MyBatis mapper的理解
- java 编码规范
- 用netty实现paho(一)
- 人工智能II
- WIFI 一键配置原理-ESP8266
- MyBatis3操作数据库
- IP格式转换功能代码分享
- CS231n课程笔记翻译1:Python Numpy教程
- 二叉树经典面试题4~判断一棵树是否是完全二叉树