MyBatis的关联查询之一对多,多对一

在关系型数据库中,我们经常要处理一对多,多对一和多对多的关系。

一对多,多对一

一、entity 实体类

public class SmbmsRole {
private long rid;
private String roleCode;
private String roleName;
   //泛型集合
private List<User> users; public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
} public long getRid() {
return rid;
} public void setRid(long rid) {
this.rid = rid;
} public String getRoleCode() {
return roleCode;
} public void setRoleCode(String roleCode) {
this.roleCode = roleCode;
} public String getRoleName() {
return roleName;
} public void setRoleName(String roleName) {
this.roleName = roleName;
}
}
public class User {
private Integer uid;
private String userCode;
private String userName;
//关联一的一方
private SmbmsRole role; public SmbmsRole getRole() {
return role;
} public void setRole(SmbmsRole role) {
this.role = role;
} public Integer getUid() {
return uid;
} public void setUid(Integer uid) {
this.uid = uid;
} public String getUserCode() {
return userCode;
} public void setUserCode(String userCode) {
this.userCode = userCode;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
}

二、dao层

//一对多
//根据角色获取所对应的人
SmbmsRole getroleId(Integer rid); //多对一 查询所有用户信息
List<User> getUserList();

三、小配置  xml

 <!--一对多-->
<!--由于是关联查询 返回的是多张表中的结果集,必须定义resultMap映射-->
<resultMap id="SmbmsRoleMaps" type="com.marketsys.entity.SmbmsRole">
<id column="rid" property="rid"></id>
<!--此处使用的是collection节点,由于在SmbmsRole类中插入的是List集合
ofType:为集合中的泛型-->
<collection property="users" ofType="User" select="getroleIdMutilSQL" column="rid">
<!--在collection中声明Provincial中的属性与表中列的映射-->
<!-- <id column="uid" property="uid"></id>-->
</collection>
</resultMap> <!--一对多 一条select-->
<!--<select id="getroleId" parameterType="int" resultMap="SmbmsRoleMaps">
select r.rid,roleName,u.uid,userName from smbms_role r inner join smbms_user u on r.rid=u.userRole where r.rid=#{rid}
</select>-->
<!--一对多 两条select-->
<select id="getroleId" resultMap="SmbmsRoleMaps">
select * from smbms.smbms_role where rid=#{rid}
</select>
<select id="getroleIdMutilSQL" resultType="com.marketsys.entity.User">
select * from smbms.smbms_user where userRole=#{rid}
</select> <!--多对一-->
<resultMap id="SmbmsRoleMap" type="com.marketsys.entity.User">
<id column="uid" property="uid"></id> <association property="role" javaType="com.marketsys.entity.SmbmsRole" select="getRole" column="userRole">
<id column="rid" property="rid"></id>
</association>
</resultMap>
<!-- 多对一 一条select-->
<!--<select id="getUserList" resultMap="SmbmsRoleMap">
select rid,roleName,uid,userName from smbms_role,smbms_user where smbms_role.rid=smbms_user.userRole
</select>-->
<!-- 多对一 二条select-->
<select id="getUserList" resultMap="SmbmsRoleMap">
select uid,userName,userRole from smbms.smbms_user
</select>
<select id="getRole" resultType="com.marketsys.entity.SmbmsRole">
select rid,roleName from smbms.smbms_role where rid=#{userRole}
</select>

四、test测试类

   SqlSession sqlSession= MybatisUtil.getSqlSession();
URoleTestDao mapper=sqlSession.getMapper(URoleTestDao.class); //一对多
@Test
public void test1(){
SmbmsRole smbmsRole=mapper.getroleId(3);
System.out.println("身份为:"+smbmsRole.getRoleName()+"\n姓名为:");
for (User item:smbmsRole.getUsers()) {
System.out.println(item.getUserName());
}
} //多对一
@Test
public void test2(){
List<User> userList = mapper.getUserList();
for (User user:userList) {
System.out.println("用户:"+user.getUserName()+"\t角色:"+user.getRole().getRoleName());
}
}

Mybatis关联查询之一的更多相关文章

  1. Mybatis关联查询和数据库不一致问题分析与解决

    Mybatis关联查询和数据库不一致问题分析与解决 本文的前提是,确定sql语句没有问题,确定在数据库中使用sql和项目中结果不一致. 在使用SpringMVC+Mybatis做多表关联时候,发现也不 ...

  2. MyBatis基础:MyBatis关联查询(4)

    1. MyBatis关联查询简介 MyBatis中级联分为3中:association.collection及discriminator. ◊ association:一对一关联 ◊ collecti ...

  3. MyBatis关联查询,一对多关联查询

    实体关系图,一个国家对应多个城市 一对多关联查询可用三种方式实现: 单步查询,利用collection标签为级联属性赋值: 分步查询: 利用association标签进行分步查询: 利用collect ...

  4. mybatis 关联查询实现一对多

    场景:最近接到一个项目是查询管理人集合  同时每一个管理人还存在多个出资人   要查询一个管理人列表  每个管理人又包含了出资人列表 采用mybatis关联查询实现返回数据. 实现方式: 1 .在实体 ...

  5. MyBatis关联查询、多条件查询

    MyBatis关联查询.多条件查询 1.一对一查询 任务需求; 根据班级的信息查询出教师的相关信息 1.数据库表的设计 班级表: 教师表: 2.实体类的设计 班级表: public class Cla ...

  6. Mybatis关联查询之二

    Mybatis关联查询之多对多 多对多 一.entity实体类 public class Student { private Integer stuid; private String stuname ...

  7. mybatis关联查询基础----高级映射

    本文链接地址:mybatis关联查询基础----高级映射(一对一,一对多,多对多) 前言: 今日在工作中遇到了一个一对多分页查询的问题,主表一条记录对应关联表四条记录,关联分页查询后每页只显示三条记录 ...

  8. MyBatis关联查询和懒加载错误

    MyBatis关联查询和懒加载错误 今天在写项目时遇到了个BUG.先说一下背景,前端请求更新生产订单状态,后端从前端接收到生产订单ID进行查询,然后就有问题了. 先看控制台报错: org.apache ...

  9. Mybatis关联查询(嵌套查询)

    上一篇文章介绍了基于Mybatis对数据库的增.删.改.查.这一篇介绍下关联查询(join query). 三张表:user article blog 表的存储sql文件: /* Navicat My ...

  10. MyBatis关联查询 (association) 时遇到的某些问题/mybatis映射

    先说下问题产生的背景: 最近在做一个用到MyBatis的项目,其中有个业务涉及到关联查询,我是将两个查询分开来写的,即嵌套查询,个人感觉这样更方便重用: 关联的查询使用到了动态sql,在执行查询时就出 ...

随机推荐

  1. PWA 学习笔记(一)

    PWA 介绍 概念: PWA(Progressive web apps,渐进式 Web 应用)运用现代 Web API 和传统的渐进式增强策略来创建跨平台 Web 应用程序 它并不是一个快捷方式,而能 ...

  2. PHP switch的写法

    switch switch (expression) { case label1: expression = label1 时执行的代码 ; break; case label2: expressio ...

  3. SQL Server解惑——为什么你的查询结果超出了查询时间范围

    废话少说,直接上SQL代码(有兴趣的测试验证一下),下面这个查询语句为什么将2008-11-27的记录查询出来了呢?这个是同事遇到的一个问题,个人设计了一个例子. USE AdventureWorks ...

  4. qcom 8953平台 LCD亮灭屏流程及LCD知识点总结【转】

    一.LK中亮屏流程 gcdb_display_init(),进行display初始化的起始地方: oem_panel_select(),在这里去选择哪一款屏,也可以在这里添加新一款屏: dsi_pan ...

  5. crontab -e 报错(E518: Unknown option: foldenable)

    crontab 默认编辑器为vi,不支持foldenable #crontab -e Error detected while processing /root/.vimrc: line : E518 ...

  6. MATLAB实例:新建文件夹,保存.mat文件并保存数据到.txt文件中

    MATLAB实例:新建文件夹,保存.mat文件并保存数据到.txt文件中 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 用MATLAB实现:指定路径下 ...

  7. [译]Vulkan教程(05)Instance

    [译]Vulkan教程(05)Instance Creating an instance 创建一个instance The very first thing you need to do is ini ...

  8. java之程序流程控制

    顺序结构:代码由上至下依次执行: 分支结构: if () { } else{ } if () { } else if () { } else { } switch(常量){ case 常量: 语句; ...

  9. 调试seanbell/intrinsic遇到的坑

    那些遗忘过去的人注定要重蹈覆辙.——乔治•桑塔亚纳  Authorized error 刚开始按作者 GitHub 上的指示,当运行环境配置好,并且 make 之后,因为生成的 decompose.p ...

  10. 05-文档编辑与yum命令

    一.Linux vi/vim vi是所有的Unix系统都会有,但是目前我们使用最多的是vim编辑器.vim具有程序编辑的能力,可以主动以字体颜色辨别语法的正确性. vim是从vi发展出来的一个文本编辑 ...