Mybatis的动态SQL语句

Mybatis的动态SQL语句主要解决的问题是不同条件SQL语句的拼接
例如:根据用户信息,查询用户列表,当不知道根据的是用户的什么信息时,写出查询的SQL语句是有一定困难的,而动态SQL语句主要解决的就是此类问题。

if标签的使用

在持久层接口定义方法

    /**
     * 根据用户信息,查询用户列表
     * @param user
     * @return
     */
    List<User> findByUser(User user);

编写持久层接口对应的映射文件

 <!-- 根据用户信息,查询用户列表 -->
    <select id="findByUser" resultType="User" parameterType="User">
        select *from user where 1 = 1
        <if test="id != 0">
            and id = #{id}
        </if>
        <if test="username != null and username != '' ">
            and username like #{username}
        </if>
        <if test="birthday != null">
            and birthday = #{birthday}
        </if>
        <if test="sex != null">
            and sex = #{sex}
        </if>
        <if test="address != null">
            and address = #{address}
        </if>
    </select>

编写测试方法

  /**
     * 根据用户信息,查询用户列表
     */
    @Test
    public void testFindByUser()
    {
        User user = new User();
        user.setUsername("%王%");
        List<User> users = userDao.findByUser(user);
        for (User u : users)
        {
            System.out.println(u);
        }
    }

where标签的使用

为了简化上面 where 1=1 的条件拼接,我们可以采用标签来简化开发,因此修改持久层映射文件

 <!-- 根据用户信息,查询用户列表 -->
    <select id="findByUser" resultType="User" parameterType="User">
        select *from user
        <where>
            <if test="id != 0">
                and id = #{id}
            </if>
            <if test="username != null and username != '' ">
                and username like #{username}
            </if>
            <if test="birthday != null">
                and birthday = #{birthday}
            </if>
            <if test="sex != null">
                and sex = #{sex}
            </if>
            <if test="address != null">
                and address = #{address}
            </if>
        </where>
    </select>

foreach标签的使用

froeach是对一个集合进行遍历,通常在构建in条件语句的时候应用
例如:根据一个用户id集合查询用户。
对id集合进行封装,加入到List集合
在这里插入图片描述
编写持久层接口方法

  /**
     * 根据id集合查询用户
     * @param queryUR
     * @return
     */
    List<User> findInIds(QueryUR queryUR);

编写持久层接口映射文件

  <!--根据id集合查询用户 -->
    <select id="findInIds" resultType="user" parameterType="int">
       select *from user
       <where>
           <if test="ids != null and ids.size() > 0">
                <!-- foreach:用于遍历集合
                    collection:代表要遍历的集合
                    open:代表语句的开始部分
                    close:代表语句的结束部分
                    item:代表需要遍历的集合的每个元素
                    sperator:代表分隔符
                 -->
               <foreach collection="ids" open="id in (" close=")" item="uid" separator=",">
                    #{uid}
               </foreach>
           </if>
       </where>
    </select>

编写测试方法

  /**
     * 根据id集合查询用户
     */
    @Test
    public void testFindInIds()
    {
        QueryUR queryUR = new QueryUR();
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(41);
        ids.add(43);
        ids.add(45);
        queryUR.setIds(ids);
        List<User> users = userDao.findInIds(queryUR);
        for (User user : users)
        {
            System.out.println(user);
        }
    }

SQL语句的简化编写

在映射文件中,可以将重复的SQL语句通过sql标签提取出来,使用include标签引用即可,已达到sql重用的效果。如下所示:

    <!--抽取重复的语句代码片段-->
    <sql id="querySql">
        select *from user
    </sql>

    <select id="findAll" resultType="USER" >
        <include refid="querySql"></include>
    </select>

    <!--根据id查询用户-->
    <select id="findById" resultType="User" parameterType="int">
        <include refid="querySql"></include> where id= #{userID};
    </select>
发布了71 篇原创文章 · 获赞 6 · 访问量 5392
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章