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 ...
随机推荐
- MTK6577+Android之Camera驱动
MTK6577+Android之Camera驱动 <MTK安卓平台的Camera效果在线调试> 1. Camera拍照相关概念 1.1 ISP isp--(Image Signa ...
- android ViewPager+Fragment之懒加载
说说写这篇博客的背景吧,前两天去面试,问到一个问题说的是:比如我们首页,是有3个fragment构成的,并且要是实现作用可以滑,那么这个最好的选择就是ViewPager+fragment了,但是我们知 ...
- DB Query Analyzer 6.01 is released, SQL Execute Schedule function can be used
DB Query Analyzer is presented by Master Gen feng, Ma from Chinese Mainland. It has English versi ...
- Log4j运用于代码中
在JAVA代码中,我们要打印输出语句的时候,我们经常会使用System.out.print(),但是在项目开发完后,这些代码就会影响项目的运行效率,所以Log4j就派上用场了.话不多说,直接上代码. ...
- LeetCode(40)-Merge Sorted Array
听到初爱有感 开头啰嗦两句,刚在做算法题目的时候,听到了杨宗纬的<初爱>,突然有了一种本科时候的感觉,想想自己现在研二了,青春喂了狗,我果断喝了一罐啤酒,循环这首歌到吐-.. 题目: Gi ...
- ruby读取源代码自身的一种方法
我们知道ruby中如果源代码中一行开头(必须在行的开头)有__END__标示,则表示下面的都是数据行,可以用IO对象DATA来访问这些行.但是如果我们用DATA.rewind一下的话,就可以将文件流指 ...
- app ionic1 微信 微博 分享功能的实现
微信分享 1.登录微信开放平台注册账户 2.创建一个移动应用 (app) 审核过后会有一个appid 之后安装插件的时候会用到 3.在这个应用上面填写 包名 和 签名 就可以了 包名和签名的 ...
- CSS3实现多样的边框效果
半透明边框 实现效果: 实现代码: <div> 你能看到半透明的边框吗? </div> div { /* 关键代码 */ border: 10px solid rgba(255 ...
- Spring Boot Kafka
1.创建集群 http://kafka.apache.org/documentation/#quickstart 有一句我觉得特别重要: For Kafka, a single broker is j ...
- 安装VirtualBox后 不能选择64bit的系统
之前在台式机上安装VirtualBox,一切OK,能够安装64位的任何版本iso包今天在hp笔记本上安装,安装VirtualBox完毕后,只能选择32位的iso版本. 而我目前只有一个linux64b ...