主键生成方式

  • 若数据库支持自动生成主键的字段(比如 MySQL 和 SQL Server),则可以设置 useGeneratedKeys=”true”,然后再把 keyProperty 设置到目标属性上。
1
2
3
4
5
6
7
8
9
10
11
<!-- public int insertEmployee(Employee employee); -->
<!--
让MyBatis自动的将自增id赋值给传入的employee对象的id属性
useGeneratedKeys="true":原生jdbc获取自增主键的方法;
keyProperty="":将刚才自增的id封装给哪个属性
-->
<insert id="insertEmployee" useGeneratedKeys="true"
keyProperty="id">
INSERT INTO t_employee(empname,gender,email)
VALUES(#{empName},#{gender},#{email})
</insert>
  • 而对于不支持自增型主键的数据库(例如 Oracle),则可以使用 selectKey 子元素:selectKey 元素将会首先运行,id 会被设置,然后插入语句会被调用

image-20200629100602263

1
2
3
4
5
6
7
8
9
10
11
12
<!-- public int insertEmployee2(Employee employee); -->
<insert id="insertEmployee2">
<!--查询主键
order="BEFORE":
在核心sql语句之前先运行一个查询sql查到id;将查到的id赋值给javaBean的哪个属性;
-->
<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
    • 我们也可以封装多个参数为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
<!--public List<Teacher> getTeacherByConditionChoose(Teacher teacher); -->
<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(&quot;&quot;)">
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
<!-- public int updateTeacher(Teacher teacher); -->
<update id="updateTeacher">
UPDATE t_teacher
<set>
<if test="name!=null and !name.equals(&quot;&quot;)">
teacherName=#{name},
</if>
<if test="course!=null and !course.equals(&quot;&quot;)">
class_name=#{course},
</if>
<if test="address!=null and !address.equals(&quot;&quot;)">
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
<!-- public List<Teacher> getTeacherByIdIn(List<Integer> ids); -->
<select id="getTeacherByIdIn" resultMap="teacherMap">

SELECT * FROM t_teacher WHERE id IN
<!-- 帮我们遍历集合的; collection="":指定要遍历的集合的key
close="":以什么结束
index="i":索引;
如果遍历的是一个list;
index:指定的变量保存了当前索引
item:保存当前遍历的元素的值
如果遍历的是一个map:
index:指定的变量就是保存了当前遍历的元素的key
item:就是保存当前遍历的元素的值
item="变量名":每次遍历出的元素起一个变量名方便引用
open="":以什么开始
separator="":每次遍历的元素的分隔符
(#{id_item},#{id_item},#{id_item} -->
<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
<!--public List<Teacher> getTeacherByCondition(Teacher teacher); -->
<select id="getTeacherByCondition" resultMap="teacherMap">
select * from t_teacher
<!-- test="":编写判断条件 id!=null:取出传入的javaBean属性中的id的值,判断其是否为空 -->
<!-- where可以帮我们去除掉前面的and; -->

<!-- trim:截取字符串
prefix="":前缀;为我们下面的sql整体添加一个前缀
prefixOverrides="": 取出整体字符串前面多余的字符
suffix="":为整体添加一个后缀
suffixOverrides="":后面哪个多了可以去掉; -->
<!-- 我们的查询条件就放在where标签中;每个and写在前面,
where帮我们自动取出前面多余的and -->
<trim prefix="where" prefixOverrides="and" suffixOverrides="and">
<if test="id!=null">
id > #{id} and
</if>
<!-- 空串 "" and; && or: ||; if():传入非常强大的判断条件;
OGNL表达式;对象导航图
Person
===lastName
===email
===Address
===city
===province
===Street
===adminName
===info
===perCount
方法、静态方法、构造器。xxx
在mybatis中,传入的参数可以用来做判断;
额外还有两个东西;
_parameter:代表传入来的参数;
1)、传入了单个参数:_parameter就代表这个参数
2)、传入了多个参数:_parameter就代表多个参数集合起来的map
_databaseId:代表当前环境
如果配置了databaseIdProvider:_databaseId就有值

-->
<!-- 绑定一个表达式的值到一个变量 -->
<!-- <bind name="_name" value="'%'+name+'%'"/> -->
<if test="name!=null &amp;&amp; !name.equals(&quot;&quot;)">
teacherName like #{_name} and
</if>
<if test="birth!=null">
birth_date &lt; #{birth} and
</if>
</trim>
</select>

bind(用处不大)

bind 元素可以从 OGNL 表达式中创建一个变量并将其绑定到上下文。比如:

image-20200629103500053

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>