zoukankan      html  css  js  c++  java
  • mybatis_映射查询

    一、一对一映射查询:

    第一种方式(手动映射):借助resultType属性,定义专门的pojo类作为输出类型,其中该po类中封装了查询结果集中所有的字段。此方法较为简单,企业中使用普遍。

    复制代码
     1   <!-- 
     2         【手动映射:】
     3         查询用户和用户所属的订单信息:
     4         定义一个包含用户和订单实体的所有属性的全pojo类,将查询结果中的所有字段和全pojo类中的属性相对应。
     5      -->
     6     <select id="findUserAndOrders1" resultType="com.itheima.mybatis.pojo.OrdersAnduser">
     7         select o.*,u.id uid,u.username,u.address,u.birthday 
     8         from user u,orders o 
     9         where u.id=o.user_id
    10     </select>
    复制代码

    第二种方式(自动映射):借助resultMap属性,定义专门的resultMap用于映射一对一查询结果。

    以用户订单关联关系为例,一个订单只能所属一个用户(一对一查询)

     

    复制代码
     1    <!-- 
     2         type:表示返回的数据类型
     3         id:表示resultMap的唯一标识
     4      -->
     5     <resultMap type="com.itheima.mybatis.pojo.Orders" id="OrdersUserResultMap">
     6         <!-- 主键列对应的实体类中的唯一属性 -->
     7         <id column="id" property="id"/>
     8         <!-- 普通列对应实体类中的普通属性 -->
     9         <result column="user_id" property="userId"/>
    10         <result column="number" property="number"/>
    11         <result column="createtime" property="createtime"/>
    12         <result column="note" property="note"/>
    13         <!-- association表示进行关联查询的实体映射
    14             property:表示被关联对象在查询对象中的属性民称
    15             javaType:表示被关联对象的全路径名称
    16          -->
    17         <association property="user" javaType="com.itheima.mybatis.pojo.User">
    18             <id column="uid" property="id"/>
    19             <result column="username" property="username"/>
    20             <result column="gender" property="gender"/>
    21             <result column="birthday" property="birthday"/>
    22             <result column="address" property="address"/>
    23         </association>
    24     </resultMap>
    25     <!--  
    26         【自动映射:】
    27         使用resultMap,封装一对一映射关系:
    28         在orders订单类配置User对象,一个订单只能所属一个用户。
    29     -->
    30     <select id="findUserAndOrders2" resultMap="OrdersUserResultMap">
    31         select o.*,u.id uid,u.username,u.address,u.birthday 
    32         from user u,orders o 
    33         where u.id=o.user_id
    34     </select>
    复制代码

    二、一对多映射查询:

    只能使用手动映射, 在resultMap中可以使用collection标签来标记对集合对象的关系映射。

    以用户订单关联关系为例,一个用户可以有多个订单(一对多查询)

    复制代码
     1   <!-- 一对多映射: -->
     2     <select id="findUserAndOrders3" resultMap="UserOrdersResultMap">
     3         select u.*,o.id oid,o.createtime,o.number 
     4         from user u,orders o 
     5         where u.id=o.user_id
     6     </select>
     7     <!-- 
     8         type:表示返回的数据类型
     9         id:表示resultMap的唯一标识
    10      -->
    11     <resultMap type="com.itheima.mybatis.pojo.User" id="UserOrdersResultMap">
    12         <id column="oid" property="id"/>
    13         <result column="username" property="username"/>
    14         <result column="gender" property="gender"/>
    15         <result column="birthday" property="birthday"/>
    16         <result column="address" property="address"/>
    17         <!-- 
    18             collection:表示关联查询的结果集
    19             property:关联查询的结果集存储在User对象的上的哪个属性
    20             ofType:表示返回集合中的数据类型
    21          -->
    22         <collection property="ordersList" ofType="com.itheima.mybatis.pojo.Orders">
    23             <id column="id" property="id"/>
    24             <result column="user_id" property="userId"/>
    25             <result column="number" property="number"/>
    26             <result column="createtime" property="createtime"/>
    27             <result column="note" property="note"/>
    28         </collection>
    29     </resultMap>
    复制代码
  • 相关阅读:
    hive 之start hiveServer2 ,thriftServer失败
    sqoop 导入mysql中表存在联合主键
    hive metastore Server 出现异常
    hiveF 函数解析时间问题
    hive 动态分区数设置
    sqoop 操作从hdfs 导入到mysql中语句
    hive 锁表问题
    在hive中直接对timestamp类型取max报错
    Qt程序crash信息的捕捉与跟踪(转)
    功能快捷键如注释、声明和实现之间切换(转)
  • 原文地址:https://www.cnblogs.com/wangchaoyuana/p/7545230.html
Copyright © 2011-2022 走看看