Mybatis one to one, one to many, many-Code

One to One

 <! - relational mapping -> 
    <! - 1-1: automatically maps -> 
    < the SELECT the above mentioned id = "OneToOne" resultType = "userview" > 
        the SELECT * U, U c.num from the User, the WHERE Card c. = c.per_fk u.id 
    </ the SELECT > 
    
    <-! 1-1: manual mapping of cascaded look   -> 
    < resultMap of the type = "Card" the above mentioned id = "cardrs" > 
       < the Result Property = "NUM" column = "NUM" /> 
    </ The 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: the completion of a custom type mapping 
           property: User class custom type (Card) property name 
           javaType: Specifies the attributes corresponding to the type of -> 
       < Association Property = "Card" the javaType = "Card" The resultMap = "cardrs" > 
          <-! <Property Result = "NUM" column = "NUM" /> -> 
       </ Association > 
    </ The resultMap > 
    < SELECT ID = "oneToOne1" The resultMap = "oTors" > 
        SELECT * U, c.num. User U from, WHERE u.id Card C = c.per_fk 
    </ SELECT >

One (nested query cases)

<! - 1-1: manual mapping of nested queries (step)   -> 
    < The resultMap type = "the User" ID = "oTors2"  > 
       <! - <column ID = "ID" = Property "ID" /> 
       <the Result column = "username" Property = "username" /> 
       <= the Result column "Sex" Property = "Sex" /> -> 
       <-! the SELECT: call another one sql statement (namespace .sql statement ID) 
            column: in the first column to the result set associated with the query name -> 
       < association Property = "Card" the 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>

Many

<!-- 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>

Many to many

<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>
    

And nested queries associated with the query difference:

1, using a relational query sql statement querying a database, the set of data and a custom resultMap returns results when the query according to a query result

2, according to the contents of the query nested query sub-table queries, each query table at least once, the results of using nested queries.

3, associated with the query result set must be arranged all properties, no mapping is not nested query if the same attribute data is automatically mapped to the object

 

 

Guess you like

Origin www.cnblogs.com/meani/p/12001713.html