基本知識
resultMap
- constructor–實(shí)例化的時(shí)候通過構(gòu)造器將結(jié)果集注入到類中
- oidArg– ID 參數(shù); 將結(jié)果集標(biāo)記為ID闽颇,以方便全局調(diào)用
- oarg–注入構(gòu)造器的結(jié)果集
- id–結(jié)果集ID讹开,將結(jié)果集標(biāo)記為ID捆交,以方便全局調(diào)用
- result–注入一個(gè)字段或者javabean屬性的結(jié)果
- association–復(fù)雜類型聯(lián)合;許多查詢結(jié)果合成這個(gè)類型
- 嵌套結(jié)果映射– associations能引用自身,或者從其它地方引用
- collection–復(fù)雜類型集合
- 嵌套結(jié)果映射– collections能引用自身,或者從其它地方引用
- discriminator–使用一個(gè)結(jié)果值以決定使用哪個(gè)resultMap
- case–基于不同值的結(jié)果映射
- 嵌套結(jié)果映射–case也能引用它自身, 所以也能包含這些同樣的元素疫蔓。它也可以從外部引用resultMap
- case–基于不同值的結(jié)果映射
http://blog.csdn.net/wxwzy738/article/details/24742495
一、一對一關(guān)系association
- association
association通常用來映射一對一的關(guān)系结借,例如筐摘,有個(gè)類user,對應(yīng)的實(shí)體類如下:(getter,setter方法省略)private String id;//主鍵 private String userName;//用戶姓名
有個(gè)類Article,對應(yīng)的實(shí)體類如下:private String id;//主鍵 private String articleTitle;//標(biāo)題 private String articleContent;//內(nèi)容
當(dāng)我想查詢一個(gè)用戶的時(shí)候,也查到他寫的一篇文章應(yīng)該如何寫呢?在user類中加入一個(gè)屬性articleprivate String id;//主鍵 private String userName;//用戶姓名 private Article article;//新增的文章屬性
- mapper.xml 配置
在user類的mapper.xml中這樣配置<resultMap id="userResultMap" type="test.mybatis.entity.User"> <id column="id" property="id" jdbcType="VARCHAR" javaType="java.lang.String"/> <result column="userName" property="userName"/> //這里把user的id傳過去 <association property="article" column="id" select="test.mybatis.dao.articleMapper.selectArticleByUserId" />//test.mybatis.dao.articleMapper為命名空間 </resultMap>
同時(shí)咖熟,article類的mapper.xml<resultMap id="articleResultMap" type="test.mybatis.entity.Article"> <id column="id" property="id" jdbcType="VARCHAR" javaType="java.lang.String"/> <result column="articleTitle" property="articleTitle" jdbcType="VARCHAR" javaType="java.lang.String"/> <result column="articleContent" property="articleContent" jdbcType="VARCHAR" javaType="java.lang.String"/> </resultMap> (當(dāng)然圃酵,這里還有查詢user表的語句,省略)
繼續(xù)在article的mapper.xml寫下如下<select id="selectArticleByUserId" parameterType="java.lang.String" resultMap="ArticleResultMap" > select * from tb_article where userId=#{userId} </select>
二馍管、一對多關(guān)系collection
- 實(shí)體類增加對應(yīng)屬性
private String id;//主鍵
private String userName;//用戶姓名
private List<Article> articleList;
- userMapper.xml配置
<resultMap id="userResultMap" type="test.mybatis.entity.User">
<id column="id" property="id" jdbcType="VARCHAR" javaType="java.lang.String"/>
<result column="userName" property="userName" jdbcType="VARCHAR" javaType="java.lang.String"/>
//這里把user的id傳過去
<collection property="articleList" column="id"
select="test.mybatis.dao.articleMapper.selectArticleListByUserId" />
</resultMap>
- userMapper.xml也可以這樣配置
question 與questionOption時(shí)一對多關(guān)系
<!-- User 連接查詢 方法配置 (一個(gè)問題對多個(gè)問題選項(xiàng)) -->
<resultMap type="Question" id="questionMap">
<id column="id" property="id"/>
<result column="display_id" property="displayId"/>
<result column="question_description" property="description"/>
<result column="is_deleted" property="isDeleted"/>
<collection resultMap="optionMap" property="questionOptionList" ofType="QuestionOption"/>
</resultMap>
<resultMap type="QuestionOption" id="optionMap">
<id column="option_id" property="id"/>
<result column="question_id" property="questionId"/>
<result column="option_descritpion" property="optionDescription" javaType="java.lang.String"/>
<result column="is_correct" property="isCorrect"/>
</resultMap>
<select id="getQuestionById" resultMap="questionMap" parameterType="int">
<![CDATA[
select
B.id as id,
B.display_id as display_id,
B.description as question_description,
B.is_deleted as is_deleted,
P.id as option_id,
P.question_id as question_id,
P.option_description as option_descritpion,
P.is_correct as is_correct
from question B
left outer join question_option P on (B.id = P.question_id)
where B.id = #{id}
]]>
</select>
幾點(diǎn)注意
- 在調(diào)用查詢方法的時(shí)候異常為
runtimeException
不會(huì)自己拋出郭赐,所以當(dāng)有問題時(shí),可以用try catch保住查錯(cuò) - result返回的類型記得對應(yīng)确沸,
resultType
時(shí)為java類resultMap
時(shí)填寫類型為map - 嵌套查詢捌锭,也就是第一種方法查詢時(shí),
collection
的column
為上層主鍵名罗捎,也就是說一般為傳入的參數(shù)
三观谦、批量更新
- 第一種
注意末尾要加號<update id="updateQuestionOptions" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" separator=";"> UPDATE question_option SET option_description = #{item.optionDescription},is_correct = #{item.isCorrect} WHERE id = #{item.id} </foreach> </update>
separator=";"
- 第二中
<update id="deleteQuestionByIds" parameterType="java.util.List">
UPDATE
question
SET
is_deleted = 1,
updated_time = NOW()
WHERE id in
<foreach collection="list" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach>
</update>
注意注意前后要加括號open="(" separator="," close=")"