Mybatis如何自动生成sql语句


当前第2页 返回上一页

例如:根据用户信息,查询用户列表,当不知道根据的是用户的什么信息时,写出查询的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>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

更多SQL内容来自木庄网络博客


打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...