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

动态sql

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

2.需求

用户信息综合查询列表和用户信息查询列表总数这两个statement的定义使用动态sql。
对查询条件进行判断,若是输入参数不为空才进行查询条件拼接。

3.mapper.xml
原查询语句配置:java

<mapper namespace="cn.edu.hpu.mybatis.mapper.UserMapper">
    
    <!-- 用户信息综合查询 
    #{UserCustom.sex}取出包装对象中性别值
    ${UserCustom.username}取得pojo包装对象中用户名称
    -->
    <select id="findUserList" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo" 
                                resultType="cn.edu.hpu.mybatis.PO.UserCustom">
        select * from user where user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%'
    </select>
    
    <!-- 用户信息综合查询总数 -->
    <select id="findUserCount" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo" resultType="int">
        select count(*) from user where user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%'
    </select>
    ......
</mapper>

修改后的查询语句配置:程序员

<!-- 用户信息综合查询 
    #{UserCustom.sex}取出包装对象中性别值
    ${UserCustom.username}取得pojo包装对象中用户名称
    -->
    <select id="findUserList" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo" 
                                resultType="cn.edu.hpu.mybatis.PO.UserCustom">
        select * from user 
        
        <!-- where标签能够自动去掉第一个and -->  
        <where>
        <if test="userCustom!=null">
            <if test="userCustom.sex!=null and userCustom.sex!=''">
                and user.sex=#{userCustom.sex}
            </if>
            <if test="userCustom.username!=null and userCustom.username!=''">
                and user.username like '%${userCustom.username}%'
            </if>
        </if>
        </where>
    </select>
    
    <!-- 用户信息综合查询总数 -->
    <select id="findUserCount" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo" resultType="int">
        select count(*) from user 


        <!-- where标签能够自动去掉第一个and -->  
        <where>
        <if test="userCustom!=null">
            <if test="userCustom.sex!=null and userCustom.sex!=''">
                and user.sex=#{userCustom.sex}
            </if>
            <if test="userCustom.username!=null and userCustom.username!=''">
                and user.username like '%${userCustom.username}%'
            </if>
        </if>
        </where>
    </select>

4.测试代码sql

//用户信息综合查询
    @Test
    public void testFindUserList() throws Exception{
        
        SqlSession sqlSession=sqlSessionFactory.openSession();
        
        //建立UserMapper代理对象
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        
        //建立包装对象,设置查询条件
        UserQueryVo userQueryVo=new UserQueryVo();
        UserCustom userCustom=new UserCustom();
        //因为这里使用动态sql,若是这里不设置某个值,条件不会拼接在sql中
        //userCustom.setSex("男");
        userCustom.setUsername("张三");
        userQueryVo.setUserCustom(userCustom);
        
        //调用userMapper的方法
        List<UserCustom> users=userMapper.findUserList(userQueryVo);
        
        for (int i = 0; i < users.size(); i++) {
            UserCustom user=(UserCustom)users.get(i);
            System.out.println(user.getId()+":"+user.getUsername());
        }
    }

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

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


5.sql片断测试


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

5.2定义sql片断spa

<mapper namespace="cn.edu.hpu.mybatis.mapper.UserMapper">

    <!-- 定义sql片断 
    id:sql片断的惟一标识 
    在sql片断中不要加入where
    经验:通常咱们定义sql片断是为了可重用性,是基于单表来定义sql片断,
    这样的话这个sql片断可重用性才高-->
    <sql id="query_user_where">
        <if test="userCustom!=null">
            <if test="userCustom.sex!=null and userCustom.sex!=''">
                and user.sex=#{userCustom.sex}
            </if>
            <if test="userCustom.username!=null and userCustom.username!=''">
                and user.username like '%${userCustom.username}%'
            </if>
        </if>
    </sql>
    ......
</mapper>

5.3引用sql片断
在mapper.xml中定义的statement中引用sql片断:代理

<!-- 用户信息综合查询 
    #{UserCustom.sex}取出包装对象中性别值
    ${UserCustom.username}取得pojo包装对象中用户名称
    -->
    <select id="findUserList" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo" 
                                resultType="cn.edu.hpu.mybatis.PO.UserCustom">
        select * from user 
        
        <!-- where标签能够自动去掉第一个and -->  
        <where>
            <!-- 应用sql片断的id,若是refid指定的id再也不本mapper文件中,须要前边加namespace -->
            <include refid="query_user_where"></include>
            <!-- 在这里还可能要引用其余的sql片断 -->
        </where>
    </select>
    
    <!-- 用户信息综合查询总数 -->
    <select id="findUserCount" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo" resultType="int">
        select count(*) from user 


        <!-- where标签能够自动去掉第一个and -->  
        <where>
            <!-- 应用sql片断的id,若是refid指定的id再也不本mapper文件中,须要前边加namespace -->
            <include refid="query_user_where"></include>
            <!-- 在这里还可能要引用其余的sql片断 -->
        </where>
    </select>

测试:code

//用户信息综合查询
@Test
public void testFindUserList() throws Exception{
    
    SqlSession sqlSession=sqlSessionFactory.openSession();
    
    //建立UserMapper代理对象
    UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
    
    //建立包装对象,设置查询条件
    UserQueryVo userQueryVo=new UserQueryVo();
    UserCustom userCustom=new UserCustom();
    //因为这里使用动态sql,若是这里不设置某个值,条件不会拼接在sql中
    userCustom.setSex("男");
    userCustom.setUsername("张三");
    userQueryVo.setUserCustom(userCustom);
    
    //调用userMapper的方法
    List<UserCustom> users=userMapper.findUserList(userQueryVo);
    
    for (int i = 0; i < users.size(); i++) {
        UserCustom user=(UserCustom)users.get(i);
        System.out.println(user.getId()+":"+user.getUsername());
    }
}

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


小结:

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

相关文章
相关标签/搜索