第一节 if条件
第二节 choose,when和otherwise条件
Model
public class Student {
private int sno;
private String sname;
private int sage;
public Student() {
super();
}
public Student(int sno, String sname, int sage) {
super();
this.sno = sno;
this.sname = sname;
this.sage = sage;
}
public Student(String sname, int sage) {
super();
this.sname = sname;
this.sage = sage;
}
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getSage() {
return sage;
}
public void setSage(int sage) {
this.sage = sage;
}
@Override
public String toString() {
return "Student [sno=" + sno + ", sname=" + sname + ", sage=" + sage + "]";
}
}
》》》》》》》》》
StudentMapper.java
import java.util.List;
import java.util.Map;
import com.imis.model.Student;
public interface StudentMapper {
public List<Student> searchStudents(Map<String,Object> map);
public List<Student> searchStudents2(Map<String,Object> map);
}
》》》》》》》》》
StudentMapper.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="com.imis.mappers.StudentMapper">
<resultMap type="Student" id="StudentResult">
<id property="sno" column="sno"/>
<result property="sname" column="sname"/>
<result property="sage" column="sage"/>
</resultMap>
<select id="searchStudents" parameterType="Map" resultMap="StudentResult">
select * from student
where gradeID=#{gradeID}
<if test="sname!=null">
and sname like #{sname}
</if>
<if test="sage!=null">
and sage=#{sage}
</if>
</select>
<select id="searchStudents2" parameterType="Map" resultMap="StudentResult">
select * from student
<choose>
<when test="searchBy='gradeID'">
where gradeID=#{gradeID}
</when>
<when test="searchBy='sname'">
where sname=#{sname}
</when>
<otherwise>
where sage=#{sage}
</otherwise>
</choose>
</select>
</mapper>
》》》》》》》》》》
测试
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.imis.mappers.StudentMapper;
import com.imis.model.Student;
import com.imis.util.SqlSessionFactoryUtil;
public class StudentTest {
private static Logger logger=Logger.getLogger(StudentTest.class);
private SqlSession sqlSession=null;
private StudentMapper studentMapper=null;
@Before
public void setUp() throws Exception {
sqlSession=SqlSessionFactoryUtil.openSession();
studentMapper=sqlSession.getMapper(StudentMapper.class);
}
@After
public void tearDown() throws Exception {
sqlSession.close();
}
@Test
public void testSearchStudents() {
logger.info("查询学生(待条件)");
Map<String,Object> map=new HashMap<String,Object>();
map.put("gradeID", 1);
map.put("sname", "%张%");
map.put("sage", 12);
List<Student> studentList=studentMapper.searchStudents(map);
for(Student student:studentList) {
System.out.println(student);
}
sqlSession.commit();
}
@Test
public void testSearchStudents2() {
logger.info("查询学生(待条件2)");
Map<String,Object> map=new HashMap<String,Object>();
map.put("searchBy", "gradeID");
map.put("gradeID", 1);
map.put("sname", "%张%");
map.put("sage", 12);
List<Student> studentList=studentMapper.searchStudents2(map);
for(Student student:studentList) {
System.out.println(student);
}
sqlSession.commit();
}
}
第三节 where条件(最常用)
1,自动加上where;
2,如果where字句以and或者or开头,则自动删除第一个and或者or;
<select id="searchStudents3" parameterType="Map" resultMap="StudentResult">select * from student
<where>
<if test="gradeID!=null">
gradeID=#{gradeID}
</if>
<if test="sname!=null">
and sname like #{sname}
</if>
<if test="sage!=null">
and sage=#{sage}
</if>
</where>
</select>
第四节 trim条件
功能和where元素类似,提供了前缀,后缀功能,更加灵活
<select id="searchStudents4" parameterType="Map" resultMap="StudentResult">select * from student
<trim prefix="where" prefixOverrides="and/or">
<if test="gradeID!=null">
gradeID=#{gradeID}
</if>
<if test="sname!=null">
and sname like #{sname}
</if>
<if test="sage!=null">
and sage=#{sage}
</if>
</trim>
</select>
第五节 foreach循环
<select id="searchStudents5" parameterType="Map" resultMap="StudentResult">
select * from student
<if test="gradeIDs!=null">
<where>
gradeID in
<foreach item="gradeID" collection="gradeIDs" open="(" separator="," close=")"></foreach>
#{gradeID}
</where>
</if>
</select>
第六节 set条件(最常用)
1,自动加上set;
2,自动剔除最后一个逗号“,”;
<update id="updateStudent" parameterType="Student">update student
<set>
<if test="sname!=null">
sname=#{sname},
</if>
<if test="sage!=null">
sage=#{sage}, <!-- 最后一个,自动剔除 -->
</if>
</set>
where sno=#{sno}
</update>