mybatis高级映射关系,一对一,一对多,多对多,多对一

来源:互联网 发布:真假混卖化妆品淘宝店 编辑:程序博客网 时间:2024/05/17 04:02
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<!-- namespace命名空间,作用就是对sql进行分类化的管理,理解为sql隔离
    注意:使用mapper代理开发时,namespace有特殊作用,namespace等于mapper接口地址
 -->
<mapper namespace="com.yanshu.service.UserService" >
<!--一对一查询 resultType实现 ,确定查询的主表:订单表 open_Order,确定查询的关联表:用户表 TopUsers  -->
<select id="getUserService" resultType="com.yanshu.pojo.Users">
select t.SellerNick,t.SellerId,o.oid from  open_Order o,TopUsers t where  o.id=t.id
</select>
<!--一对一查询 -->
<!-- 定义查询订单关联用户的 resultMap,将整个的查询结果映射到com.yanshu.pojo.Users中 -->
<resultMap type="com.yanshu.pojo.Order" id="UsersResultMap">
<!-- 配置映射的订单信息 -->
<!--id:查询列中的唯一标识,订单信息中的唯一标识,如果多列组成唯一标识  
(如:一般数据库设计中的字典表 使用联合主键),就需要配置多个id 
column:数据库中的值, 订单信息的唯一标识 列 
property:映射数据库列的字段或属性.订单信息的唯一标识列所映射到orders中的那个属性(假如:数据库中orders表中的主键为orders_id,而实体属性名称为ordersId,
则这个配置应为<id column="orders_id" property="ordersId"/>,类似hibernate实体映射文件配置)。
-->
<id column="id" property="id"/>
<result column="oid" property="oid"/>
<!-- 配置映射的关联用户信息 -->
<!--
resultMap:使用association和collection完成一对一和一对多高级映射(对结果有特殊的映射要求)。
association:将关联查询信息映射到一个pojo对象中。
collection:将关联查询信息映射到一个list集合中。
property:要将关联查询的用户信息映射到Orders中那个属性
 -->
 <association column="id" property="id" select="com.yanshu.pojo.Users">
  <id column="id" property="id"/>
  <result column="SellerNick" property="SellerNick"/>
  <result column="SellerId" property="SellerId"/>
 </association>
</resultMap>
<!-- 一对多的关系:用户可以有多个订单:2级菜单 -->
<resultMap type="com.yanshu.pojo.Users" id="oneMoreResultMap">
<id column="id" property="id"/>
  <result column="SellerNick" property="SellerNick"/>
  <result column="SellerId" property="SellerId"/>
<collection property="ordersList" ofType="com.yanshu.pojo.Order">
<id column="id" property="id"/>
<result column="oid" property="oid"/>
</collection>

</resultMap>
<!-- 一对一的关系/多对一 :多个订单一个用户-->
<resultMap type="com.yanshu.pojo.Order" id="moreOneResultMap">
<id column="id" property="id"/>
<result column="oid" property="oid"/>
<!-- <collection property="Usersone" ofType="com.yanshu.pojo.Users">
<id column="id" property="id"/>
  <result column="SellerNick" property="SellerNick"/>
  <result column="SellerId" property="SellerId"/>
</collection> -->
<!--使用resultMap属性引用下面的教师实体映射-->
<!-- <association property="Usersone" javaType="com.yanshu.pojo.Users" resultMap="userRequest"/> -->
<association property="Usersone" javaType="com.yanshu.pojo.Users">
<id column="id" property="id"/>
  <result column="SellerNick" property="SellerNick"/>
  <result column="SellerId" property="SellerId"/>
</association>

</resultMap>
<resultMap type="com.yanshu.pojo.Users" id="userRequest">
<id column="id" property="id"/>
  <result column="SellerNick" property="SellerNick"/>
  <result column="SellerId" property="SellerId"/>
</resultMap>

<!-- 多对多关系 -->
<resultMap type="com.yanshu.pojo.Users" id="MoreResultMap">
<id column="id" property="id"/>
  <result column="SellerNick" property="SellerNick"/>
  <result column="SellerId" property="SellerId"/>
<collection property="ordersList" ofType="com.yanshu.pojo.Order">
<id column="id" property="id"/>
<result column="oid" property="oid"/>
<!-- <association property="auction" javaType="com.yanshu.pojo.Auction" resultMap="auctionRequest" /> -->
<collection property="auction" ofType="com.yanshu.pojo.Auction">
<id column="id" property="id"/>
<result column="onsale" property="onsale"/>
</collection>
</collection>
</resultMap>
<resultMap type="com.yanshu.pojo.Auction" id="auctionRequest">
<id column="id" property="id"/>
<result column="onsale" property="onsale"/>
</resultMap>
<!--查询订单,关联查询用户信息,使用resultMap实现  -->
<select id="findUserOne" resultMap="moreOneResultMap">
select t.SellerNick,t.SellerId,o.oid from  open_Order o,TopUsers t where  o.id=t.id
</select>

<select id="findMore" resultMap="MoreResultMap" >
select t.SellerNick,t.SellerId,o.oid,a.id ,a.onsale from  open_Order o,TopUsers t ,open_Auction a where  o.id=t.id and a.id=o.id
</select>



</mapper>
阅读全文
0 0