關於mybatis中的resultType與resultMap用法及誤區
1.resultType與resultMap
resultType:指定返回資料的型別 如果為List 此處應填對應的java Object 注:select的條件最好與Object裡的欄位一一對應,避免引發其他錯誤(大小寫敏感)
resultMap:指定返回的資料為定義好的XXXMap,其中的欄位必須一一對應(大小寫敏感)
2.foreach傳參
傳入是單一引數List ——foreach裡的collection為list
傳入是多個引數,需要用Map傳參 ——foreach裡的collection為map裡的key值
3.batch insert
insert on duplicate key and batch
<insert id="batchInsertOnDuplicateDate" parameterType="com.test.op.interfaces.vo.StatisticsDataForFwyyVO" >
<foreach collection="list" item="dataForFwyyVOs" index="index" open="" close="" separator=";">
insert into tb_statistics (create_time,data_time,
...)
values
(
#{dataForFwyyVOs.createTime,jdbcType=VARCHAR},
#{dataForFwyyVOs.dataTime,jdbcType=DATE},
... )
on duplicate key update
create_time = #{dataForFwyyVOs.createTime,jdbcType=VARCHAR},
...
</foreach>
/*StatisticsDataFwyy batchInsertOnDuplicateDate*/
</insert>
4.batch update
批量執行多條update語句
<update id="updateBySnIds" parameterType="java.util.Map">
<foreach collection="snIds" item="item" index="index" open="" close="" separator=";">
update sn_card
set is_saled = 1, order_num = #{orderNum,jdbcType=VARCHAR},
...
where id = #{item.snId} and version = #{item.version}
</foreach>
/*SNCard updateBySnIds*/
</update>
執行單條update語句
<update id="deleteByIdsIn">
update service_sku
set is_deleted = 1
<if test="#{0} != null and #{0}.size > 0">
where id in
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
</if>
5.case when
<update id="updateBatch" parameterType="java.util.List" >
update tb_address
<trim prefix="set" suffixOverrides=",">
<trim prefix="businessId =case" suffix="end,">
<foreach collection="list" item="item" index="index">
when id=${item.id} then #{item.businessId}
</foreach>
</trim>
<trim prefix="area = case" suffix="end,">
<foreach collection="list" item="item" index="index">
when id=${item.id} then ${item.area}
</foreach>
</trim>
<trim prefix=" province =case" suffix="end,">
<foreach collection="list" item="item" index="index">
when id=${item.id} then ${item.province}
</foreach>
</trim>
<trim prefix=" city =case" suffix="end,">
<foreach collection="list" item="item" index="index">
when id=${item.id} then ${item.city}
</foreach>
</trim>
<trim prefix=" county =case" suffix="end,">
<foreach collection="list" item="item" index="index">
when id=${item.id} then ${item.county}
</foreach>
</trim>
...
updateTime=now()
</trim>
where id in
<foreach collection="list" open="(" close=")" separator="," item="item" index="index" >
${item.id}
</foreach>
/*address updateBatch*/
</update>
6.otherwise
組合多個查詢條件 不建議使用
<select id="countTotalAmountForFwyy" parameterType="java.util.Map" resultType="java.math.BigDecimal">
select sum(actual_price) from tb_order
where
<choose>
<when test="classifyId != 0">
classify_id = #{classifyId,jdbcType=INTEGER}
</when>
<otherwise>
classify_id in (1,2,4)
</otherwise>
</choose>
<choose>
<when test="startDate != null">
and pay_time >= #{startDate,jdbcType=TIMESTAMP} and pay_time <= #{endDate,jdbcType=TIMESTAMP}
</when>
<otherwise>
and pay_time <= #{endDate}
</otherwise>
</choose>
and actual_price > 0 and be_del=false
/*order countTotalAmountForFwyy*/
</select>
相關文章
- mybatis xml裡的 resultMap、resultOrdered、resultSets、resultSetType、resultType 區別MyBatisXML
- Mybatis 輸出對映-- resultType resultMapMyBatis
- mybatis之sql查詢配置檔案resultType和resultMapMyBatisSQL
- myBatis——註解,#{}與${},resultMap的使用MyBatis
- resultMap 和 resultType 的欄位對映覆蓋問題
- Mybatis筆記03---ResultMap及分頁MyBatis筆記
- CSS中的class與id區別及用法CSS
- mybatis中resultMap使用之返回分組資料MyBatis
- EXTJs 中icon 與iconCls的區別及用法!JS
- vue中 關於$emit的用法VueMIT
- 關於JavaScript中arguments的用法JavaScript
- 關於SQL開發規範中的那些誤區!SQL
- Mybatis中updateByPrimaryKeySelective和updateByPrimaryKey的用法區別MyBatis
- ajax與jsonp的區別及用法JSON
- thchnique 與 technology的區別及用法
- Gerrit的用法及與gitlab的區別Gitlab
- js中!和!!的區別與用法JS
- 關於Mybatis中SQL語句的整理MyBatisSQL
- exists和not exists及in和not in的用法與區別
- Python進階:切片的誤區與高階用法Python
- mybatis #與$的區別MyBatis
- Mybatis中#{}和${}傳參的區別及#和$的區別小結MyBatis
- Linux中wget、yum與apt-get用法及區別Linuxwgetapt-get
- 關於mssql中go等幾個關鍵字的用途及區別SQLGo
- hive中round、floor、ceil區別及用法Hive
- 關於資料抓取很多新人的誤區
- 關於PHP 時區錯誤的問題PHP
- Mybatis 強大的結果集對映器resultMapMyBatis
- mybatis原始碼學習------resultMap和sql片段的解析MyBatis原始碼SQL
- 關於 in與exist , not in與not exist 的區別
- 關於C#中async/await的用法C#AI
- 關於shell中的pl/sql指令碼錯誤排查與分析SQL指令碼
- 關於 mybatis-plus 與JPA 混合使用MyBatis
- 關於js中的‘==’ 與 ‘===’JS
- 【Mybatis系列】從原始碼角度理解Mybatis欄位對映-AS&ResultMapMyBatis原始碼
- 專案中關於解構的常用用法
- (八)Mybatis當中#{}常用屬性的用法MyBatis
- JavaScript中apply、call、bind的區別與用法JavaScriptAPP