美文网首页
MyBatis关联映射

MyBatis关联映射

作者: Mr_J316 | 来源:发表于2019-06-18 14:47 被阅读0次

多对一单向关联映射

public class Category {
    private String id;// 类别编号
    private String name;// 类别名称
}
public class User {
    private String id;// 用户编号
    private String name;// 用户名
    private Integer age;// 年龄
    private Category category;// 所属类别
}

配置方法一:

<resultMap id="userResultMap" type="User">
    <id property="id" column="id"/>   
    <result property="name" column="name"/>
    <result property="age" column="age"/>
    <result property="category.id" column="cid" />
    <result property="category.name" column="cname" />
</resultMap>

<select id="selectById" parameterType="String" resultMap="userResultMap"> 
    select u.id,u.name,u.age,c.id as cid,c.name as cname
    from users u left join category c
    on u.categoryid=c.id 
    where u.id=#{id}        
</select>

配置方法二:

<resultMap id="userResultMap" type="User">
    <id property="id" column="iD"/>   
    <result property="name" column="name"/>
    <result property="age" column="age"/>
    <association property="category" javaType="Category">
         <id property="id" column="cid"/>
         <result property="name" column="cname"/>
    </association>
</resultMap>
<select id="selectById" parameterType="String" resultMap="userResultMap"> 
    select u.id,u.name,u.age,c.id as cid,c.name as cname
    from users u left join category c
    on u.categoryid=c.id 
    where u.id=#{id}        
</select>

配置方法三:

<resultMap id="userResultMap" type="User">
    <id property="id" column="id"/>   
    <result property="name" column="name"/>
    <result property="age" column="age"/>
    <association property="category" javaType="Category" 
                 resultMap="categoryResultMap"/>
</resultMap>
<resultMap id="categoryResultMap" type="Category">
   <id property="id" column="cid"/>
   <result property="name" column="cname"/>
</resultMap>

<select id="selectById" parameterType="String" resultMap="userResultMap"> 
    select u.id,u.name,u.age,c.id as cid,c.name as cname
    from users u left join category c
    on u.categoryid=c.id 
    where u.id=#{id}        
</select>

多对一/一对多双向关联映射

public class Category {
    private String id;// 类别编号
    private String name;// 类别名称
    private List<User> users;// 用户集合
}
public class User {
    private String id;// 用户编号
    private String name;// 用户名
    private Integer age;// 年龄
    private Category category;// 所属类别
}

配置方法一:

<resultMap id="categoryResultMap" type="Category">
    <id property="id" column="cid" />
    <result property="name" column="cname" />
    <collection property="users" ofType="User">
        <id property="id" column="id" />
        <result property="name" column="name" />
        <result property="age" column="age" />
    </collection>
</resultMap>

<select id="selectById" parameterType="String" resultMap="categoryResultMap">
    select c.id as cid,c.name as cname,u.id,u.name,u.age
    from category c left join users u
    on c.id=u.categoryid
    where c.id=#{id}
</select>

配置方法二:

<resultMap id="categoryResultMap" type="Category">
    <id property="id" column="cid"/>   
    <result property="name" column="cname"/>
    <collection property="users" ofType="User"   
                resultMap="com.dao.UserDao.userResultMap"/>
</resultMap>
    
<select id="selectById" parameterType="String" resultMap="categoryResultMap">
    select c.id as cid,c.name as cname,u.id,u.name,u.age
    from category c left join users u
    on c.id=u.categoryid 
    where c.id=#{id}    
</select>

相关文章

  • MyBatis 关联映射

    客观世界中的对象很少有孤立存在的,例如班级,往往与班级的学生存在关联关系,如果 得到某个班级的实例,那么应该可以直...

  • MyBatis关联映射

    多对一单向关联映射 配置方法一: 配置方法二: 配置方法三: 多对一/一对多双向关联映射 配置方法一: 配置方法二:

  • mybatis的关联映射

    在查询时经常需要获取两个或两个以上关联表的数据,通过关联映射可以由一个对象获取相关联对象的数据。例如查询一个Emp...

  • MyBatis的关联映射

    三种关联关系 一对一:在任意一方引入对方主键作为外键。 一对多:在“多”的一方,添加“一”的一方的主键作为外键。 ...

  • MyBatis(五)-关联映射

    关联关系概述 在关系型数据库中,多表之间存在着三种关联关系,分别为一对一、一对多和多对多,如图所示。 这三种关联关...

  • 4.mybatis关联映射

      前面几篇关于mybatis的文章中的案例都是基于单表下面的操作,如果涉及多表之间的操作情况就变复杂了,常见的多...

  • MyBatis的关联映射(学习笔记)

      本文针对MyBatis的关联映射,重点在于:(1)不同的关联关系(一对一、一对多、多对多)如何创建数据表和在对...

  • [JAVAEE]实验07:MyBatis关联映射实践

    一、实验目的: 掌握MyBatis的关联映射(一对一、一对多、多对多)实验内容:模拟用户批量购买理财产品的业务。用...

  • Mybatis映射文件

    mybatis二级缓存在的问题? 1、Mybatis 映射文件之增删改: 2、Mybatis 映射文件之 Inse...

  • Mybatis入门到精通-note1

    简介 Mybatis将Java方法与SQL语句关联,简化了JDBC的使用,SQL语句在一行代码中执行 提供了映射引...

网友评论

      本文标题:MyBatis关联映射

      本文链接:https://www.haomeiwen.com/subject/dwsfqctx.html