知識點:主要介紹mybatis中,動態sql中的if,where,trim,set,foreach的用法
自學谷粒學院mybatis學習視頻,參考mybatis官方文檔
java包:log4j.jar
mybatis-3.4.1jar
mysql-connector-java-5.1.37-bin.jar
實體類:
Employee.java類
package com.hand.mybatis.bean;
public class Employee {
private Integer eId;
private String eName;
private Integer gender;
private String email;
private Department dept;
public Employee() {
super();
}
public Employee(Integer eId,String eName, Integer gender, String email) {
super();
this.eId=eId;
this.eName = eName;
this.gender = gender;
this.email = email;
}
public Employee(Integer eId, String eName, Integer gender, String email, Department dept) {
super();
this.eId = eId;
this.eName = eName;
this.gender = gender;
this.email = email;
this.dept = dept;
}
public Integer geteId() {
return eId;
}
public void seteId(Integer eId) {
this.eId = eId;
}
public String getEName() {
return eName;
}
public void setEname(String ename) {
this.eName = ename;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Employee [eId=" + eId + ", ename=" + eName + ", gender=" + gender + ", email=" + email + "]";
}
}
Department.java實體類
package com.hand.mybatis.bean;
import java.util.List;
public class Department {
private Integer id;
private String departName;
private List<Employee> empList;
public Department() {
super();
}
public Department(Integer id) {
super();
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDepartName() {
return departName;
}
public void setDepartName(String departName) {
this.departName = departName;
}
public List<Employee> getEmpList() {
return empList;
}
public void setEmpList(List<Employee> empList) {
this.empList = empList;
}
@Override
public String toString() {
return "Department [id=" + id + ", departName=" + departName + "]";
}
}
EmployeeMapperDynamicSQL.java mapper接口
package com.hand.mybatis.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.hand.mybatis.bean.Employee;
public interface EmployeeMapperDynamicSQL {
//使用if
List<Employee> getEmpsByconditionIf(Employee employee);
//使用trim()
List<Employee> getEmpsByconditionTrim(Employee employee);
//使用choose
List<Employee> getEmpsByconditionChoose(Employee employee);
//跟新字段(set)
int updateEmp(Employee employee);
//根據list集合條件,查詢集合(foreach遍歷list集合條件)
List<Employee> getEmpsByConditionForeach(@Param("idlist") List<Integer> idlist);
//批量保存(foreach插入多條數據)
int addEmpsBatch(@Param("emps") List<Employee> emps);
}
EmployeeMapperDynamicSQL.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="com.hand.mybatis.dao.EmployeeMapperDynamicSQL">
<!-- • if
• choose (when, otherwise):分支選擇,swtich-case
如果帶了eid就用eid查詢,如果帶了eName就用eName查詢,只會進入其中一個
• trim 字符串截取(where(封裝查詢條件), set(封裝修改條件))
• foreach -->
<!-- //使用if -->//一般if用在sql條件的多字段拼接,if判斷字段不為null,"",等情況下
<!-- public List<Employee> getEmpsByconditionIf(Employee employee); -->
<!-- 查詢員工,要求,攜帶了那個字段查詢條件就帶上那個字段的值 -->
<select id="getEmpsByconditionIf" resultType="com.hand.mybatis.bean.Employee">
select * from emp
<where>
<!-- where 1=1 --><!-- test,判斷表達式(OGNL)
從參數中取值判斷,遇見特殊符號去寫轉義字符
w3c: ISO 8859-1
&&,&&
"" ""-->
<if test="eId!=null">
eid=#{eId}
</if>
<if test="eName!=null && eName!= """>
and ename like #{eName} //and一般放在字段前面,《where》標簽不會報錯
</if>
<if test="email!=null and email.trim()!="""> //一些字符如果出錯,可以ws3=>HTML ISO-8859-1參考手冊里查找對應的字符,如mybatis里寫
and email=#{email} 大小與號(><)出錯,改成<(<)>(>)則不會報錯
</if>
<!-- ognl會進行字符串與數字的轉換判斷“0”==0 -->
<if test="gender==0 or gender==1">
and gender=#{gender}
</if>
</where>
</select>
<!-- 使用trim -->
<!-- List<Employee> getEmpsByconditionTrim(Employee employee); -->
<select id="getEmpsByconditionTrim" resultType="com.hand.mybatis.bean.Employee">
select * from emp
<!--后面多出來的and或者or where 標簽不能解決
trim標簽中是整個字符串拼接后的結果
prefix="":給拼接后的整個字符串加一個前綴
prefixOverrides="":前綴覆蓋,去掉整個字符串前面多余的字符
suffix="":后綴 suffix給拼接后的整個字符串加一個后綴
suffixOverrides="" 后綴覆蓋,去掉整個字符串后面多余的字符-->
<!-- 自定義字符串的截取規則 -->
<trim prefix="where" suffixOverrides="and">//這里意思是在條件最前加where,去掉每個if條件里的字段后面多余的and,就不會報錯
<if test="eId!=null">
eid=#{eId} and
</if>
<if test="eName!=null && eName!= """>
ename like #{eName} and
</if>
<if test="email!=null and email.trim()!=""">
email=#{email} and
</if>
<!-- ognl會進行字符串與數字的轉換判斷“0”==0 -->
<if test="gender==0 or gender==1">
gender=#{gender}
</if>
</trim>
</select>
<!-- //使用choose
List<Employee> getEmpsByconditionChoose(Employee employee); -->
<select id="getEmpsByconditionChoose" resultType="com.hand.mybatis.bean.Employee">
select * from emp
<where>
<!-- 如果帶了eid就用eid查詢,如果帶了eName就用eName查詢,只會進入其中一個 -->
<choose>
<when test="eId!=null">//只會選擇一條路徑,若同時有兩個字段傳入,則優先選擇前面的字段
eid=#{eId}
</when>
<when test="eName!=null">
ename=#{eName}
</when>
<when test="gender!=null">
gender=#{gender}
</when>
<when test="email!=null">
email=#{email}
</when>
<otherwise>
1=1 //當所有字段都為空時,查詢原表中所有記錄
</otherwise>
</choose>
</where>
</select>
<!-- //跟新字段 傳那個字段跟新那個字段(使用set)
void updateEmp(Employee employee); -->
<update id="updateEmp">
UPDATE emp
<set> <!-- //方法一:使用set標簽去掉多余的,號 -->
<if test="eName!=null">
ename=#{eName},
</if>
<if test="gender!=null">
gender=#{gender},
</if>
<if test="email">
email=#{email}
</if>
</set>
<!-- 方法二:使用trim標簽去掉多余的,號
<trim prefix="set" suffixOverrides=",">
<if test="eName!=null">
ename=#{eName},
</if>
<if test="gender!=null">
gender=#{gender},
</if>
<if test="email">
email=#{email}
</if>
</trim> -->
WHERE eid=#{eId}
</update>
<!-- //根據list集合條件,查詢集合(foreach遍歷list集合條件)
List<Employee> getEmpsByConditionForeach(List<Integer> idlist); -->
<select id="getEmpsByConditionForeach" resultType="com.hand.mybatis.bean.Employee">
<!-- select * from emp where eid in(101,102,103) 方法一:in(1,2,3)--> //原始傳入多個id的情況,用in(1,2,3)
select * from emp where eid in
<!--
collection:指定要遍歷的集合
list類型的參數會特殊處理封裝在map中,map的key就是list
item:將當前遍歷的元素賦值給指定的變量
separator:每個元素之間的分隔符
open:遍歷出所有結果拼接一個開始的字符
close:遍歷出所有結果拼接一個結束的字符
index:索引,遍歷list的時候,index就是索引,item就是當前值
遍歷map的時候index表示就是map的key,item就是map的值
#{變量名}:每個元素之間的分隔符
-->
<foreach collection="idlist" item="itemid" separator=","
open="(" close=")">
#{itemid}
</foreach>
</select>
<!-- 批量保存(foreach插入多條數據兩種方法)
int addEmpsBatch(@Param("emps") List<Employee> emps); -->
<!-- MySQL下批量保存,可以foreach遍歷 mysql支持values(),(),()語法 --> //推薦使用
<insert id="addEmpsBatch">
INSERT INTO emp(ename,gender,email,did)
VALUES
<foreach collection="emps" item="emp" separator=",">
(#{emp.eName},#{emp.gender},#{emp.email},#{emp.dept.id})
</foreach>
</insert>
<!-- 這種方式需要數據庫連接屬性allowMutiQueries=true的支持 --> //在jdbc.url=jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true
<!-- <insert id="addEmpsBatch"> 后加上allowMultiQueries=true
<foreach collection="emps" item="emp" separator=";"> 表示可以多次執行insert into語句,中間;不會錯
INSERT INTO emp(ename,gender,email,did)
VALUES(#{emp.eName},#{emp.gender},#{emp.email},#{emp.dept.id})
</foreach>
</insert> -->
</mapper>
代碼:https://github.com/shuaishuaihand/mybatis.git
