国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

mybatis入門基礎(chǔ)(五)----動(dòng)態(tài)SQL

2019-11-15 00:44:21
字體:
供稿:網(wǎng)友
mybatis入門基礎(chǔ)(五)----動(dòng)態(tài)SQL一:動(dòng)態(tài)SQL

  1.1.定義

    mybatis核心對sql語句進(jìn)行靈活操作,通過表達(dá)式進(jìn)行判斷,對sql進(jìn)行靈活拼接、組裝。

  1.2.案例需求

    用戶信息綜合查詢列表這個(gè)statement的定義使用動(dòng)態(tài)sql,對查詢條件進(jìn)行判斷,如果輸入?yún)?shù)不為空才進(jìn)行查詢拼接。

  1.3.UserMapper.xml

 1 <!-- 用戶信息綜合查詢  2     #{userCustom.sex}:取出pojo包裝對象中性別值 3     ${userCustom.username}:取出pojo對象中用戶名稱 4 --> 5     <select id="findUserList" parameterType="com.mybatis.entity.UserQueryVo"  6     resultType="com.mybatis.entity.UserCustom"> 7         select * from t_user  8         <!-- 動(dòng)態(tài)sql查詢:where可以自動(dòng)去掉第一個(gè)and --> 9         <where>10             <if test="userCustom!=null">11                 <if test="userCustom.sex!=null and userCustom.sex!='' ">12                     and sex=#{userCustom.sex}13                 </if>14                 <if test="userCustom.username!=null and userCustom.username!='' ">15                     and username=#{userCustom.username}16                 </if>17             </if>18         </where>19 <!--          where sex=#{userCustom.sex} and username LIKE '%${userCustom.username}%' -->20     </select>

  1.4.測試代碼

 1     @Test 2     public void testFindUserList() { 3         Sqlsession sqlSession = sqlSessionFactory.openSession(); 4         //創(chuàng)造查詢條件 5         UserQueryVo userQueryVo = new UserQueryVo(); 6         UserCustom userCustom = new UserCustom(); 7 //        userCustom.setSex("2"); 8         //這里使用動(dòng)態(tài)sql,如果不設(shè)置某個(gè)值,條件不會(huì)拼接sql中 9         userCustom.setUsername("小");10         userQueryVo.setUserCustom(userCustom);11         // 創(chuàng)建Usermapper對象,mybatis自動(dòng)生成mapper代理對象12         UserMapper mapper = sqlSession.getMapper(UserMapper.class);13         List<UserCustom>list=mapper.findUserList(userQueryVo);14         //測試動(dòng)態(tài)sql,屬性的非空判斷測試15 //        List<UserCustom>list=mapper.findUserList(null);16         System.out.PRintln(list);17         sqlSession.commit();18         sqlSession.close();19     }
二:SQL片段

  2.1.需求

    將上邊的動(dòng)態(tài)sql判斷代碼抽取出來,組成一個(gè)sql片段,其它的statement中就可以引用sql片段,方便開發(fā)。

  2.2.定義sql片段  

 1 <!-- 定義sql片段,Id是唯一標(biāo)識(shí) 2          建議:是基于單表來定義sql片段,這樣的話sql片段的可重用性才高,在sql片段中不要包含where 3      --> 4     <sql id="query_user_where" > 5         <if test="userCustom!=null"> 6                 <if test="userCustom.sex!=null and userCustom.sex!='' "> 7                     and sex=#{userCustom.sex} 8                 </if> 9                <if test="userCustom.username!=null and userCustom.username!='' ">10                     and username=#{userCustom.username}11                 </if>12             </if>13     </sql>

  2.3.在mapper.xml中定義的statement中引用sql片段

 1 <!-- 用戶信息綜合查詢  2     #{userCustom.sex}:取出pojo包裝對象中性別值 3     ${userCustom.username}:取出pojo對象中用戶名稱 4 --> 5     <select id="findUserList" parameterType="com.mybatis.entity.UserQueryVo"  6     resultType="com.mybatis.entity.UserCustom"> 7         select * from t_user  8         <!-- 動(dòng)態(tài)sql查詢:where可以自動(dòng)去掉第一個(gè)and --> 9         <where>10         <!-- 引用sql片段的id,如果refid指定的不在本mapper.xml中,需要前邊加namespace -->11             <include refid="query_user_where"></include>12             <!-- 這里可以引用其它的sql片段 -->13         </where>14     </select>
三:foreach

  作用:向sql傳遞數(shù)組或者list,mybatis使用foreach解析

在用戶查詢列表和查詢總數(shù)的statement中增加多個(gè)id輸入查詢。

3.1.需求

  sql語句如下:

  兩種方法:

  SELECT*FROMt_user WHEREid=1ORid=10ORid=16

  SELECT*FROMt_user WHEREidIN(1,10,16)

3.2.在輸入?yún)?shù)包裝類型中添加List<Integer> ids 傳入多個(gè)id

 1 package com.mybatis.entity; 2  3 import java.util.List; 4  5 /** 6  *  7  * @ClassName: UserQueryVo 8  * @Description: TODO(包裝類型) 9  * @author warcaft10  * 11  */12 public class UserQueryVo {13 14     public List<Integer> ids;15 16     public List<Integer> getIds() {17         return ids;18     }19 20     public void setIds(List<Integer> ids) {21         this.ids = ids;22     }23 }

3.3.mapper.xml代碼

 1     <!-- 實(shí)現(xiàn)下邊的sql拼接 2             select * from t_user where id=1 OR id=2 OR id=3 3     --> 4     <select id="findUserByIds" parameterType="com.mybatis.entity.UserQueryVo"  5     resultType="com.mybatis.entity.User"> 6             select * from t_user 7         <where> 8                 <if test="ids!=null"> 9                 <!-- 使用foreach遍歷ids10                     collection:指定輸入對象的集合屬性11                     item:每個(gè)遍歷生成對象中12                     open:開始遍歷時(shí)拼接的串13                     close:技術(shù)遍歷時(shí)拼接的串14                     separator:遍歷的兩個(gè)對象中需要拼接的串15                  -->16                 <foreach collection="ids" item="user_id" open="AND (" close=")" separator="or">17                     id=#{user_id}18                 </foreach>19             </if>20         </where>21     </select>
select * from t_user where id in(1,2,3)的mapper.xml配置
 1  <select id="findUserByIds" parameterType="com.mybatis.entity.UserQueryVo"  2     resultType="com.mybatis.entity.User"> 3             select * from t_user 4         <where> 5                 <if test="ids!=null"> 6                 <!--  7                     使用foreach遍歷ids 8                     collection:指定輸入對象的集合屬性 9                     item:每個(gè)遍歷生成對象中10                     open:開始遍歷時(shí)拼接的串11                     close:技術(shù)遍歷時(shí)拼接的串12                     separator:遍歷的兩個(gè)對象中需要拼接的串13                  -->14                 <!-- 實(shí)現(xiàn)“ select * from t_user where  id in(1,2,3)”拼接 -->15                 <foreach collection="ids" item="user_id" open="AND id in ("  close=")" separator=",">16                     id=#{user_id}17                 </foreach>18             </if>19         </where>20     </select>

userMapper.java代碼

1 public interface UserMapper {2     //ids查詢用戶數(shù)據(jù)3     public List<User> findUserByIds(UserQueryVo userQueryVo);4 }

Junit測試代碼

 1 @Test 2     public void findUserByIdsTest() { 3         SqlSession sqlSession = sqlSessionFactory.openSession(); 4         // 創(chuàng)建Usermapper對象,mybatis自動(dòng)生成mapper代理對象 5         UserMapper mapper = sqlSession.getMapper(UserMapper.class); 6         //創(chuàng)造查詢條件 7         UserQueryVo userQueryVo = new UserQueryVo(); 8         //傳入多個(gè)id 9         List<Integer> ids=new ArrayList<Integer>();10         ids.add(1);11         ids.add(2);12         ids.add(3);13         //將ids通過userQueryVo傳入statement中14         userQueryVo.setIds(ids);15         //調(diào)用userMapper的代碼16         List<UserCustom> userList= mapper.findUserList(userQueryVo);17         System.out.println(userList);18         sqlSession.close();19     }


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 军事| 桐城市| 新巴尔虎右旗| 柘城县| 永兴县| 桦甸市| 依安县| 阿尔山市| 象州县| 家居| 江源县| 尚志市| 黄陵县| 临邑县| 安宁市| 德阳市| 比如县| 通城县| 卫辉市| 湘西| 丰顺县| 辽源市| 浠水县| 苏尼特左旗| 嘉峪关市| 襄城县| 重庆市| 东至县| 桐梓县| 遂溪县| 永昌县| 固始县| 黄浦区| 江山市| 夏邑县| 专栏| 吉木萨尔县| 托克逊县| 汝城县| 子长县| 泾源县|