mybatis动态sql

多条件查询

if
/*
多条件查询 String ename,String job, double sal
 */
public  List<Emp> selectEmpByCondition1(Map<String,Object> searchMap);

public  List<Emp> selectEmpByCondition2(String ename,String job, Double sal);

public  List<Emp> selectEmpByCondition3(SearchEmp searchEmp);
<resultMap id="empResult" type="Emp">
    <id column="empno" property="no"></id>
    <result column="ename" property="name"></result>
    <result column="job" property="job"></result>
    <result column="hiredate" property="hiredate"></result>
    <result column="sal" property="sal"></result>
</resultMap>
<select id="selectEmpByCondition1" parameterType="hashmap" resultMap="empResult">
        select * from emp where 1=1
        <if test="ename !=null ">
            and  ename like concat("%",#{ename},"%")
        </if>
    <if test="job !=null">
        and job= #{job}
    </if>
    <if test="sal != null" >
        and sal > #{sal}
    </if>
</select>
<select id="selectEmpByCondition2"   resultMap="empResult">
    select * from emp where 1=1
    <if test="param1 !=null ">
        and  ename like concat("%",#{param1},"%")
    </if>
    <if test="param2 !=null">
        and job= #{param2}
    </if>
    <if test="param3 != null" >
        and sal > #{param3}
    </if>

</select>
<select id="selectEmpByCondition3" parameterType="SearchEmp" resultMap="empResult">
    select * from emp where 1=1
    <if test="ename !=null ">
        and  ename like concat("%",#{ename},"%")
    </if>
    <if test="job !=null">
        and job= #{job}
    </if>
    <if test="sal != null" >
        and sal > #{sal}
    </if>
</select>
where
<select id="selectEmpByCondition1" parameterType="hashmap" resultMap="empResult">
        select * from emp`在这里插入代码片`
        <where>
            <if test="ename !=null ">
               and ename like concat("%",#{ename},"%")
            </if>
            <if test="job !=null">
               and job= #{job}
             </if>
            <if test="sal != null" >
              and  sal > #{sal}
            </if>
        </where>
</select>
foreach
public List<Emp> selectEmpByDeptno(List<Integer> deptnoList);
<select id="selectEmpByDeptno" parameterType="list"  resultMap="empResult">
    select * from emp
    <where>
        deptno in
        <foreach item="deptno" index="index" collection="list"
                 open="(" separator="," close=")">
            #{deptno}
        </foreach>
    </where>

</select>
<!--    公共sql-->
    <sql id="basesql">
          select * from emp
    </sql>
    <select id="selectEmpByCondition1" parameterType="hashmap" resultMap="empResult">
            <include refid="basesql"></include>
            <where>
                <if test="ename !=null ">
                   and ename like concat("%",#{ename},"%")
                </if>
                <if test="job !=null">
                   and job= #{job}
                 </if>
                <if test="sal != null" >
                  and  sal > #{sal}
                </if>
            </where>
    </select>

你可能感兴趣的:(mybatis)