Mybatis 动态sql语句实例详解

首先我们给出一个Mapper配置文件,用于参照使用,下面给出详细介绍

<?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="com.angel.mapper.EmployeeDao">

    <!-- 映射 -->
    <resultMap id="BaseResultMap" type="com.mybatis.Employee">
        <result column="emid" property="emId" jdbcType="INTEGER"/>
        <result column="emname" property="emName" jdbcType="VARCHAR"/>
    </resultMap>

    <!-- sql 字段 -->
    <sql id="Base_Column_List">
        emid, emname
    </sql>

    <!-- 插入单个员工 -->
    <insert id="insert">
        INSERT INTO employee (
            emid,
            emname
        ) VALUES (
            #{employee.emId,jdbcType=INTEGER},
            #{employee.emName,jdbcType=VARCHAR} 
        )
    </insert>

    <!-- 插入多个员工 -->
    <insert id="insertList">
        INSERT INTO employee (
        <include refid="Base_Column_List"/>
        )VALUES
        <foreach collection="employees" item="employee" index="index" separator=",">
            (
            #{employee.emId,jdbcType=INTEGER},
            #{employee.emName,jdbcType=VARCHAR}
            )
        </foreach>
    </insert>

    <!-- 根据id更新员工 -->
    <update id="update">
        UPDATE employee
        <set>
            <if test="employee.emId != null"> emid= #{employee.emId,jdbcType=INTEGER},</if>
            <if test="employee.emName != null"> emname= #{employee.emName,jdbcType=VARCHAR},</if>
        </set>
        WHERE emid = #{employee.emId,jdbcType=INTEGER}
    </update>

    <!-- 根据传入条件删除员工 -->
    <delete id="delete">
        DELETE from employee
        <if test="employee!=null">
            <where>
                <if test="employee.emId != null">and emid= #{employee.emId,jdbcType=INTEGER}</if>
                <if test="employee.emName != null">and emname= #{employee.emName,jdbcType=VARCHAR}</if>
            </where>
        </if>
    </delete>

    <!-- 根据传入条件找到员工(单个或者多个) -->
    <select id="findEmployee" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from employee
        <if test="employee!=null">
            <where>
                <if test="employee.emId != null">and emid= #{employee.emId,jdbcType=INTEGER}</if>
                <if test="employee.emName != null">and emname= #{employee.emName,jdbcType=VARCHAR}</if> 
            </where>
        </if>
    </select>

    <!-- 模糊查询 -->
    <select id="searchEmployee" resultType="com.mybatis.Employee">
        select <include refid="Base_Column_List"/> from employee
        <if test="employee!=null">
            <where>
                <if test="employee.emId != null">or emid like #{employee.emId,jdbcType=INTEGER}</if>
                <if test="employee.emName != null">or emname like concat('%', #{employee.emName,jdbcType=VARCHAR}, '%')</if>
            </where>
        </if>
    </select>

</mapper>

详细介绍:

统一的套路都是通过判断参数是否存在值,如果存在,则进行sql的拼接,如果不存在,则执行当前默认的sql语句,就是没有代条件的sql语句。这种写法可以有效的阻止sql注入,避免服务器奔溃,造成不必要的损失。

1、插入单个员工

<insert id="insert">
    INSERT INTO employee (
        emid,
        emname
    ) VALUES (
        #{employee.emId,jdbcType=INTEGER},
        #{employee.emName,jdbcType=VARCHAR} 
    )
</insert>

插入单个员工和平时使用的insert语句一样的,这里我没有做太大的改变。

2、插入多个员工

<insert id="insertList">
    INSERT INTO employee (
    <include refid="Base_Column_List"/>
    )VALUES
    <foreach collection="employees" item="employee" index="index" separator=",">
        (
        #{employee.emId,jdbcType=INTEGER},
        #{employee.emName,jdbcType=VARCHAR}
        )
    </foreach>
</insert>

批量插入员工,在values中进行foreach语句遍历,即可循环将多个员工插入,这里直接复制就可以使用的,改一下循环的条件即可。

3、根据id更新员工

<update id="update">
    UPDATE employee
    <set>
        <if test="employee.emId != null"> emid= #{employee.emId,jdbcType=INTEGER},</if>
        <if test="employee.emName != null"> emname= #{employee.emName,jdbcType=VARCHAR},</if>
    </set>
    WHERE emid = #{employee.emId,jdbcType=INTEGER}
</update>

在更新中,当员工的参数不为空时,则在更新语句上拼接上更新条件,然后用逗号分隔开。

4、根据传入条件删除员工

<delete id="delete">
    DELETE from employee
    <if test="employee!=null">
        <where>
            <if test="employee.emId != null">and emid= #{employee.emId,jdbcType=INTEGER}</if>
            <if test="employee.emName != null">and emname= #{employee.emName,jdbcType=VARCHAR}</if>
        </where>
    </if>
</delete>

动态条件删除,当传入的任何一个值不为空时,则删除当前的员工信息,这里可以不用根据id进行删除。

5、根据传入条件找到员工(单个或者多个)

<select id="findEmployee" resultMap="BaseResultMap">
    select <include refid="Base_Column_List"/> from employee
    <if test="employee!=null">
        <where>
            <if test="employee.emId != null">and emid= #{employee.emId,jdbcType=INTEGER}</if>
            <if test="employee.emName != null">and emname= #{employee.emName,jdbcType=VARCHAR}</if> 
        </where>
    </if>
</select>

如果传入的参数不为空,则根据条件找到对应的员工信息,当条件为空时,这里不进行拼接,则等价于执行了select * from employee,查询的是所有的数据,当有一个条件存在时,查出来的数据可能是单个或者是多个,只需要判断一下返回的集合中存在几条信息,总结如下:

  • 当集合长度只有1的时候,则可以作为查询一个员工的功能来使用
  • 当集合长度大于1的时候,则可以作为精确条件查询的功能来使用
  • 当没有查询条件,或者查询条件为null时,查询所有数据

6、模糊查询

select id="searchEmployee" resultType="com.mybatis.Employee">
    select <include refid="Base_Column_List"/> from employee
    <if test="employee!=null">
        <where>
            <if test="employee.emId != null">or emid like #{employee.emId,jdbcType=INTEGER}</if>
            <if test="employee.emName != null">or emname like concat('%', #{employee.emName,jdbcType=VARCHAR}, '%')</if>
        </where>
    </if>
</select>

前面的都是一样,当有参数时,才进入下一步,判断哪一个参数不为空,则拼接上参数的模糊查询,or是作为多条件查询,如果用and,那么这个模糊查询需要同时满足and两边的条件,这个根据你的需求来做。

Mybatis 动态sql语句详解到此结束!!

猜你喜欢

转载自blog.csdn.net/qq_38762237/article/details/81191898
今日推荐