输入参数_MyBatis中的输入/输出参数
生活随笔
收集整理的這篇文章主要介紹了
输入参数_MyBatis中的输入/输出参数
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
MyBatis的輸入/輸出參數(shù)
輸入?yún)?shù)
MyBatis中的Dao映射配置中的parameter屬性表示輸入?yún)?shù)類型,它對應(yīng)Dao接口方法的參數(shù)類型有三種:
- 基本數(shù)據(jù)類型:如int、char等
????????insert?into?user(username)?values?(#{username})
????insert>
- 實體類:POJO類(要輸入全限定類名,如:com.simon.domain.User)
????????insert?into?user(username,birthday,sex,address)values?(#{username},#{birthday},#{sex},#{address})
????insert>
- 實體類包裝類
????????insert?into?user(username,birthday,sex)values?(#{username},#{birthday},#{sex})
????insert>
輸出參數(shù)
輸出參數(shù)有兩種表現(xiàn)形式,分別為resultType和resultMap
1、resultType
resultType可以指定結(jié)果集的類型,它可以支持基本類型和實體類型
- 基本類型
<select?id="findTotal"?resultType="int">?
?select?count(*)?from?user;
select>
- 實體類
<select?id="findAll"?resultType="com.simon.domain.User">
?select?*?from?user
select>
2、resultMap
resultMap標(biāo)簽可以建立查詢的列名和實體類的屬性名稱不一致時建立關(guān)系,從而實現(xiàn)封裝。在 select 標(biāo)簽中使用 resultMap 屬性指定引用即可。同時 resultMap 可以實現(xiàn)將查詢結(jié)果映射為復(fù)雜類 型的 pojo,比如在查詢結(jié)果映射對象中包括 pojo 和 list 實現(xiàn)一對一查詢和一對多查詢。
- 定義resultMap
<resultMap?id="userMap"?type="com.simon.domain.user">
????????
????????<id?property="userId"?column="id">id>
????????
????????<result?property="userName"?column="username">result>
????????<result?property="userAddress"?column="address">result>
????????<result?property="userSex"?column="sex">result>
????????<result?property="userBirthday"?column="birthday">result>
????resultMap>
- 映射的配置
<select?id="findAll"?resultMap="userMap">??
?select?*?from?user?
select>?
原創(chuàng)不易,如果您覺得這篇文章有用,請您安排「在看」、「點贊」、「轉(zhuǎn)發(fā)」,這將是對我最大的鼓勵!
總結(jié)
以上是生活随笔為你收集整理的输入参数_MyBatis中的输入/输出参数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 写入csv文件固定列_将元
- 下一篇: batchparser 无法加载_bat