xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="">
<select id="selectTeacher" parameterType="map" resultType="map">
select t.tid,t.tname,t.taddr
from test_teacher t
where 1=1
<if test="tid != '' and tid != null">
and tid=#{tid}
</if>
<if test="tname != '' and tname != null">
and tname like #{tname}
</if>
<if test="addrs != '' and addrs != null">
and taddr in
<foreach item="item" index="index" collection="addrs" open="("
separator="," close=")">
#{item}
</foreach>
</if>
;
</select>
</mapper>
dao层:
package mybatis.dao;
import java.util.List;
public interface TestTeacherDao {
public List selectTeacher(String tid,String tname,List addrs);
}
<!--动态SQL语句,多条件查询-->
<select id="findSomeByMore" resultType="userInfoModel" parameterType="hashmap">
select * from userinfo
-- (where标签自动去掉满足条件的第一个and)
<where>
<if test="userName!=null">
and username=#{userName}
</if>
<if test="gender != null">
and gender=#{gender}
</if>
<if test="orderId != null">
and id in (select userid from orders where id=#{orderId})
</if>
</where>
</select>
批量删除
<delete id="partDelete" parameterType="list" >
delete from userinfo
<where>
<if test="list!=null and list.size()>0">
<foreach collection="list" item="id" open="id in (" separator="," close=")">
#{id}
</foreach>
</if>
</where>
</delete>