【MyBatis框架】mapper配置文件-关于动态sql

来源:互联网 发布:网络球机接线图 编辑:程序博客网 时间:2024/06/05 12:09

转载:http://blog.csdn.net/acmman/article/details/46581349

动态sql


1.什么是动态sql
mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。

2.需求

用户信息综合查询列表和用户信息查询列表总数这两个statement的定义使用动态sql。

对查询条件进行判断,如果输入参数不为空才进行查询条件拼接。

3.mapper.xml
原查询语句配置:
[html] view plain copy
  1. <mapper namespace="cn.edu.hpu.mybatis.mapper.UserMapper">  
  2.       
  3.     <!-- 用户信息综合查询   
  4.     #{UserCustom.sex}取出包装对象中性别值  
  5.     ${UserCustom.username}取得pojo包装对象中用户名称  
  6.     -->  
  7.     <select id="findUserList" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo"   
  8.                                 resultType="cn.edu.hpu.mybatis.PO.UserCustom">  
  9.         select * from user where user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%'  
  10.     </select>  
  11.       
  12.     <!-- 用户信息综合查询总数 -->  
  13.     <select id="findUserCount" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo" resultType="int">  
  14.         select count(*) from user where user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%'  
  15.     </select>  
  16.     ......  
  17. </mapper>  

修改后的查询语句配置:
[html] view plain copy
  1. <!-- 用户信息综合查询   
  2.     #{UserCustom.sex}取出包装对象中性别值  
  3.     ${UserCustom.username}取得pojo包装对象中用户名称  
  4.     -->  
  5.     <select id="findUserList" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo"   
  6.                                 resultType="cn.edu.hpu.mybatis.PO.UserCustom">  
  7.         select * from user   
  8.           
  9.         <!-- where标签可以自动去掉第一个and -->    
  10.         <where>  
  11.         <if test="userCustom!=null">  
  12.             <if test="userCustom.sex!=null and userCustom.sex!=''">  
  13.                 and user.sex=#{userCustom.sex}  
  14.             </if>  
  15.             <if test="userCustom.username!=null and userCustom.username!=''">  
  16.                 and user.username like '%${userCustom.username}%'  
  17.             </if>  
  18.         </if>  
  19.         </where>  
  20.     </select>  
  21.       
  22.     <!-- 用户信息综合查询总数 -->  
  23.     <select id="findUserCount" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo" resultType="int">  
  24.         select count(*) from user   
  25.   
  26.   
  27.         <!-- where标签可以自动去掉第一个and -->    
  28.         <where>  
  29.         <if test="userCustom!=null">  
  30.             <if test="userCustom.sex!=null and userCustom.sex!=''">  
  31.                 and user.sex=#{userCustom.sex}  
  32.             </if>  
  33.             <if test="userCustom.username!=null and userCustom.username!=''">  
  34.                 and user.username like '%${userCustom.username}%'  
  35.             </if>  
  36.         </if>  
  37.         </where>  
  38.     </select>  
 
4.测试代码
[java] view plain copy
  1. //用户信息综合查询  
  2.     @Test  
  3.     public void testFindUserList() throws Exception{  
  4.           
  5.         SqlSession sqlSession=sqlSessionFactory.openSession();  
  6.           
  7.         //创建UserMapper代理对象  
  8.         UserMapper userMapper=sqlSession.getMapper(UserMapper.class);  
  9.           
  10.         //创建包装对象,设置查询条件  
  11.         UserQueryVo userQueryVo=new UserQueryVo();  
  12.         UserCustom userCustom=new UserCustom();  
  13.         //由于这里使用动态sql,如果这里不设置某个值,条件不会拼接在sql中  
  14.         //userCustom.setSex("男");  
  15.         userCustom.setUsername("张三");  
  16.         userQueryVo.setUserCustom(userCustom);  
  17.           
  18.         //调用userMapper的方法  
  19.         List<UserCustom> users=userMapper.findUserList(userQueryVo);  
  20.           
  21.         for (int i = 0; i < users.size(); i++) {  
  22.             UserCustom user=(UserCustom)users.get(i);  
  23.             System.out.println(user.getId()+":"+user.getUsername());  
  24.         }  
  25.     }  

测试结果:
1:张三
4:张三丰

输出日志:
[plain] view plain copy
  1. DEBUG [main] - Opening JDBC Connection  
  2. DEBUG [main] - Created connection 31761534.  
  3. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@1e4a47e]  
  4. DEBUG [main] - ==>  Preparing: select * from user WHERE user.username like '%张三%'   
  5. DEBUG [main] - ==> Parameters:   
  6. DEBUG [main] - <==      Total: 2  

发现sql语句为select * from user WHERE user.username like '%张三%' ,并没有将sex拼接进去,说明我们的动态sql设置成功

相应的,把userCustom.setUsername("张三");也注释掉,发现输出日志:
[plain] view plain copy
  1. DEBUG [main] - Opening JDBC Connection  
  2. DEBUG [main] - Created connection 24027753.  
  3. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@16ea269]  
  4. DEBUG [main] - ==>  Preparing: select * from user   
  5. DEBUG [main] - ==> Parameters:   
  6. DEBUG [main] - <==      Total: 5  

发现sql语句为select * from user,并没有将sex和username拼接进去,说明我们的动态sql设置成功

5.sql片段

5.1需求
将上边实现的动态sql判断代码块抽取出来,组成一个sql片段。其它的statement中就可以引用sql片段。
方便程序员进行开发。

5.2定义sql片段
[html] view plain copy
  1. <mapper namespace="cn.edu.hpu.mybatis.mapper.UserMapper">  
  2.   
  3.     <!-- 定义sql片段   
  4.     id:sql片段的唯一标识   
  5.     在sql片段中不要加入where  
  6.     经验:一般我们定义sql片段是为了可重用性,是基于单表来定义sql片段,  
  7.     这样的话这个sql片段可重用性才高-->  
  8.     <sql id="query_user_where">  
  9.         <if test="userCustom!=null">  
  10.             <if test="userCustom.sex!=null and userCustom.sex!=''">  
  11.                 and user.sex=#{userCustom.sex}  
  12.             </if>  
  13.             <if test="userCustom.username!=null and userCustom.username!=''">  
  14.                 and user.username like '%${userCustom.username}%'  
  15.             </if>  
  16.         </if>  
  17.     </sql>  
  18.     ......  
  19. </mapper>  

5.3引用sql片段
在mapper.xml中定义的statement中引用sql片段:
[html] view plain copy
  1. <!-- 用户信息综合查询   
  2.     #{UserCustom.sex}取出包装对象中性别值  
  3.     ${UserCustom.username}取得pojo包装对象中用户名称  
  4.     -->  
  5.     <select id="findUserList" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo"   
  6.                                 resultType="cn.edu.hpu.mybatis.PO.UserCustom">  
  7.         select * from user   
  8.           
  9.         <!-- where标签可以自动去掉第一个and -->    
  10.         <where>  
  11.             <!-- 应用sql片段的id,如果refid指定的id不再本mapper文件中,需要前边加namespace -->  
  12.             <include refid="query_user_where"></include>  
  13.             <!-- 在这里还可能要引用其他的sql片段 -->  
  14.         </where>  
  15.     </select>  
  16.       
  17.     <!-- 用户信息综合查询总数 -->  
  18.     <select id="findUserCount" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo" resultType="int">  
  19.         select count(*) from user   
  20.   
  21.   
  22.         <!-- where标签可以自动去掉第一个and -->    
  23.         <where>  
  24.             <!-- 应用sql片段的id,如果refid指定的id不再本mapper文件中,需要前边加namespace -->  
  25.             <include refid="query_user_where"></include>  
  26.             <!-- 在这里还可能要引用其他的sql片段 -->  
  27.         </where>  
  28.     </select>  

测试:
[java] view plain copy
  1. //用户信息综合查询  
  2. @Test  
  3. public void testFindUserList() throws Exception{  
  4.       
  5.     SqlSession sqlSession=sqlSessionFactory.openSession();  
  6.       
  7.     //创建UserMapper代理对象  
  8.     UserMapper userMapper=sqlSession.getMapper(UserMapper.class);  
  9.       
  10.     //创建包装对象,设置查询条件  
  11.     UserQueryVo userQueryVo=new UserQueryVo();  
  12.     UserCustom userCustom=new UserCustom();  
  13.     //由于这里使用动态sql,如果这里不设置某个值,条件不会拼接在sql中  
  14.     userCustom.setSex("男");  
  15.     userCustom.setUsername("张三");  
  16.     userQueryVo.setUserCustom(userCustom);  
  17.       
  18.     //调用userMapper的方法  
  19.     List<UserCustom> users=userMapper.findUserList(userQueryVo);  
  20.       
  21.     for (int i = 0; i < users.size(); i++) {  
  22.         UserCustom user=(UserCustom)users.get(i);  
  23.         System.out.println(user.getId()+":"+user.getUsername());  
  24.     }  
  25. }  

测试结果:
1:张三
4:张三丰


输出日志:
[plain] view plain copy
  1. DEBUG [main] - Opening JDBC Connection  
  2. DEBUG [main] - Created connection 17689439.  
  3. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@10deb5f]  
  4. DEBUG [main] - ==>  Preparing: select * from user   
  5. DEBUG [main] - ==> Parameters:   
  6. DEBUG [main] - <==      Total: 5  

说明sql片段引用成功

小结:

sql片段方便程序员进行开发

0 0
原创粉丝点击