Mybatis總結一之SQL標簽方法


---恢復內容開始---

   定義:mapper.xml映射文件中定義了操作數據庫的sql,並且提供了各種標簽方法實現動態拼接sql。每個sql是一個statement,映射文件是mybatis的核心。

一,內容標簽

1.NamePlace

NamePlace命名空間作用是對sql進行分類化管理。若使用Dao開發方式,映射文件的namespace可以任意命名,如果采用的是mapper接口代理的方法開發,Mapper的映射文件中namespace必須為接口的全名。

1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE mapper
3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5 <mapper namespace="Mapper.EmpMapper">
6     //CURD操作標簽
7         //if片段
8 </mapper>

2.CRUD標簽

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="Mapper.EmpMapper">
 6     <!-- 查詢 -->
 7     <select id="" parameterType="" resultType=""></select>
 8 
 9     <!-- 添加 -->
10     <insert id="" parameterType=""></insert>
11 
12     <!-- 刪除 -->
13     <delete id="" parameterType=""></delete>
14 
15     <!-- 更新 -->
16     <update id="" parameterType=""></update>
17 </mapper>

3、標簽調用方法

(1)selectOne與selectList方法

selectOne表示查詢出一條結果進行映射,使用selectOne查詢多條記錄會拋出異常。selectureList表示查詢一個列表進行映射,對於使用selectOne可以實現的插敘,使用selectList必然也可以實現。

(1)代理對象內部調用

動態代理對象調用sqlSession.selectOne()和sqlSessionList()是根據mapper接口方法的返回值決定的,如果mapper方法返回單個pojo對象,代理對象內部通過selectOne查詢數據庫。如果mapper()方法返回集合對象,代理對象內部通過selectList查詢數據庫。

二、動態SQL標簽

1、if標簽

 1 //進行空字符串校驗
 2 <select id="findUserList" parameterType="user" resultType="user">
 3     select * from user  where 1=1
 4         <if test="id!=null and id!=''">
 5             and id=#{id}
 6         </if>
 7         <if test="username!=null and username!=''">
 8             and username like '%${username}%'
 9         </if>
10 </select>

2、where標簽

 1 //<where/>可以自動處理第一個and
 2 <select id="findUserList" parameterType="user" resultType="user">
 3     select * from user
 4         <where>
 5             <if test="id!=null and id!=''">
 6                 and id=#{id}
 7             </if>
 8             <if test="username!=null and username!=''">
 9                 and username like '%${username}%'
10             </if>
11         </where>
12 </select>

3、sql片段

 1 //建立sql片段
 2 <sql id="query_user_where">
 3     <if test="id!=null and id!=''">
 4         and id=#{id}
 5     </if>
 6     <if test="username!=null and username!=''">
 7         and username like '%${username}%'
 8     </if>
 9 </sql>
10 
11 //使用include引用sql片段
12 <select id="findUserList" parameterType="user" resultType="user">
13     select * from user
14         <where>
15             <include refid="query_user_where"/>
16         </where>
17 </select>
18 
19 //引用其它mapper.xml的sql片段
20 <include refid="namespace.sql片段"/>

三、foreach標簽

1、通過sql傳遞數據或list,mybatis使用foreach參數定義如下:Collection指定輸入對象中集合屬性,item每個遍歷生成對象,open開始遍歷時拼接的串,close結束遍歷時拼接的串,separator:遍歷的兩個對象需要拼接的串。

 

(sql)語句

1 SELECT * FROM USERS WHERE username LIKE '%張%' AND (id =10 OR id =89 OR id=16)
2 SELECT * FROM USERS WHERE username LIKE '%張%' id IN (10,89,16)

(vo)類

1 public class QueryVo{
2     private User user;
3         private UserCustom userCustom;
4     //傳遞多個用戶id
5     private List<Integer> ids;
6     set()/get()  ...
7 }

(映射文件)

 1 復制代碼
 2 <select id="findUserList" parameterType="UserQueryVo" resultType="UserCustom">
 3      SELECT * FROM USER
 4      <where>
 5         <!-- 使用實現下邊的sql拼接: AND (id=1 OR id=10 OR id=16) -->
 6 
 7         <if test="ids!=null and ids.size>0">
 8 
 9            <foreach collection="ids" item="user_id" open="AND (" close=")" separator="or">
10 
11                   id=#{user_id}
12 
13            </foreach>
14 
15        </if>
16      </where> 
17 </select>
18 
19  
20 
21 <!-- 使用實現下邊的sql拼接: and id IN(1,10,16)—>
22 
23 <foreach collection="ids" item="user_id" open="and id IN(" close=")" separator=",">
24 
25      #{user_id}
26 
27 </foreach>

(測試代碼)

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

(Mapper映射文件)

 1 <select id="selectUserByList" parameterType="java.util.List" resultType="user">
 2     select * from user
 3       <where>
 4       <!-- 傳遞List,List中是pojo -->
 5       <if test="list!=null">
 6          <foreach collection="list" item="item" open="and id in( "separator="," close=")">
 7             #{item.id}
 8          </foreach>
 9        </if>
10      </where>
11 </select>

(Mapper)接口

1 public List<User> selectUserByList(List userlist);

(測試)

 1 //構造查詢條件List
 2 List<User> userlist = new ArrayList<User>();
 3 User user = new User();
 4 user.setId(1);
 5 userlist.add(user);
 6 
 7 user = new User();
 8 user.setId(2);
 9 userlist.add(user);
10 //傳遞userlist列表查詢用戶列表
11 List<User>list = userMapper.selectUserByList(userlist);

3、傳遞pojo類數組

(Mapper映射文件)

參數含義:index為數組的下標,item為數組每個元素的名稱,名稱隨意,open循環開始,close循環結束,separator中間分隔輸出。

 1 <select id="selectUserByArray" parameterType="Object[]" resultType="user">
 2     select * from user
 3       <where>
 4          <!-- 傳遞pojo類數組 -->
 5         <if test="array!=null">
 6             <foreach collection="array" index="index" item="item"
 7                          open="and id in("separator=","close=")">
 8                     #{item.id}
 9             </foreach>
10         </if>
11        </where>
12 </select>

(mapper接口)

1 public List<User> selectUserByArray(Object[] userlist)

(測試)

 1 //構造查詢條件List
 2 Object[] userlist = new Object[2];
 3 User user = new User();
 4 user.setId(1);
 5 userlist[0]=user;
 6 
 7 user = new User();
 8 user.setId(2);
 9 userlist[1]=user;
10 
11 //傳遞user對象查詢用戶列表
12 List<User>list = userMapper.selectUserByArray(userlist);

4、傳遞字符串類數組

(1)Mapper映射文件

 1 復制代碼
 2 <select id="selectUserByArray" parameterType="Object[]" resultType="user">
 3     select * from user
 4     <where>
 5         <!-- 傳遞字符串數組 -->
 6         <if test="array!=null">
 7             <foreach collection="array"index="index"item="item"
 8                     open="and id in("separator=","close=")">
 9                     #{item}
10             </foreach>
11         </if>
12     </where>
13 </select>

如果數組中是簡單類型則寫為#{item},不用通過ognl獲取對象屬性值

(2)mapper接口

1 public List<User> selectUserByArray(Object[] userlist)

(3)測試

1 //構造查詢條件List
2 Object[] userlist = new Object[2];
3 userlist[0]=”1”;
4 userlist[1]=”2”;
5 //傳遞user對象查詢用戶列表
6 List<User>list = userMapper.selectUserByArray(userlist);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM