Mybatis——使用ognl表达式实现动态sql

本文讲述在mybatis中如何使用ognl表达式实现动态组装sql语句

新建Users实体类:

public class Users {
    private Integer uid;
    private String userName;
    private String tel;

    //添加上面私有字段的get、set方法
}

新建一个Dao接口类,mybatis配置文件在配置namespace属性时需要加入这个类的完整类名,找到这个类里的方法执行:

public interface UserDao {

    /**
     * 依据userName查询用户信息
     * @param user
     * @return
     */
    List<Users> listUser(Users user);

    /**
     * 动态选择条件
     * @param user
     * @return
     */
    List<Users> listUser2(Users user);

    /**
     * 动态范围查询
     * @param uids
     * @return
     */
    List<Users> listUser3(Integer[] uids);

    /**
     * 动态更新用户信息
     * @param user
     */
    void updateUser(Users user);

    /**
     * 批量添加
     * @param list
     */
    void addBatch(List<Users> list);

    /**
     * 批量删除
     * @param ids
     */
    void deleteBatch(int[] ids);

    /**
     * 批量更新
     * @param list
     */
    void updateBatch1(List<Users> list);

    /**
     * 批量更新
     * @param list
     */
    void updateBatch2(List<Users> list);
}

新建mybatis的配置文件(下面代码可以作为mybatis配置文件的模板,这里的namespace属性可以设置为上面的dao类的完整类名):

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- mapper根节点的namespace指定Dao接口的完整类名,
因为mybatis在解析的时候要根据这个类名找到相应dao方法执行 -->
<mapper namespace="">
    
</mapper>

在mybatis配置文件中写一条条件查询sql语句:

 

<!-- 动态查询(使用if拼接条件,适用于多选多的形式) -->
<select id="listUser" parameterType="users" resultMap="userMap">
    select u_id, u_name, u_tel from user_info4 user_info
    <where>
        <if test="userName != null and userName !=''">
            u_name = #{userName}
        </if>
        <if test="tel != null and tel !=''">
            and u_tel = #{tel}
        </if>
    </where>
</select>

这里使用了where和if标签,<where></where>意思就是sql语句中的where,当然,where直接在sql中写出来也可以,<if></if>标签就是条件判断,test属性中写条件语句,如果test中的条件语句为true,那么标签中的sql语句就会拼接到上面的sql语句中,所以这条sql语句的意思就是如果传过来的Users对象中,userName字段不为null,或字段值不为空,那么就添加一个对userName的查询,tel也是如此。注意:在对tel字段的判断时,标签中的sql语句前加了一个and,如果前一条判断为false,那么mybatis会自动将and关键字删除。

 

<!-- 动态查询(使用choose选择条件,适用于多选一) -->
<select id="listUser2" parameterType="users" resultMap="userMap">
    select u_id, u_name, u_tel from user_info4
    <choose>
        <when test="userName != null and userName != ''">
            where u_name = #{userName}
        </when>
        <when test="tel != null and tel != ''">
            where u_tel = #{tel}
        </when>
        <otherwise>
            order by u_id desc
        </otherwise>
    </choose>
</select>

这里使用的是choose-when-otherwise标签,有点类似于java中的switch-case-default选择条件语句,相比于if标签,这里只能选择一个,即多选一。在这条sql语句中,会按顺序判断when子句,如果所有的when子句都为false,则会执行otherwise子句中的sql语句。

 

<!-- 动态查询(使用foreach范围查询,适用于in和or语句) -->
<!-- int[] ids = new int[]{1,2,3,4}; -->
<select id="listUser3" parameterType="collection" resultMap="userMap">
    select u_id, u_name, u_tel from user_info4
    <where>
        u_id in
        <if test="array != null">
            <foreach collection="array" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </where>
</select>

foreach标签适用于范围查询(in和or语句),如遍历一个id集合,查询出集合中所有id对应的用户。在foreach标签中,collection属性指定需要遍历的集合的名称,这里只有一个参数,所以可以随意取;item指定遍历的每一项的别名,open指定在遍历前需要加上的内容,separator指定每遍历一个后加上的内容,close指定遍历完后需要加上的内容,如遍历上面的ids集合,那么最终得到的内容就是 (1,2,3,4) 。

 

<!-- 动态更新(使用set动态更新字段) -->
<update id="updateUser" parameterType="users" >
    update user_info4
    <trim prefix="set" suffixOverrides=",">
        <if test="userName != null and userName != ''">
            u_name = #{userName},
        </if>
        <if test="tel != null and tel != ''">
            u_tel = #{tel}
        </if>
    </trim>
    <!-- where u_id = #{uid} -->
    <where>
        u_id = #{uid}
    </where>
</update>

trim标签用于动态设值,例如在更新数据时,可以动态将改变的字段设置。在trim标签中,prefix属性表示在更新字段之前添加set关键字,suffixOverrides表示将最后一个更新字段的逗号替换成suffix指定的空格符,如果不指定suffix默认就是空格。

 

<!-- 批量添加,利用sql的特性 -->
<insert id="addBatch" parameterType="list">
    insert into user_info4(u_id, u_name, u_tel) values
    <foreach collection="list" item="user" separator=",">
        (#{user.uid}, #{user.userName}, #{user.tel})
    </foreach>
</insert>

foreach标签不仅可以用于范围查询,还可以遍历集合用于批量添加。因为可以利用sql的特性,例如:insert into user_info4(u_name, u_tel) values('', ''), ('', ''), ('', '');这样执行这条sql语句就可以实现批量添加。

 

<!-- 批量更新1,这一种方式兼容性较好,当数据量大时 -->
<update id="updateBatch1" parameterType="list">
    <foreach collection="list" item="user" separator=";">
        update user_info4
        <set>
            u_name = #{user.userName}
        </set>
        where u_id = #{user.uid}
    </foreach>
</update>

foreach还可以用于遍历出多条sql语句,使得一次可以执行多条sql,当然,如果需要MySql执行多条批量操作时,需要开启批量查询功能,即在MySql的url中加入 allowMultiQueries=true 。

 

<!-- 批量更新2,使用MySQL的case when语句 -->
    <update id="updateBatch2" parameterType="list">
        update user_info4 set u_name = case u_id
        <foreach collection="list" item="user" separator=" ">
            when #{user.uid} then #{user.userName}
        </foreach>
        end
        where u_id in
        <foreach collection="list" item="user" open="(" close=")" separator=",">
            #{user.uid}
        </foreach>
    </update>

这里使用的是MySql中的case when语句来更新的,基本语法:

update user_info4 set u_name = case u_id

  when 3 then '游客1'

  when 4 then '游客2'

  end

  where u_id in(3,4);

 

posted @ 2020-05-16 10:36  张财华  阅读(922)  评论(0编辑  收藏  举报