MyBatis之动态SQL

if 和 where

public interface UserDao {
    List<User> getUserListByCondition(User user);
}
<select id="getUserListByCondition" parameterType="User" resultType="User">
    select id, username, birthday, sex, address from user
    <where>
        <if test="username != null">
            and username = #{username}
        </if>
        <if test="sex != null">
            and sex = #{sex}
        </if>
    </where>
</select>

foreach

public interface UserDao {
    List<User> getUserListByIds(QueryVo vo);
}
package com.ttpfx.domain;

import java.util.List;

public class QueryVo {
    private List<Integer> ids;

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

    public void setIds(List<Integer> ids) {
        this.ids = ids;
    }
}
<select id="getUserListByIds" parameterType="QueryVo" resultType="User">
        select id, username, birthday, sex, address from user
    <where>
        <if test="ids != null and ids.size() > 0">
            <foreach collection="ids" item="id" open="and id in (" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </where>
</select>

参考

https://mybatis.org/mybatis-3/zh/dynamic-sql.html

上一篇:Avue-crud 表格进行多选删除,但是会删除掉多选以外的表格数据问题


下一篇:基于Roberta进行微博情感分析