关于数据库查询

本文介绍了一个使用MyBatis实现的SQL条件查询示例,通过<if>标签进行动态SQL构建,支持按名称、公司、申办人及电话等字段进行模糊或精确查询,并按创建时间降序排列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

后台代码

<select id="selectWorkNeedList" parameterType="WorkNeed" resultMap="WorkNeedResult">
        <include refid="selectWorkNeedVo"/>
        <where>  
            <if test="name != null  and name != ''"> and n.name like concat('%', #{name}, '%')</if>
            <if test="company != null  and company != ''"> and n.company like concat('%', #{company}, '%')</if>
            <if test="declarant != null  and declarant != ''"> and n.declarant like concat('%', #{declarant}, '%')</if>
            <if test="telephone != null "> and n.telephone = #{telephone}</if>
            AND YEAR(n.create_time) = YEAR(NOW())
            ${params.dataScope}
        </where>
        order by n.create_time desc
    </select>

 <select>.....</select>语句中的 <if  ....>  </if>是用来过滤信息的。

and name like concat('%', #{name}是来进行模糊查询的,

telephone = #{telephone}时进行普通查询(查询信息必须输入完成)。

 order by ...desc,按照某个属性降序排列(由大到小)

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值