关于mybatis中的resultType与resultMap用法及误区
来源:互联网 发布:java实现分段函数 编辑:程序博客网 时间:2024/06/04 20:27
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>
阅读全文
0 0
- 关于mybatis中的resultType与resultMap用法及误区
- Mybatis中的resultType与resultMap
- 关于mybatis配置文件中的resultMap和resultType
- mybatis 中resulttype 与resultmap用法
- mybatis中的resultMap与resultType的区别
- mybatis中的resultMap与resultType,傳參數#,$區別
- Mybatis中的resultType和resultMap
- Mybatis中的resultType和resultMap
- Mybatis中的resultType和resultMap
- Mybatis中的resultType和resultMap
- mybatis中的resultType和resultMap
- Mybatis中的resultType和resultMap
- Mybatis中的resultType和resultMap
- Mybatis中的resultMap和resultType
- Mybatis中的resultType和resultMap
- Mybatis中的resultType和resultMap
- Mybatis中的resultType和resultMap
- Mybatis中的resultType和resultMap
- 1.java常用关键字细节,构造函数
- rabbitmq问题解决方法汇总
- 弹性伸缩ESS
- 常用linux指令
- 验证结构中IMonitor的作用
- 关于mybatis中的resultType与resultMap用法及误区
- app切换到后台,点击图标快速启动
- React Native热更新(iOS)-Pushy
- JVM
- 模版方法模式
- postman
- 安卓面试清单----OKHttp源码解析(三)
- idea-spring+springmvc+thymeleaf搭建项目
- Iptables中DNAT、SNAT和MASQUERADE的理解