Mpper.xml映射文件中定義了操作數據庫的sql,並且提供了各種標簽方法實現動態拼接sql。每個sql是一個statement,映射文件是mybatis的核心。
一、內容標簽
1、NamePlace
NamePlace命名空間作用就是對sql進行分類化管理。若使用Dao開發方式,映射文件的nameplace可以任意命名;但如果采用的是Mapper接口代理的方式開發,Mapper的映射文件中namespace必須為接口的全名。
<?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="Mapper.EmpMapper"> //CURD操作標簽
//if片段
</mapper>
2、CRUD標簽
<?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="Mapper.EmpMapper"> <!-- 查詢 --> <select id="" parameterType="" resultType=""></select> <!-- 添加 --> <insert id="" parameterType=""></insert> <!-- 刪除 --> <delete id="" parameterType=""></delete> <!-- 更新 --> <update id="" parameterType=""></update> </mapper>
3、標簽調用方法
(1)selectOne與selectList方法
selectOne表示查詢出一條結果集進行映射,使用selectOne查詢多條記錄會拋出異常。selectList表示查詢出一個列表(多條記錄)進行映射,對於使用selectOne可以實現的查詢,使用selectList必然也可以實現(list中只有一個對象)。
(3)代理對象內部調用
動態代理對象調用sqlSession.selectOne()和sqlSession.selectList()是根據mapper接口方法的返回值決定,如果mapper方法返回單個pojo對象(非集合對象),代理對象內部通過selectOne查詢數據庫。如果mapper方法返回集合對象,代理對象內部通過selectList查詢數據庫。
二、動態SQL標簽
1、if標簽
//進行空字符串校驗 <select id="findUserList" parameterType="user" resultType="user"> select * from user where 1=1 <if test="id!=null and id!=''"> and id=#{id} </if> <if test="username!=null and username!=''"> and username like '%${username}%' </if> </select>
2、where標簽
//<where/>可以自動處理第一個and <select id="findUserList" parameterType="user" resultType="user"> select * from user <where> <if test="id!=null and id!=''"> and id=#{id} </if> <if test="username!=null and username!=''"> and username like '%${username}%' </if> </where> </select>
3、sql片段
Sql中可將重復的sql提取出來,使用時用include引用即可,最終達到sql重用的目的,如下:
//建立sql片段 <sql id="query_user_where"> <if test="id!=null and id!=''"> and id=#{id} </if> <if test="username!=null and username!=''"> and username like '%${username}%' </if> </sql> //使用include引用sql片段 <select id="findUserList" parameterType="user" resultType="user"> select * from user <where> <include refid="query_user_where"/> </where> </select> //引用其它mapper.xml的sql片段 <include refid="namespace.sql片段"/>
三、foreach標簽
1、通過pojo類傳遞list
向sql傳遞數組或List,mybatis使用foreach解析,foreach參數定義如下:collection指定輸入 對象中集合屬性, item每個遍歷生成對象中,open開始遍歷時拼接的串,close結束遍歷時拼接的串,separator:遍歷的兩個對象中需要拼接的串。
(1)sql語句
SELECT * FROM USERS WHERE username LIKE '%張%' AND (id =10 OR id =89 OR id=16) SELECT * FROM USERS WHERE username LIKE '%張%' id IN (10,89,16)
(2)vo類
public class QueryVo{ private User user;
private UserCustom userCustom; //傳遞多個用戶id private List<Integer> ids; set()/get() ... }
(3)映射文件
<select id="findUserList" parameterType="UserQueryVo" resultType="UserCustom">
SELECT * FROM USER
<where>
<!-- 使用實現下邊的sql拼接: AND (id=1 OR id=10 OR id=16) -->
<if test="ids!=null and ids.size>0">
<foreach collection="ids" item="user_id" open="AND (" close=")" separator="or">
id=#{user_id}
</foreach>
</if>
</where>
</select>
<!-- 使用實現下邊的sql拼接: and id IN(1,10,16)—>
<foreach collection="ids" item="user_id" open="and id IN(" close=")" separator=",">
#{user_id}
</foreach>
(4)測試代碼
List<Integer> ids = new ArrayList<Integer>(); ids.add(1);//查詢id為1的用戶 ids.add(10); //查詢id為10的用戶 queryVo.setIds(ids); List<User> list = userMapper.findUserList(queryVo);
2、傳遞單個list
(1)Mapper映射文件
<select id="selectUserByList" parameterType="java.util.List" resultType="user"> select * from user <where> <!-- 傳遞List,List中是pojo --> <if test="list!=null"> <foreach collection="list" item="item" open="and id in( "separator="," close=")"> #{item.id} </foreach> </if> </where> </select>
(2)Mapper接口
public List<User> selectUserByList(List userlist);
(3)測試程序
//構造查詢條件List List<User> userlist = new ArrayList<User>(); User user = new User(); user.setId(1); userlist.add(user); user = new User(); user.setId(2); userlist.add(user);
//傳遞userlist列表查詢用戶列表
List<User>list = userMapper.selectUserByList(userlist);
3、傳遞pojo類數組
(1)Mapper映射文件
參數含義:index為數組的下標,item為數組每個元素的名稱,名稱隨意定義,open循環開始,close循環結束,separator中間分隔輸出。
<select id="selectUserByArray" parameterType="Object[]" resultType="user"> select * from user <where> <!-- 傳遞pojo類數組 --> <if test="array!=null"> <foreach collection="array" index="index" item="item" open="and id in("separator=","close=")"> #{item.id} </foreach> </if> </where> </select>
(2)Mapper接口
public List<User> selectUserByArray(Object[] userlist)
(3)測試程序
//構造查詢條件List Object[] userlist = new Object[2]; User user = new User(); user.setId(1); userlist[0]=user; user = new User(); user.setId(2); userlist[1]=user; //傳遞user對象查詢用戶列表 List<User>list = userMapper.selectUserByArray(userlist);
4、傳遞字符串類數組
(1)Mapper映射文件
<select id="selectUserByArray" parameterType="Object[]" resultType="user"> select * from user <where> <!-- 傳遞字符串數組 --> <if test="array!=null"> <foreach collection="array"index="index"item="item" open="and id in("separator=","close=")"> #{item} </foreach> </if> </where> </select>
如果數組中是簡單類型則寫為#{item},不用再通過ognl獲取對象屬性值了。
(2)Mapper接口
public List<User> selectUserByArray(Object[] userlist)
(3)測試程序
//構造查詢條件List Object[] userlist = new Object[2]; userlist[0]=”1”; userlist[1]=”2”; //傳遞user對象查詢用戶列表 List<User>list = userMapper.selectUserByArray(userlist);