mybatis映射文件的SQL深入

if标签

    <!--    根据条件查询用户-->
    <select id="findByCondition" parameterType="user" resultType="user">
        select * from user where 1=1
        <if test="username!=null">
            and username = #{username}
        </if>
        <if test="sex!=null">
            and sex = #{sex}
        </if>
    </select>

where标签

    <!--    根据条件查询用户-->
    <select id="findByCondition" parameterType="user" resultType="user">
        select * from user
        <where>
            <if test="username!=null">
                and username = #{username}
            </if>

            <if test="sex!=null">
                and sex = #{sex}
            </if>
        </where>
    </select>

foreach标签

QueryVo类:

public class QueryVo {

    User user;
    List<Integer> ids;

    public List<Integer> getIds() {
        return ids;
    }

    public void setIds(List<Integer> ids) {
        this.ids = ids;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

持久层Dao接口

 List<User> findByIds(QueryVo vo);

持久层Dao映射配置:

<!--根据id集合查询用户-->
    <select id="findByIds" parameterType="queryvo" resultType="user">
        select * from user
        <where>
            <if test="ids!=null and ids.size()>0">
                <foreach collection="ids" open="and id in (" close=")" item="id" separator=",">
                    #{id}<!--此处的大括号内容的书写和item一样-->
                </foreach>
            </if>
        </where>
    </select>

测试代码:

/**
     * 根据id集合查询
     */
    @Test
    public void testFindByIds(){
        QueryVo vo = new QueryVo();
        List<Integer> list = new ArrayList<>();
        list.add(41);
        list.add(42);
        list.add(43);
        vo.setIds(list);
        List<User> users = userDao.findByIds(vo);
        for (User u : users){
            System.out.println(u);
        }
    }

参考视频:https://www.bilibili.com/video/av47952553/?p=47 (p=46,47,48)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值