mybatis 模糊查询

1.入参为对象的时候
List<AppUser> queryUser(AppUser user);对应的sql

<!--模糊查询 对象-->
    <select id="queryUser" resultType="AppUser">
        select
        <include refid="dto" /> from USER where
        <if test="phone != null and phone != ''">
            <bind name="uphone" value="'%'+phone+'%'"/>
            phone like #{uphone}
        </if>
         <if test="name != null and name != ''">
             <bind name="uname" value="'%'+name+'%'"/>
             and name like #{uname}
         </if>
         order by id desc
    </select>

2.入参类型为Map,且没有加注解的时候,key 为phone、name
List<AppUser> queryUserBy2Map(HashMap<String,Object> mp);对应的sql

<select id="queryUserBy2Map" parameterType="java.util.HashMap" resultType="AppUser" >
        select
        <include refid="dto" /> from USER where
        <if test="phone != null and phone != ''">
            <bind name="uphone" value="'%'+phone+'%'"/>
            phone like #{uphone}
        </if>
        <if test="name != null and name != ''">
            <bind name="uname" value="'%'+name+'%'"/>
            and name like #{uname}
        </if>
        order by id desc
    </select>

3.入参类型为Map,但是dao 层添加注解@Param,key为phone,name
List<AppUser> queryUserByMap(@Param("appUser") HashMap mp);对应的sql

<!--模糊查询 map 方式,dao层加@Param("appUser") 注解-->
    <select id="queryUserBy2Map" parameterType="java.util.HashMap" resultType="AppUser" >
        select
        <include refid="dto" /> from USER where
        <if test="phone != null and phone != ''">
            <bind name="uphone" value="'%'+phone+'%'"/>
            phone like #{uphone}
        </if>
        <if test="name != null and name != ''">
            <bind name="uname" value="'%'+name+'%'"/>
            and name like #{uname}
        </if>
        order by id desc
    </select>
上一篇:jmeter变量嵌套函数的使用


下一篇:面向对象与面向过程