输入参数_MyBatis中的输入/输出参数
生活随笔
收集整理的這篇文章主要介紹了
输入参数_MyBatis中的输入/输出参数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
MyBatis的輸入/輸出參數
輸入參數
MyBatis中的Dao映射配置中的parameter屬性表示輸入參數類型,它對應Dao接口方法的參數類型有三種:
- 基本數據類型:如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>
輸出參數
輸出參數有兩種表現形式,分別為resultType和resultMap
1、resultType
resultType可以指定結果集的類型,它可以支持基本類型和實體類型
- 基本類型
<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標簽可以建立查詢的列名和實體類的屬性名稱不一致時建立關系,從而實現封裝。在 select 標簽中使用 resultMap 屬性指定引用即可。同時 resultMap 可以實現將查詢結果映射為復雜類 型的 pojo,比如在查詢結果映射對象中包括 pojo 和 list 實現一對一查詢和一對多查詢。
- 定義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>?
原創不易,如果您覺得這篇文章有用,請您安排「在看」、「點贊」、「轉發」,這將是對我最大的鼓勵!
總結
以上是生活随笔為你收集整理的输入参数_MyBatis中的输入/输出参数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 写入csv文件固定列_将元
- 下一篇: batchparser 无法加载_bat