接口定義方法:User getUserById();映射文件:<selectid="getUserById"resultType="hello.pojo.User">select * from user where id = 1</select>
查詢多個對象
接口定義方法:List<User> getAllUser();映射文件:<selectid="getAllUser"resultType="hello.pojo.User">select * from user</select>
3 特殊操作
模糊查詢
<selectid="fuzzy"resultType="User">select * from user where username like "%"#{name}"%"
</select>
批量刪除:不能使用 #{ } 是因為它會自動添加單引號
<!--int deleteMore(@Param("ids") String ids);--><deleteid="batchDelete">delete from user where id in (${ids}) <!--示例:delete from user where id in (1, 2, 3)--></delete>
二 MyBatis 獲取參數值的方式
#{ } 對應占位符賦值;${ } 對應字符串拼接,外側需要加單引號
不關心接口中方法的形參命名
1 單個字面量類型的參數
可以以任意的名稱獲取參數的值,不在乎接口中的方法形參的名字,但最好見名知意
接口方法:
public interface ParamMapper {// 根據用戶名查詢用戶信息User getUserByName(String username);
}映射文件:
<selectid="getUserByName"resultType="param.pojo.User">select * from user where username = #{notmatters}select * from user where username = '${notmatters}' <!--等價--></select>
接口方法:
public interface ParamMapper {// 驗證登錄User checkLogin(String username, String password);
}映射文件:
<selectid="checkLogin"resultType="param.pojo.User">select * from user where username = #{arg0} and password = #{arg1}select * from user where username = #{param1} and password = #{param2} <!--等價--></select>
3 Map 類型的參數
方法參數為一個 Map 時,使用自定義的 key 訪問 value
接口方法:
public interface ParamMapper {// 驗證登錄 參數為MapUser checkLoginByMap(Map<String,Object> mapParam);
}映射文件:
<selectid="checkLoginByMap"resultType="param.pojo.User">select * from user where username = #{usernameKey} and password = #{passwordKey}
</select>測試:
...
Map param = new HashMap<String,Object>();
param.put("usernameKey", "伍佰");
param.put("passwordKey", "500");
userMapper.checkLoginByMap(param);
4 實體類類型的參數
通過訪問實體類對象中的屬性名獲取屬性值
接口方法:
public interface ParamMapper {// 添加用戶int insertUser(User paramUser);
}映射文件:
<insertid="insertUser">insert into user values (#{id}, #{username}, #{password}, #{age}, #{sex}, #{email})
</insert>
略有臃腫,可以使用別名的方式,只對不一致的表字段起別名 select id, user_name userName, password, age, sex from user where user_name like "%"#{mohu}"%"
<resultMapid="userMap"type="param.pojo.User"><idproperty="id"column="id"></id><resultproperty="userName"column="user_name"></result><resultproperty="password"column="password"></result><resultproperty="age"column="age"></result><resultproperty="sex"column="sex"></result></resultMap><selectid="testFuzzy"resultMap="userMap">select * from user where user_name like "%"#{fuzzy}"%"
</select>
映射文件:使用 association 子標簽后,左表 employee 可以訪問屬性 did;association 標簽需要指明屬性 javaType
<mappernamespace="glad.mapper.ResultMapMapper"><resultMapid="myResultMap"type="glad.pojo.Employee"><idproperty="eid"column="eid"></id><resultproperty="ename"column="ename"></result><associationproperty="department"javaType="glad.pojo.Department"><idproperty="did"column="did"></id><resultproperty="dname"column="dname"></result></association></resultMap><selectid="getEmpAndDept"resultMap="myResultMap"><!--使用 association 后,employee 可以訪問屬性 did-->select * from employee left join department on employee.did = department.did where employee.eid = #{eid}</select></mapper>
映射文件:使用 collection 標簽聲明類的 List 屬性,并設置標簽的 ofType 屬性(代表容器內元素的屬性)
<mappernamespace="glad.mapper.ResultMapMapper"><resultMapid="myResultMap"type="glad.pojo.Department"><idproperty="did"column="did"></id><resultproperty="dname"column="dname"></result><collectionproperty="emps"ofType="glad.pojo.Employee"><idproperty="eid"column="eid"></id><resultproperty="ename"column="ename"></result></collection></resultMap><selectid="getDeptAndEmp"resultMap="myResultMap">select * from department left join employee on department.did = employee.did where department.did = #{did}</select></mapper>
四 動態 SQL
1 if 標簽
滿足指定條件時,才會將 if 標簽中的 SQL 加入
POJO:省略 Lombok 注解,和表中的屬性名以 X 區分,注意在映射文件的各處訪問的是表還是傳入的參數
映射文件: 數據庫表的字段,和類的屬性名不一致時,使用 resultMap,否則查詢結果一直為 null 為了保證格式正確,需要在 where 后加一個恒成立條件
<mappernamespace="dynamic.mapper.DynamicMapper"><resultMapid="myResultMap"type="dynamic.pojo.User"><idproperty="idX"column="id"></id><resultproperty="usernameX"column="username"></result><resultproperty="passwordX"column="password"></result><resultproperty="ageX"column="age"></result><resultproperty="sexX"column="sex"></result><resultproperty="emailX"column="email"></result></resultMap><selectid="getInfo"resultMap="myResultMap">select * from user where 1 = 1<iftest="idX != '' and idX != null">and id = #{idX}</if><iftest="usernameX != '' and usernameX != null">and username = #{usernameX}</if><iftest="passwordX != '' and passwordX != null">and password = #{passwordX}</if><iftest="ageX != '' and ageX != null">and age = #{ageX}</if><iftest="sexX != '' and sexX != null">and sex = #{sexX}</if><iftest="emailX != null and emailX !=''">and email = #{emailX}</if></select></mapper>
2 where 標簽
一般和 if 標簽結合使用: 若 where 標簽中的 if 子標簽條件都不滿足,則 where 標簽沒有任何功能,即不會添加 where 關鍵字; 若 where 標簽中的 if 子標簽條件滿足,則 where 標簽會自動添加 where 關鍵字,并將條件最前方多余的 and 去掉
<selectid="getEmpListByMoreTJ2"resultType="Emp">select * from t_emp<where><iftest="ename != '' and ename != null">ename = #{ename}</if><iftest="age != '' and age != null">and age = #{age}</if><iftest="sex != '' and sex != null">and sex = #{sex}</if></where></select>
3 trim 標簽
trim 用于去掉或添加標簽中的內容
屬性作用
prefix
在 trim 標簽中的內容的前面添加某些內容
prefixOverrides
在 trim 標簽中的內容的前面去掉某些內容
suffix
在 trim 標簽中的內容的后面添加某些內容
suffixOverrides
在 trim 標簽中的內容的后面去掉某些內容
映射文件
<selectid="getEmpListByMoreTJ"resultType="Emp">select * from t_emp<!--在以下部分的開頭添加where,末尾刪去and--><trimprefix="where"suffixOverrides="and"><iftest="ename != '' and ename != null">ename = #{ename} and</if><iftest="age != '' and age != null">age = #{age} and</if><iftest="sex != '' and sex != null">sex = #{sex}</if></trim></select>
<!--List<Emp> getEmpListByChoose(Emp emp);--><selectid="getEmpListByChoose"resultType="Emp">select * from t_emp<where><choose><whentest="ename != '' and ename != null">ename = #{ename}</when><whentest="age != '' and age != null">age = #{age}</when><whentest="sex != '' and sex != null">sex = #{sex}</when><whentest="email != '' and email != null">email = #{email}</when></choose></where></select>
5 foreach 標簽
用于執行批量操作
屬性作用
collection
設置要循環的數組或集合
item
表示集合或數組中的每一個數據
separator
設置元素之間的分隔符
open
設置 foreach 標簽中的內容的開始符
close
設置 foreach 標簽中的內容的結束符
映射文件
<!--int insertMoreEmp(@Param("emps") List<Emp> emps);--><insertid="insertMoreEmp">insert into t_emp values<foreachcollection="emps"item="emp"separator=",">(null,#{emp.ename},#{emp.age},#{emp.sex},#{emp.email},null)</foreach></insert><!--int deleteMoreByArray(@Param("eids") int[] eids);--><deleteid="deleteMoreByArray">delete from t_emp where eid in<foreachcollection="eids"item="eid"separator=","open="("close=")">#{eid}</foreach></delete>
6 sql 標簽
提取 SQL 語句的公共部分
<sqlid="empColumns">eid, ename, age, sex, did
</sql>引用:
select <includerefid="empColumns"></include> from t_emp