博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

mybatis 所有标签实例以及注意事项

Posted on 2021-06-06 23:21  海绵谷  阅读(104)  评论(0编辑  收藏  举报

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>