听说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一对一映射配置详解的更多相关文章

  1. 转载 Spring、Spring MVC、MyBatis整合文件配置详解

    Spring.Spring MVC.MyBatis整合文件配置详解   使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. ...

  2. Mybatis SQL映射文件详解

    Mybatis SQL映射文件详解 mybatis除了有全局配置文件,还有映射文件,在映射文件中可以编写以下的顶级元素标签: cache – 该命名空间的缓存配置. cache-ref – 引用其它命 ...

  3. Spring MVC、MyBatis整合文件配置详解

    Spring:http://spring.io/docs MyBatis:http://mybatis.github.io/mybatis-3/ Building a RESTful Web Serv ...

  4. 深入浅出Mybatis系列四-配置详解之typeAliases别名(mybatis源码篇)

    注:本文转载自南轲梦 注:博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 上篇文章<深入浅出Mybatis系列(三)---配置详解之properties ...

  5. Mybatis(三) 映射文件详解

    前面说了全局配置文件中内容的详解,大家应该清楚了,现在来说说这映射文件,这章就对输入映射.输出映射.动态sql这几个知识点进行说明,其中高级映射(一对一,一对多,多对多映射)在下一章进行说明. 一.输 ...

  6. Spring、Spring MVC、MyBatis整合文件配置详解

    原文  http://www.cnblogs.com/wxisme/p/4924561.html 主题 MVC模式MyBatisSpring MVC 使用SSM框架做了几个小项目了,感觉还不错是时候总 ...

  7. 【转】Spring、Spring MVC、MyBatis整合文件配置详解

    见:http://www.tuicool.com/articles/eyINveF web.xml的配置 web.xml应该是整个项目最重要的配置文件了,不过servlet3.0中已经支持注解配置方式 ...

  8. Spring、Spring MVC、MyBatis整合文件配置详解2

    使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. Spring:http://spring.io/docs MyBatis ...

  9. Spring、Spring MVC、MyBatis 整合文件配置详解

    使用SSM框架做了几个小项目了,感觉还不错是时候总结一下了.先总结一下SSM整合的文件配置.其实具体的用法最好还是看官方文档. Spring:http://spring.io/docs MyBatis ...

随机推荐

  1. (视频) 《快速创建网站》 3.2 WordPress多站点及Azure在线编辑器 - 扔掉你的ftp工具吧,修改代码全部云端搞定

    本文是<快速创建网站>系列的第6篇,如果你还没有看过之前的内容,建议你点击以下目录中的章节先阅读其他内容再回到本文. 1. 网站管理平台WordPress和云计算平台Azure简介 (6分 ...

  2. 网站开发进阶(二十二)HTML UI知识汇总(更新中...)

    HTML知识汇总(更新中...) 1.<iframe> 标签 浏览器支持 所有浏览器都支持 <iframe> 标签. 定义和用法 iframe 元素会创建包含另外一个文档的内联 ...

  3. 【一天一道LeetCode】#15 3Sum

    一天一道LeetCode系列 (一)题目 Given an array S of n integers, are there elements a, b, c in S such that a + b ...

  4. ROS探索总结(十三)——导航与定位框架

    导航与定位是机器人研究中的重要部分.         一般机器人在陌生的环境下需要使用激光传感器(或者深度传感器转换成激光数据),先进行地图建模,然后在根据建立的地图进行导航.定位.在ROS中也有很多 ...

  5. 是我out了,c11标准出炉鸟

    gcc -std=c11 -Wall -O3 -g0 -s -o x.c x 或者 clang -std=c11 -Wall -O3 -g0 -s -o x.c x 来吧! 我是有多无聊啊 测试代码: ...

  6. LeetCode(24)-Balanced Binary Tree

    题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  7. Jmeter(二十五)_Xpath关联

    在Jmeter中,除了正则表达式可以用作关联,还有一种方式也可以做关联,那就是 XPath Extractor.它是利用xpath提取出关键信息,传递变量. 具体用法 添加一个后置处理器-XPath ...

  8. css中bfc和ifc

    bfc定义:块级格式化上下文,他是一个独立的渲染区域,他规定了这个内部如何布局,并且与这个区域的外部毫不相干.BFC布局规则: 内部的Box会在垂直方向,一个接一个地放置.Box垂直方向的距离由mar ...

  9. Java不走弯路教程(6.JDBC)

    6.JDBC 在上一章,我们完成了MyDb数据库的简单的客户段调用.作为产品我们还封装了驱动程序,并且提供了统一的调用接口. 大家应该知道,市面上有多种数据库产品,比如Oracle,Mysql,DB2 ...

  10. Linux的eth0,eth1,eth2,lo详解

    eth0,eth1,eth2……代表网卡一,网卡二,网卡三……lo代表127.0.0.1,即localhost 参考:Linux命令:ifconfig 功能说明:显示或设置网络设备 语 法:ifcon ...