Mybatis一对一,一对多,多对多代码

一对一

<!-- 关系映射 -->
    <!-- 1-1:自动映射 -->
    <select id="oneToOne" resultType="UserView">
        select u.*,c.num from user u,card c where u.id=c.per_fk
    </select>
    
    <!-- 1-1:手动映射之级联查询  -->
    <resultMap type="card" id="cardrs">
       <result property="num" column="num"/>
    </resultMap>
    <resultMap type="user" id="commrs">
       <id column="id" property="id"/>
       <result column="username" property="username"/>
       <result column="sex" property="sex"/>
    </resultMap>
    <resultMap type="user" id="oTors" extends="commrs">
       <!-- <id column="id" property="id"/>
       <result column="username" property="username"/>
       <result column="sex" property="sex"/> -->
       <!--association:完成自定义类型的映射
           property:User类中自定义类型(Card)的属性名
           javaType:指定对应属性的类型-->
       <association property="card" javaType="card" resultMap="cardrs">
          <!-- <result property="num" column="num"/> -->
       </association>
    </resultMap>
    <select id="oneToOne1" resultMap="oTors">
        select u.*,c.num from user u,card c where u.id=c.per_fk
    </select>

一对一(嵌套查询例)

<!-- 1-1:手动映射之嵌套查询(分步)  -->
    <resultMap type="User" id="oTors2" >
       <!-- <id column="id" property="id"/>
       <result column="username" property="username"/>
       <result column="sex" property="sex"/> -->
       <!-- select:调用另外一条sql语句(命名空间.sql语句id)
            column:在第一个结果集中用来关联查询的列名 -->
       <association property="card" javaType="card" 
       select="com.offcn.dao.UserDao.getCardByUid" column="id"></association>
    </resultMap>
    <select id="oneToOne2" resultMap="oTors2">
       select  * from user  
    </select>
    
    <select id="getCardByUid" parameterType="int" resultType="card">
       select * from card where per_fk=#{uid}
    </select>

一对多

<!-- 1-n:关联查询 -->
    <resultMap type="Orders" id="orderrs">
           <id column="oid" property="id"></id>
           <result column="number" property="number"/>
    </resultMap>
    <resultMap type="user" id="oTmrs" extends="commrs">
        <collection property="olist" ofType="Orders" resultMap="orderrs">
            <id column="oid" property="id"></id>
           <result column="number" property="number"/>
        </collection>
    </resultMap>
    <select id="oneToMany" resultMap="oTmrs">
       select u.*,o.id oid,o.number from user u,orders o where u.id=o.userId
    </select>
    
    <!--1-n:嵌套查询-->
    <resultMap type="user" id="oTmrs2">
        <collection property="olist" ofType="Orders" 
        select="com.offcn.dao.UserDao.getOrdersByUid" column="id"></collection>
    </resultMap>
    <select id="oneToMany2" resultMap="oTmrs2">
       select * from user
    </select>
    
    <select id="getOrdersByUid" parameterType="int" resultType="Orders">
       select * from orders where userId=#{uid}
    </select>

多对多

<resultMap type="user" id="mTmrs">
        <result column="username" property="username"/>
        <collection property="olist" ofType="Orders">
           <result property="number" column="number"/>
           
           <collection property="dlist" ofType="Orderdetail">
              <result column="itemsNum" property="itemsNum"/>
              
              <association property="items" javaType="Items">
                  <result column="name" property="name"/>
                  <result column="price" property="price"/>
              </association>
           </collection>
        </collection>
    </resultMap>
    <select id="manyToMany" resultMap="mTmrs">
       select u.username,o.number,i.name,i.price,d.itemsNum  
       from user  u,orders o,orderdetail d,items i 
       where u.id=o.userId and o.id=d.ordersId and d.itemsId=i.id
    </select>

嵌套查询和关联查询的区别:

1、关联查询是使用一条sql语句对数据库进行查询,在查询后根据查询结果将数据和自定义的resultMap集合返回结果

2、嵌套查询根据查询的内容分表查询,每个表至少查询一次,查询的结果嵌套使用。

3、关联查询的结果集必须将所有的属性都配置,没有配置的不会映射,嵌套查询如果有相同的属性会自动映射数据到对象

相关推荐