mybatis-sql实战总结

动态条件查询

动态条件查询总结
注意点:
1.可以用where 1=1 也可以用 防止出现后面没有条件的情况
2.模糊查询用concat拼接
A.SSTRING10 like concat(#{adAccEntrie.sstring10},‘%’)
3. 对于前端传来的日期参数,使用STR_TO_DATE来转为统一的格式
STR_TO_DATE( #{adAccEntrie.date01}, ‘%Y-%m-%d’)

4.对于String类型的字段,不但要判断是不是null,还要判断是不是空字符串””.
对于集合类型的字段,如List,不但要判断是不是null,还需要判断是不是空的集合,用size判断就行。
对于Date类型的字段,只需要判断是不是null就行了。

//String类型怎么判断拼接
<if test="adAccEntrie.sstring08 != null and adAccEntrie.sstring08 != ''">
                AND A.SSTRING08 = #{adAccEntrie.sstring08}<!--来源系统-->
</if>
//集合类型字段判断怎么拼接                   
<if test="ids != null and ids.size() > 0">
  AND ID IN
  <foreach collection="ids" item="id" open="(" close=")" separator=",">
    #{id}
  </foreach>
</if>
//Date类型字段判断怎么拼接
<if test="adAccEntrie.date01 != null and adAccEntrie.date02  != null">
                AND A.DATE01 BETWEEN STR_TO_DATE( #{adAccEntrie.date01}, '%Y-%m-%d') AND STR_TO_DATE( #{adAccEntrie.date02}, '%Y-%m-%d' )<!--记账日期-->
            </if>

5.如果只查一个表 select *就足够了,但是如果有多个表select A.*就能查出指定表的所有字段了。

<!--    账户明细查询-->
    <select id="getAccEntrieListByParam" resultMap="AdAccEntrieMap">
        SELECT A.*
        FROM Ad_Acc_Entrie A
        <where>
            <if test="adAccEntrie.sstring08 != null and adAccEntrie.sstring08 != ''">
                AND A.SSTRING08 = #{adAccEntrie.sstring08}<!--来源系统-->
            </if>
            <if test="adAccEntrie.date01 != null and adAccEntrie.date02  != null">
                AND A.DATE01 BETWEEN STR_TO_DATE( #{adAccEntrie.date01}, '%Y-%m-%d') AND STR_TO_DATE( #{adAccEntrie.date02}, '%Y-%m-%d' )<!--记账日期-->
            </if>
            <if test="adAccEntrie.sstring01 != null and adAccEntrie.sstring01 != ''">
                AND A.SSTRING01 = #{adAccEntrie.sstring01}<!--借贷标识-->
            </if>
            <if test="adAccEntrie.mstring11 != null and adAccEntrie.mstring11 != ''">
                AND A.MSTRING11 = #{adAccEntrie.mstring11}<!--管理机构-->
            </if>
            <if test="adAccEntrie.sstring10 != null and adAccEntrie.sstring10 !=''">
                AND A.SSTRING10 like concat(#{adAccEntrie.sstring10},'%')<!--业务项目类型-->
            </if>
            <if test="adAccEntrie.lstring01 != null and adAccEntrie.lstring01 !=''">
                AND A.LSTRING01 like concat(#{adAccEntrie.lstring01},'%')<!--凭证类别-->
            </if>
            <if test="adAccEntrie.mstring07 != null and adAccEntrie.mstring07 != ''">
                AND A.MSTRING07 = #{adAccEntrie.mstring07}<!--业务号码-->
            </if>
            <if test="adAccEntrie.sstring17 != null and adAccEntrie.sstring17 != ''">
                AND A.SSTRING17 = #{adAccEntrie.sstring17}<!--业务场景编码-->
            </if>
            <if test="adAccEntrie.mstring02 != null and adAccEntrie.mstring02 != ''">
                AND A.MSTRING02 = #{adAccEntrie.mstring02}<!--科目代码-->
            </if>
        </where>

        limit #{ph.page},#{ph.rows}
        
    </select>

怎么List遍历集合进行拼接?

最终会拼接成 (元素1,元素2,元素3,元素4,元素5,…,元素n)

<if test="ids != null and ids.size() > 0">
  AND ID IN
  <foreach collection="ids" item="id" open="(" close=")" separator=",">
    #{id}
  </foreach>
</if>

相关推荐

  1. mybatis-sql实战总结

    2024-07-20 07:12:04       18 阅读
  2. Mybatis 动态Sql标签使用总结

    2024-07-20 07:12:04       28 阅读
  3. mybatis动态SQL常用语法总结

    2024-07-20 07:12:04       21 阅读
  4. MyBatis总结(2)- MyBatis实现原理(二)

    2024-07-20 07:12:04       18 阅读
  5. MyBatis总结(2)- MyBatis实现原理(三)

    2024-07-20 07:12:04       18 阅读
  6. Mybatis使用注解实现复杂动态SQL

    2024-07-20 07:12:04       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-20 07:12:04       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-20 07:12:04       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-20 07:12:04       45 阅读
  4. Python语言-面向对象

    2024-07-20 07:12:04       55 阅读

热门阅读

  1. Python--正则表达式re模块基础匹配方法

    2024-07-20 07:12:04       16 阅读
  2. 2024-07-19 Unity插件 Odin Serializer1 —— 插件介绍

    2024-07-20 07:12:04       17 阅读
  3. 【多商户自营解决方案】

    2024-07-20 07:12:04       16 阅读
  4. 基于深度学习的股票预测

    2024-07-20 07:12:04       15 阅读
  5. centos(或openEuler系统)安装clickhouse集群

    2024-07-20 07:12:04       14 阅读
  6. 介绍下项目的架构

    2024-07-20 07:12:04       12 阅读
  7. Docker 和 k8s 之间是什么关系?

    2024-07-20 07:12:04       14 阅读
  8. 软考高级第四版备考--第25天干系人绩效

    2024-07-20 07:12:04       16 阅读