使用myabtis中的动态sql语句实现多条件查询

2023-09-12

StoreHouseMapper

package com.hh.mapper;

import com.hh.pojo.StoreHouse;

import java.util.List;

/**
 * @author hh
 * @version 1.0
 * @DATE 2023-09-12 14:09:48
 */
public interface StoreHouseMapper {

    List<StoreHouse> selectSHByCondition(StoreHouse storeHouse);
    List<StoreHouse> selectSHByConditionTwo(StoreHouse storeHouse);
}

StoreHouseMapper.xml

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.hh.mapper.StoreHouseMapper">


    <select id="selectSHByCondition" resultType="com.hh.pojo.StoreHouse">
        select * from t_storehouse
        <where>
            <if test="sName!=null and sName!=''">
                s_name like "%"#{sName}"%"
            </if>
            <if test="sType!=null and sType!=''">
                and s_type like "%"#{sType}"%"
            </if>
            <if test="sDescription!=null and sDescription!=''">
                and s_description like "%"#{sDescription}"%"
            </if>
        </where>
    </select>

    <select id="selectSHByConditionTwo" resultType="StoreHouse">
        select * from t_storehouse
        <trim prefix="where" suffixOverrides="and">
            <if test="sName!=null and sName!=''">
                s_name like "%"#{sName}"%" and
            </if>
            <if test="sType!=null and sType!=''">
                s_type like "%"#{sType}"%" and
            </if>
            <if test="sDescription!=null and sDescription!=''">
                 s_description like "%"#{sDescription}"%"
            </if>
        </trim>
    </select>
</mapper>

 

posted @ 2023-09-12 14:31  努力是一种常态  阅读(11)  评论(0编辑  收藏  举报