mybatis动态sql

在实际的场景中,经常会遇到动态SQL的增、删、改、查问题,这里就必要说明何谓动态SQL,我们举一个实际的例子,比如,在一个web工程中,经常会有一个搜索框,并且在搜索之前通常会进行一个关键词的过滤,比如可以过滤的条件有:姓名、年龄等,这样的话当我们姓名和年龄都不选,则等价于下面的SQL语句

select * from students;   -- 不去限制姓名和年龄
  • 1

当我们将年龄选择为>20时,相当于下面的SQL语句:

select * from students where age>20;
  • 1

当我们同时选择条件姓名为:张三,年龄>20,则相当于下面的SQL语句:

 select * from students where age>20 and name='张三';
  • 1

当我们有很多的条件时,此时就需要我们去组合这些条件,并动态的生成一个可执行的SQL语句,这样就不是一个简单的SQL语句能够解决问题,那么我们该怎么办呢?在MyBatis中同样是支持这种动态SQL的写法,具体见下面的内容。

MyBatis动态SQL支持


动态SQL之查询

<?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="studentNameSpace">

    <!-- resultMap标签将了工程entity实体类中的对象与数据库中的表对应起来
         resultMap中的id属性是一个唯一的名字
         子标签中的id属性用来指定主键
         result标签用来指定其他的键,其中property属性是指实体中的字段,对应的
         column属性表示的数据库中的响应的字段
     -->
    <resultMap type="com.jpzhutech.entity.Student" id="studentMap">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="sal" column="sal"/>
    </resultMap>

    <!-- 动态查询SQL语句 -->
    <select id="findAll" parameterType="map" resultMap="studentMap">
        select id , name , sal
        from students
        <where>
            <if test="pid!=null" >
                and id = #{pid}    <!-- #{}和之前使用c3p0的时候写的?含义是相同的 -->
            </if>

            <if test="pname!=null" >
                and name = #{pname}
            </if>

            <if test="psal!=null" >
                and sal = #{psal}
            </if>
        </where>
    </select>
</mapper>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
动态SQL之插入

<!-- 动态insert -->
    <!-- 定义两个sql片段,第一个对应字段名,id属性值任意并且唯一 -->
    <sql id="key">
      <trim suffixOverrides=",">
        <if test="id!=null">
            id,
        </if>

        <if test="name!=null">
            name,
        </if>

        <if test="sal!=null">
            sal,
        </if>
      </trim>
    </sql>


    <!-- 定义第二个sql片段,第二个对应?,key属性值任意并且唯一 -->
    <sql id="value">
      <trim suffixOverrides=",">
        <if test="id!=null">
            #{id},
        </if>

        <if test="name!=null">
            #{name},
        </if>

        <if test="sal!=null">
            #{sal},
        </if>
      </trim>
    </sql>

    <!-- <include refid="key"/>和<include refid="value"/>表示引用上面sql片段 -->
    <insert id="insertStudent" parameterType="com.jpzhutech.entity.Student">
        insert into students(<include refid="key"/>) values(<include refid="value"/>);
    </insert>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
动态SQL之删除

 <!-- 动态删除操作  delete from students where id in(?,?,?);-->
    <delete id="deleteStudent">
        delete from students where id in
        <!-- foreach用于迭代数组元素 
             open表示开始符号
             close表示结束符号
             seprator表示元素间的分割符
             items表示迭代的数组
        -->
        <foreach collection="array" open="(" close=")" separator="," item="ids">
            #{ids}
        </foreach>
    </delete>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
动态SQL之更新

 <!-- 动态更新SQL语句,update table_name set name=? , sal=? where id=?,
         其中id不能更新,因为id为主键,这个动态更新该怎么写呢? 
         set标签会自动判断后面是否加,
         -->
    <update id="updateStudent" parameterType="map" >
        update students
        <set>
            <if test="pname!=null">
                name = #{pname},
            </if>

            <if test="psal!=null">
                sal = #{psal},
            </if>
        </set>
        where id = #{pid}
    </update>

猜你喜欢

转载自blog.csdn.net/Fighting_xr/article/details/80015283