主键生成方式
- 若数据库支持自动生成主键的字段(比如 MySQL 和 SQL Server),则可以设置 useGeneratedKeys=”true”,然后再把 keyProperty 设置到目标属性上。
1 2 3 4 5 6 7 8 9 10 11
|
<insert id="insertEmployee" useGeneratedKeys="true" keyProperty="id"> INSERT INTO t_employee(empname,gender,email) VALUES(#{empName},#{gender},#{email}) </insert>
|
- 而对于不支持自增型主键的数据库(例如 Oracle),则可以使用 selectKey 子元素:selectKey 元素将会首先运行,id 会被设置,然后插入语句会被调用

1 2 3 4 5 6 7 8 9 10 11 12
| <insert id="insertEmployee2">
<selectKey order="BEFORE" resultType="integer" keyProperty="id"> select max(id)+1 from t_employee </selectKey> INSERT INTO t_employee(id,empname,gender,email) VALUES(#{id},#{empName},#{gender},#{email}) </insert>
|
参数(Parameters)传递
- 单个参数
- 可以接受基本类型,对象类型,集合类型的值。这种情况MyBatis可直接使用这个参数,不需要经过任何处理。
- 多个参数
- 任意多个参数,都会被MyBatis重新包装成一个Map传入。Map的key是param1,param2,0,1…,值就是参数的值。
- 命名参数
- 为参数使用@Param起一个名字,MyBatis就会将这些参数封装进map中,key就是我们自己指定的名字
- POJO
- 当这些参数属于我们业务POJO时,我们直接传递POJO
- Map
动态SQL
if
1 2 3 4 5 6 7 8 9
| <select id = findUserByCondition" resultMap="userMap" select * from user where 1=1 <if test ="userName"!=null"> and username =#{userName} </if> <if test ="age"!=null"> and age =#{age} </if> </select>
|
where
1 2 3 4 5 6 7 8 9 10 11
| <select id = findUserByCondition" resultMap="userMap" select * from user <where> <if test ="userName"!=null"> and username =#{userName} </if> <if test ="age"!=null"> and age =#{age} </if> </where> </select>
|
choose (when, otherwise)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <select id="getTeacherByConditionChoose" resultMap="teacherMap"> select * from t_teacher <where> <choose> <when test="id!=null"> id=#{id} </when> <when test="name!=null and !name.equals("")"> teacherName=#{name} </when> <when test="birth!=null"> birth_date = #{birth} </when> <otherwise> 1=1 </otherwise> </choose> </where> </select>
|
set(解决最后一个,)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <update id="updateTeacher"> UPDATE t_teacher <set> <if test="name!=null and !name.equals("")"> teacherName=#{name}, </if> <if test="course!=null and !course.equals("")"> class_name=#{course}, </if> <if test="address!=null and !address.equals("")"> address=#{address}, </if> <if test="birth!=null"> birth_date=#{birth} </if> </set> <where> id=#{id} </where> </update>
|
foreach
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <select id="getTeacherByIdIn" resultMap="teacherMap"> SELECT * FROM t_teacher WHERE id IN
<if test="ids.size >0"> <foreach collection="ids" item="id_item" separator="," open="(" close=")"> #{id_item} </foreach> </if> </select>
|
trim
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| <select id="getTeacherByCondition" resultMap="teacherMap"> select * from t_teacher
<trim prefix="where" prefixOverrides="and" suffixOverrides="and"> <if test="id!=null"> id > #{id} and </if>
<if test="name!=null && !name.equals("")"> teacherName like #{_name} and </if> <if test="birth!=null"> birth_date < #{birth} and </if> </trim> </select>
|
bind(用处不大)
bind 元素可以从 OGNL 表达式中创建一个变量并将其绑定到上下文。比如:

include
1 2 3 4 5 6 7 8
| <!--抽取可重用的sql语句 --> <sql id="selectSql">select * from t_teacher</sql>
<!--public Teacher getTeacherById(Integer id); --> <select id="getTeacherById" resultMap="teacherMap"> <include refid="selectSql"></include> where id=#{id} </select>
|