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

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

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

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

接口定义如下:

List<DataInputInfo> getYearGatherDataInputData(Map<String,Object> params);

对应xml写法

<select id="getYearGatherDataInputData" parameterType="map" resultType="com.test.system.api.entity.BudgetInputInfo">
        select spbii.*
        from data_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 dynamicBatchRemoveAllTableData(Map<String,Object> params);

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

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

对应的xml文件

<delete id="dynamicBatchRemoveAllTableData" 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,不进行预编译,获取数据用$符号

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐