myBatis动态sql语句:if标签和where标签
@Test
public void test6(){
List<HashMap> list = BatisMapper.getSqlSession().test4(5000, "s");
System.out.println(list);
System.out.println(list.size());
}
@Test
public void test7(){
List<HashMap> list = BatisMapper.getSqlSession().test5(5000, 100000, "s");
System.out.println(list);
System.out.println(list.size());
}
// 接口
public List<HashMap> test4(@Param("salary") int salary,@Param("job") String job);
<select id="test4" resultType="HashMap">
SELECT * FROM emp WHERE 1=1
<if test="salary != null and salary != '' and salary != 0 ">
and salary > #{salary}
</if>
<if test="job != null and job != ''">
and job like concat("%",#{job},"%")
</if>
</select>
<select id="test5" resultType="HashMap">
SELECT * FROM emp
<where>
<if test="salary != null and salary != '' and salary != 0 ">
and salary > #{salary}
</if>
<if test="bonus != bonus and bonus != '' and bonus != 0 ">
and bonus > #{bonus}
</if>
<if test="job != null and job != ''">
and job like concat("%",#{job},"%")
</if>
</where>
</select>