定义接口
public interface StudentMapper {
public List<Student> getByAge(int age);
public List<Student> getStudent_Where(Student student);
public List<Student> getStudent_Where1(Student student);
public List<Student> getStudent_choosewhenotherwise(Map<String,Object> map);
public List<Student> getStudentForeach(Map<String,Object> map);
public List<Student> getStudentByTrim(Student student);
public void insertByTrim(Map<String,Object> map);
public void updateStudent(Student student);
}
xml绑定sql
<?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="cn.yhy.mapper.StudentMapper">
<!--if-->
<select id="getStudent_Where" resultType="Student" parameterType="Student">
select * from tbl_student where 1=1
<if test="age>20">
and age=#{age}
</if>
<if test="name!=null">
and name=#{name}
</if>
</select>
<!--where-->
<select id="getStudent_Where1" resultType="Student" parameterType="Student">
select * from tbl_student
<where>
<if test="name!=null &&name!=''">
name = #{name}
</if>
<if test="score!=null && score!=''">
score=#{score}
</if>
</where>
</select>
<!--choose-->
<select id="getStudent_choosewhenotherwise" resultType="Student" parameterType="map">
select * from tbl_student
<choose>
<when test="id!=null &&id!=''">
where id=#{id}
</when>
<when test="name!=null &&name!=''">
where name=#{name}
</when>
<otherwise>
where 1=1
</otherwise>
</choose>
</select>
<!--foreach-->
<select id="getStudentForeach" resultType="Student" parameterType="map">
select * from tbl_student
<if test="ages!=null">
<where>
age IN
<foreach collection="ages" item="val" open= "(" separator= "," close= ")">
#{val}
</foreach>
</where>
</if>
</select>
<!--trim查询-->
<select id="getStudentByTrim" parameterType="Student" resultType="Student">
select * from tbl_student
<trim prefix="where" prefixOverrides="and|or">
<if test="name!=null &&name!=''">
and name=#{name}
</if>
</trim>
</select>
<!--trim增加-->
<insert id="insertByTrim" parameterType="map">
insert into tbl_student
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="name != null">
name,
</if>
<if test="score != null">
score,
</if>
<if test="birth != null">
birth,
</if>
<if test="age != null">
age,
</if>
</trim>
<trim prefix=" values (" suffix=")" suffixOverrides=",">
<if test="name != null">
#{name},
</if>
<if test="score != null">
#{score},
</if>
<if test="birth != null">
#{birth},
</if>
<if test="age != null">
#{age},
</if>
</trim>
</insert>
<update id="updateStudent" parameterType="Student">
update tbl_student
<set>
<if test="name !=null and name !=''">name=#{name}</if>
<if test="score !=null and score !=''">score=#{score}</if>
<if test="birth !=null and birth !=''">birth=#{birth}</if>
<if test="age !=null and age !=''">age=#{age}</if>
</set>
where id=#{id}
</update>
</mapper>
测试类测试
public class TestStudent {
public static void main(String[] args) {
SqlSession session = SSF.getSqlSessionFactory().openSession();
try {
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student student=new Student();
student.setId(4);
student.setName("li8");
mapper.updateStudent(student);
session.commit();
} catch (Exception e) {
e.printStackTrace();
}finally {
session.close();
}
}
}