MyBatis(4)
一、DynamicSQL 动态SQL
1. if
标签——用于判断
test
属性:判断表达式OGNL
要求:查询哪个字段(即传入了哪个字段),那么查询条件就带上这个字段的值
EmployeeMapper接口
public List<Employee> getEmpsByConditionIf(Employee employee);
EmployeeMapper.xml
<select id="getEmpsByConditionIf" resultType="mybatis_4_DynamicSQL.com.Lemon.mybatis.bean.Employee">
select *
from mybatis.tbl_employee
<where>
<if test="id!=null">
id = #{id}
</if>
<if test="lastName!=null && lastName!=""">
and last_name like #{lastName}
</if>
<if test="email!=null and email.trim()!=""">
and email = #{email}
</if>
<if test="gender==0 or gender==1">
and gender = #{gender};
</if>
</where>
</select>
(1)where
标签:将所有的查询条件包含在内,但是只会去掉开头的第一个多出来的and或者or —— 解决sql拼串问题
(2)test
表达式中遇到特殊符号应写转义字符,具体参考OGNL官方文档
(3) 在if内部的查询条件中,and要写在前面
主函数
测试1:
// gender为空:查询条件不带gender —— where id = ? and last_name like ? and email = ?
Employee employee = new Employee(4, "%e%", "jerry@Lemon.com", null);
测试2:
// email和gender为空:查询条件不带email和gender —— where id = ? and last_name like ?
Employee employee = new Employee(4, "%e%", null, null);
测试3:
// id,email,gender为空,由于第一个条件不满足,所以需要使用where标签,否则会报错:where and last_name like ? —— 正确发送的sql:WHERE last_name like ?
Employee employee = new Employee(null, "%e%", null, null);
List<Employee> emps = mapper.getEmpsByConditionIf(employee);
for (Employee emp : emps) {
System.out.println(emp);
}
2. trim
标签——字符串截取
(1)prefix
:前缀,给拼串后的字符串加一个前缀
(2)prefixOverrides
:前缀覆盖,去掉整个字符串前面的字符
(3)suffix
:后缀,给拼串后的字符串加一个后缀
(4)suffixOverrides
:后缀覆盖,去掉整个字符串后面的字符
用途:如果在查询条件后面多出and或者or,where
标签是解决不了的,可以使用trim
标签进行截取
where
标签体的改进:
select *
from mybatis.tbl_employee
<trim prefix="where" suffixOverrides="and">
<if test="id!=null">
id = #{id} and
</if>
<if test="lastName!=null && lastName!=""">
last_name like #{lastName} and
</if>
<if test="email!=null and email.trim()!=""">
email = #{email} and
</if>
<if test="gender==0 or gender==1">
gender = #{gender};
</if>
</trim>
这里就表示在整个字符串前加上where
,在整个字符串后去掉and
3. choose-when-otherwise
:分支选择switch-case
要求:只查询一个,如果带了id就用id查,如果带了lastName就用lastName查
EmployeeMapper接口
public List<Employee> getEmpsByConditionChoose(Employee employee);
EmployeeMapper.xml
<select id="getEmpsByConditionChoose" resultType="mybatis_4_DynamicSQL.com.Lemon.mybatis.bean.Employee">
select *
from mybatis.tbl_employee
<where>
<choose>
<when test="id!=null">
id=#{id}
</when>
<when test="lastName!=null">
last_name like #{lastName}
</when>
<when test="email!=null">
email=#{email}
</when>
<otherwise>
gender=0
</otherwise>
</choose>
</where>
</select>
主函数
测试1:
// where last_name like ?
Employee employee = new Employee(null, "%e%", null, null);
测试2:
// where id=?
Employee employee = new Employee(1, "%e%", null, null);
测试3:
// where gender=0
Employee employee = new Employee(null, null, null, null);
List<Employee> emps = mapper.getEmpsByConditionChoose(employee);
for (Employee emp : emps) {
System.out.println(emp);
}
4、 set
标签
可以防止每个sql语句后的逗号
EmployeeMapper接口
public void updateEmp(Employee employee);
EmployeeMapper.xml
<update id="updateEmp">
update mybatis.tbl_employee
<set>
<if test="lastName!=null">
last_name=#{lastName},
</if>
<if test="email!=null">
email=#{email},
</if>
<if test="gender!=null">
gender=#{gender},
</if>
</set>
where id=#{id}
</update>
主函数
// SET last_name=? where id=?
Employee employee = new Employee(1, "Admin", null, null);
mapper.updateEmp(employee);
openSession.commit();
5、foreach
标签
(1)collection
:指定要遍历的集合名称
(2)item
:将当前遍历集合得到的元素赋值给指定的变量
(3)separator
:元素之间的分隔符
(4)open
:将遍历得到的所有结果,拼接一个开始的字符
(5)close
:将遍历得到的所有结果,拼接一个结束的字符
(6)index
:
遍历list时,index:索引,item:当前值
遍历map时,index:map的key,item:map的value
#{变量名}就能取出变量的值,也就是当前遍历集合得到的元素
EmployeeMapper接口
public List<Employee> getEmpsByConditionForeach(@Param("ids")List<Integer> ids);
EmployeeMapper.xml
<select id="getEmpsByConditionForeach" resultType="mybatis_4_DynamicSQL.com.Lemon.mybatis.bean.Employee">
select * from mybatis.tbl_employee
<foreach collection="ids" item="item_id" separator="," open="where id in (" close=")">
#{item_id}
</foreach>
</select>
主函数
// where id in ( ? , ? , ? , ? )
List<Employee> list = mapper.getEmpsByConditionForeach(Arrays.asList(1, 2, 3, 4));
for (Employee employee : list) {
System.out.println(employee);
}
等效于:<!-- where id in (1, 2, 3, 4) -->
6、 foreach
的批量保存
EmployeeMapper接口
public void addEmps(@Param("emps")List<Employee> emps);
EmployeeMapper.xml
<insert id="addEmps">
INSERT INTO mybatis.tbl_employee(last_name, email, gender, d_id)
VALUES
<foreach collection="emps" item="emp" separator=",">
(#{emp.lastName}, #{emp.email}, #{emp.gender}, #{emp.dept.id})
</foreach>
</insert>
主函数
// INSERT INTO mybatis.tbl_employee(last_name, email, gender, d_id) VALUES (?, ?, ?, ?) , (?, ?, ?, ?)
List<Employee> emps = new ArrayList<>();
emps.add(new Employee(null, "tom", "tom@126.com", "0", new Department(1)));
emps.add(new Employee(null, "frank", "frank@126.com", "1", new Department(2)));
mapper.addEmps(emps);
openSession.commit();
二、MyBatis的两个内置参数
(1)_parameter:代表整个参数
对于单个参数:_parameter就是这个参数
对于多个参数:参数会被封装成一个map,那么_parameter就是这个map
(2)_databaseId:
如果配置了DatabaseInProvider标签,那么_databaseId就是代表当前数据库的别名
三、bind
标签
可以将OGNL表达式的值绑定到一个变量中,方便后来引用这个变量的值
(1)name
:后续引用变量的名字
(2)value
:用于拼接的表达式
<bind name="_lastName" value="'%'+lastName+'%'"/>
<if test="_databaseId=='mysql'">
select * from mybatis.tbl_employee
<if test="_parameter!=null"> <!-- _parameter就表示employee对象 -->
where last_name like #{_lastName}
</if>
</if>
四、sql
标签
抽取可重用的sql片段,方便后面引用
1. sql抽取:将经常要查询或插入用的列名抽取出来,方面引用
2. 在sql语句中,可以用include来引用已经抽取出来的sql
3. include内部还可以自定义property,sql标签内部就能使用自定义的属性
4. include-property的取值的正确方式:${prop},不能用#{ }