mybatis 「selectKey」 resutMap 「collections」的使用

1. Mybatis selectKey使用, 獲取insert對象後,返回主鍵id

mapper. insert(對象entity) 返回值void

<select>

     <selectKey keyProper ty="id" order="BEFORE", resultType="java.lang.String"> 

          select right(uuid_short(),20)

     </selectKey>

    insert into tablename(id,name)values(#{id}, #{name})

</select>


  在serviceImpl獲取時,    String id = 對象entity.getId()即拿到insert後返回主鍵id
 

2.Mybatis下 resutMap 和 <collections>的使用 

<!--  t_service 與  t_service_content 是一對多的關係-->
<!--  分頁查詢是單表查詢, 使用resultMap聚合查詢結果爲 對象套list -->
    <select resultMap="ServiceNewsMap">
      select * from t_service
      where limit #{startNum}, #{pageSize}
    </select>

<!--  ServiceNewsEntity就包含  List<ServiceNewsContentEntity>  contentEntityList   -->
1.帶分頁了,ServiceNewsContentEntity該實體類在下面出現了2次 
2.不帶分頁的,則沒有 column="id" select="queryServiceNewsContentById", 只出現一次
<collection標籤內的 javaType="java.util.List"可以不寫,推薦不寫

  <resultMap id="ServiceNewsMap" type="ServiceNewsEntity">
    <id column="id" property="id"/>
    <result column="name" property="name"/>
    <collection property="contentEntityList" ofType="com.winner.ServiceNewsContentEntity"
            column="id" select="queryServiceNewsContentById">
      <id column="service_news_id"  property="servicenewsId" />
      <result column="title"  property="title" />
      <result column="language"  property="language" />
    </collection>
  </resultMap>

  <select id="queryServiceNewsContentById" resultType="com.winner.ServiceNewsContentEntity">
    select * from t_service_content where servicenewsId=#{id}
  </select>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章