mybatis的使用技巧1——mybatis之万能map的介绍和使用

10 篇文章 0 订阅
7 篇文章 0 订阅

在java实际项目开发过程中,查询操作其实是最多最常见的。

例如数据的查询,多个模块或者微服务之间的接口调用等,都涉及大量Vo数据模型转换,这个时候利用万能Map就可以很好的解决问题,还能提高工作效率。

注意一点,map作为参数时,在parameterType这里可以不定义。

1.数据查询,直接调用数据接口层

接口定义如下:

List<BudgetInputInfo> getYearGatherPlanInputData(Map<String,Object> params);

对应xml写法

<select id="getYearGatherPlanInputData" parameterType="map" resultType="com.test.system.api.entity.BudgetInputInfo">
        select spbii.*
        from budget_input_info spbii
        <where>
            spbii.del_flag = '0' and spbii.status = 1
            <if test="staId != null "> and spbii.sta_id = #{staId}</if>
            <if test="planId != null "> and spbii.plan_id = #{planId}</if>
            <if test="planSn != null  and planSn != ''"> and spbii.plan_sn like concat('%', #{planSn}, '%')</if>
            <if test="qoutaType != null  and qoutaType != ''"> and spbii.qouta_type = #{qoutaType}</if>
            <if test="allQoutaType != null  and allQoutaType == '05'"> and spbii.qouta_type != '05'</if>
            <if test="quotaStr != null  and quotaStr != ''"> and spbii.quota_str like concat('%', #{quotaStr}, '%')</if>
            <if test="inputBudget != null "> and spbii.input_budget = #{inputBudget}</if>
            <if test="dataType != null and dataType != ''"> and spbii.data_type = #{dataType}</if>
            <if test="planType != null and planType != ''"> and spbii.plan_type = #{planType}</if>
        </where>
        order by spbii.create_time desc
    </select>

注意事项:

  • 为什么不直接使用BudgetInputInfo对象?因为xml中allQoutaType和quotaStr属性不属于当前对象,原始对象是不允许做处理的,因此这条sql是支持任意查询的,灵活度高,复用性强。
  • resultType可以更改成resultMap,如果不想在xml定义返回结果集,就直接在resultType中写对应的数据模型全类名,省事简单,但是sql复用性会变差,这块根据实际情况做处理即可。
2.自定义删除多数据表,可自定义使用同一参数或者对应的参数集合均可以

例如存在如下接口:

int dynamicBatchRemoveAllWeekData(Map<String,Object> params);

通过构造自定义参数,可以实现万能map高阶技能,支持一次性动态删除多个数据表

Map<String,Object> params = new HashMap<>();
        params.put("tableList",new String[]{
                "week_disaster_response_child",
                "week_important_work_child",
                "week_common_child"
        });
        //测试数据仅供参考
        params.put("ids",new Long[]{1L,2L});
        params.put("tempTypeArray",new String[]{"01","02"});

对应的xml文件

<delete id="dynamicBatchRemoveAllWeekData" statementType="STATEMENT">
        <foreach collection="tableList" item="tabName" index="index" >
            delete from ${tabName} where bi_id in
            <foreach item="id" collection="ids" open="(" separator="," close=")">
                ${id}
            </foreach>
            <if test="tabName == 'scrp_plan_input_week_common_child' ">
                and temp_type in
                <foreach item="tempType" collection="tempTypeArray" open="(" separator="," close=")">
                     ${tempType}
                </foreach>
            </if>
            ;
        </foreach>
    </delete>

注意该xml使用了statementType属性,该属性直接操作sql,不进行预编译,获取数据用$符号

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值