mybatis一对一映射配置详解
听说mybatis一对一有三种写法,今天我试了一下。
数据库表准备
为了偷懒,我直接就拿用户权限菜单里的菜单表和菜单与权限的中间表做实现,他们原来是多对多的关系,这边我假设这两张表是一对一。
表 gl_role_men:id,role_id,menu_id ---------> 实体类 GlrolemenuModel private String id;private String roleId;private String menuId;private MenuModel menu;
表 menu:id,menu_name,url ---------> 实体类 MenuModel private String id;private String menuName;private String url;
一对一第一种写法
glrolemenuMapper.xml
这个映射文件里的写法有几个要注意的地方,因为是GlrolemenuModel里放了MenuModel的信息,所以我称GlrolemenuModel是维护关系的一方,那么resultMap的type就是GlrolemenuModel
property对应是实体类的属性,column对应的是数据库里表字段名
<resultMap type="com.tieasy.model.GlrolemenuModel" id="glrolemenu_menu_map">
<id property="id" column="id" />
<result property="roleId" column="role_id" />
<result property="menuId" column="menu_id" /> <!-- 上面是GlrolemenuModel表里的信息,下面是MenuModel表里的信息,应该很清楚了吧,注意下面的property的写法,同时实体类GlrolemenuModel
里也要加上private MenuModel menu -->
<result property="menu.menuName" column="menu_name" />
<result property="menu.url" column="url" />
</resultMap>
<select id="glrolemenu_getAllGlrolemenuAndMenu" resultMap="glrolemenu_menu_map">
select
gl_role_menu.id,
gl_role_menu.role_id,
gl_role_menu.menu_id,
menu.menu_name,
menu.url
from gl_role_menu left join menu on gl_role_menu.menu_id = menu.id;<!-- 一个简单的关联查询 -->
</select>
GlrolemenuDao
List<GlrolemenuModel> getAllGlrolemenuAndMenu();
GlrolemenuDaoImpl
public List<GlrolemenuModel> getAllGlrolemenuAndMenu() {
return baseDao.selectList("com.tieasy.model.mapper.glrolemenu_getAllGlrolemenuAndMenu", null);
}
Action里调用接口实现方法
List<GlrolemenuModel> list = glrolemenuDao.getAllGlrolemenuAndMenu();
返回list的json
[{"id":"02ce54203c514b3ca176a3203957c222-1484040384-510","roleId":"02ce54203c514b3ca176a3203957ce0e-1484040384-581","menuId":"00dfcf127f4b4ba1bc8a891938519be0-1484040323-960","menu":{"menuName":"权限管理","url":"/quanxianguanli"}}]
一对一第二种写法
这边使用到了association,这个真的很神奇,原谅我很土鳖,和第一种的写法区别不大,主要就是把被维护的哪一方MenuModel换成用<association>来写
glrolemenuMapper.xml
淡黄色背景的就是改动的部分
<resultMap type="com.tieasy.model.MenuModel" id="menuResultMap">
<id property="id" column="id" />
<result property="menuName" column="menu_name" />
<result property="url" column="url" />
</resultMap>
<resultMap type="com.tieasy.model.GlrolemenuModel" id="glrolemenu_menu_map">
<id property="id" column="id" />
<result property="roleId" column="role_id" />
<result property="menuId" column="menu_id" /> <association property="menu" resultMap="menuResultMap" />
</resultMap>
<select id="glrolemenu_getAllGlrolemenuAndMenu" resultMap="glrolemenu_menu_map">
select
gl_role_menu.id,
gl_role_menu.role_id,
gl_role_menu.menu_id,
menu.menu_name,
menu.url
from gl_role_menu left join menu on gl_role_menu.menu_id = menu.id;
</select>
其它文件都不需要动
元素<association>被用来导入“有一个”(has-one)类型的关联。在上述的例子中,我们使用了<association>元素引用了另外的在同一个XML文件中定义的<resultMap>。
同时我们也可以使用<association> 定义内联的resultMap。
<resultMap type="com.tieasy.model.GlrolemenuModel" id="glrolemenu_menu_map">
<id property="id" column="id" />
<result property="roleId" column="role_id" />
<result property="menuId" column="menu_id" /> <association property="menu" javaType="com.tieasy.model.MenuModel" >
<id property="id" column="id" />
<result property="menuName" column="menu_name" />
<result property="url" column="url" />
</association>
</resultMap>
<select id="glrolemenu_getAllGlrolemenuAndMenu" resultMap="glrolemenu_menu_map">
select
gl_role_menu.id,
gl_role_menu.role_id,
gl_role_menu.menu_id,
menu.menu_name,
menu.url
from gl_role_menu left join menu on gl_role_menu.menu_id = menu.id;
</select>
一对一第三种写法
glrolemenuMapper.xml
还是只有映射文件有改变,但是我这边是因为数据库里每张表只有一条数据,所以其实实际id="glrolemenu_getAllGlrolemenuAndMenu"的这个select是要有个查询条件parameterType,哎,我是有多懒。不过反正是查出来了。
在此方式中,<association>元素的select属性被设置成了id为glrolemenu_getMenuById的语句。这里,两个分开的SQL语句将会在数据库中分别执行。
看看mybatis打印的信息,确定是分两次查询的

<resultMap type="com.tieasy.model.MenuModel" id="menuResultMap">
<id property="id" column="id" />
<result property="menuName" column="menu_name" />
<result property="url" column="url" />
</resultMap>
<select id="glrolemenu_getMenuById" parameterType="String" resultMap="menuResultMap">
select
id,
menu_name,
url
from menu where id = #{id};
</select> <resultMap type="com.tieasy.model.GlrolemenuModel" id="glrolemenu_menu_map">
<id property="id" column="id" />
<result property="roleId" column="role_id" />
<!-- <result property="menuId" column="menu_id" /> --> <association property="menu" column="menu_id" select="glrolemenu_getMenuById" />
</resultMap>
<select id="glrolemenu_getAllGlrolemenuAndMenu" resultMap="glrolemenu_menu_map">
select
id,
role_id,
menu_id
from gl_role_menu;
</select>
总结
这个mybatis一对一配置,对于我这种懒人我选的话可能会选第一二种写法,但是需要强调的是第三种方法可以实现懒加载,因为它是分两个sql先后执行的,当选择懒加载的时候,只会执行第一个sql,只有当我们需要访问到第二条sql的数据的时候,才会触发它执行,这就是所谓的懒加载。
嗯,那什么,就和单例模式的饿汉模式和懒汉模式类似。
mybatis一对一映射配置详解的更多相关文章
- 转载 Spring、Spring MVC、MyBatis整合文件配置详解
Spring.Spring MVC.MyBatis整合文件配置详解 使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. ...
- Mybatis SQL映射文件详解
Mybatis SQL映射文件详解 mybatis除了有全局配置文件,还有映射文件,在映射文件中可以编写以下的顶级元素标签: cache – 该命名空间的缓存配置. cache-ref – 引用其它命 ...
- Spring MVC、MyBatis整合文件配置详解
Spring:http://spring.io/docs MyBatis:http://mybatis.github.io/mybatis-3/ Building a RESTful Web Serv ...
- 深入浅出Mybatis系列四-配置详解之typeAliases别名(mybatis源码篇)
注:本文转载自南轲梦 注:博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 上篇文章<深入浅出Mybatis系列(三)---配置详解之properties ...
- Mybatis(三) 映射文件详解
前面说了全局配置文件中内容的详解,大家应该清楚了,现在来说说这映射文件,这章就对输入映射.输出映射.动态sql这几个知识点进行说明,其中高级映射(一对一,一对多,多对多映射)在下一章进行说明. 一.输 ...
- Spring、Spring MVC、MyBatis整合文件配置详解
原文 http://www.cnblogs.com/wxisme/p/4924561.html 主题 MVC模式MyBatisSpring MVC 使用SSM框架做了几个小项目了,感觉还不错是时候总 ...
- 【转】Spring、Spring MVC、MyBatis整合文件配置详解
见:http://www.tuicool.com/articles/eyINveF web.xml的配置 web.xml应该是整个项目最重要的配置文件了,不过servlet3.0中已经支持注解配置方式 ...
- Spring、Spring MVC、MyBatis整合文件配置详解2
使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. Spring:http://spring.io/docs MyBatis ...
- Spring、Spring MVC、MyBatis 整合文件配置详解
使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. Spring:http://spring.io/docs MyBatis ...
随机推荐
- python-inotify 在linux上安装
python-inotify 在linux上安装 0 下载 $ wget --no-check-certificate https://pypi.python.org/packages/source/ ...
- myfirstBI项目总结
app 应用信息统计: saiku安装:http://blog.csdn.net/longshenlmj/article/details/17359645 workbench解压即用,http://b ...
- LeetCode之“字符串”:最短回文子串
题目链接 题目要求: Given a string S, you are allowed to convert it to a palindrome by adding characters in f ...
- gtk程序运行报 main_loop!=NULL 错误的解决办法
现象是将按钮的clicked Action与gtk_main_quit函数绑定起来会发生如上错误. 原因不明. 如果将window的destroy Action与gtk_main_quit绑定是没有问 ...
- 一个简单的ruby生成器例子(用连续体Continuation实现)
ruby中有很多经典的驱动器结构,比如枚举器和生成器等.这次简单介绍下生成器的概念.生成器是按照功能要求,一次产生一个对象,或称之为生成一个对象的方法.ruby中的连续体正好可以用来完成生成器的功能. ...
- sql——查询出表中不为空或为空字段的总值数
查询所给的表中值为空的总数 判断字段是否为空的sql语句 SELECT sex FROM id where sex is not NULL SELECT COUNT(*) t FROM id wher ...
- 自动布局Autoresizing与Autolayout
一.关于iPhone屏幕的一些基本常识 1.ios屏幕适配的尺寸 iPhone的尺寸3.5inch.4.0inch.4.7inch.5.5inch iPad的尺寸7.9inch.9.7inch 2.点 ...
- JVM内存详解-阅读笔记
- jstack Dump
jstack Dump 日志文件中的线程状态 dump 文件里,值得关注的线程状态有: 死锁,Deadlock(重点关注) 执行中,Runnable 等待资源,Waiting on conditio ...
- 学会分析YUV数据
做视频采集与处理,自然少不了要学会分析YUV数据.因为从采集的角度来说,一般的视频采集芯片输出的码流一般都是YUV数据流的形式,而从视频处理(例如H.264.MPEG视频编解码)的角度来说,也是在原始 ...