首页 > 数据库 >MyBatis动态SQL

MyBatis动态SQL

时间:2023-10-05 21:44:08浏览次数:30  
标签:username password 标签 user SQL MyBatis 动态 id

MyBatis动态SQL

在项目的开发中,编码人员经常需要根据不同的条件拼接SQL语句。在组拼SQL语句的过程中除了实现核心功能以外还需要处处小心,时时警惕确保不遗漏必要的标点符号、空格以及关键字。

动态SQL常用标签如下:

<if/>

<where/>

<choose/>、<when/>、<otherwise/>

<set/>

<trim/>

<bind/>

<foreach/>

<sql/>

<include/>

if查询案例代码

<select id="queryUserWithIf" resultType="User">
    select * from user where 1=1
    <if test="username !=null and username !='' ">
        and username=#{username}
    </if>
    <if test="password !=null and password !='' ">
        and password=#{password}
    </if>
</select>

where查询案例代码

<select id="queryUserWithWhere" resultType="User">
    select * from user
    <where>
        <if test="username !=null and username !=''">
            and username=#{username}
        </if>
        <if test="password !=null and password !=''">
            and password=#{password}
        </if>
    </where>
</select>

<choose/>、<when/>、<otherwise/>案例代码

<select id="queryUserWithChoose" resultType="User">
    select * from user
    <where>
        <choose>
            <when test="username !=null and username !=''">
                and username=#{username}
            </when>
            <when test="password !=null and password !=''">
                and password=#{password}
            </when>
            <otherwise>
                and username like concat('%','lu','%')
            </otherwise>
        </choose>
    </where>
</select>

<set>案例代码

<update id="updateUserWithSet" parameterType="User">
    update user
    <set>
        <if test="username !=null and username !=''">
            username=#{username},
        </if>
        <if test="password !=null and password !=''">
            password=#{password},
        </if>
        <if test="gender !=null and gender !=''">
            gender=#{gender}
        </if>
    </set>
    where id=#{id}
</update>

<trim/>

<trim/>标签常用于用于在SQL语句前后添加或删除一些内容。<trim/>标签常用属性及其作用如表5-1所示。

表5-1 <trim/>标签属性

属性

释义

prefix

在SQL语句前添加内容

prefixOverrides

删除SQL语句前多余的关键字或字符

suffix

在SQL语句后添加内容

suffixOverrides

删除SQL语句后多余的关键字或字符

<trim>案例代码

<update id="updateUserWithTrim" parameterType="User">
    update user
    <trim prefix="set" suffixOverrides="and">
        username=#{username} and
    </trim>
    where id=#{id}
</update>

在映射文件中依据用户id进行更新操作。在更新语句中利用<trim/>标签的prefix属性在username前添加set关键字并利用<trim/>标签的suffixOverrides属性去掉and关键字。假如不使用<trim/>标签那么拼接后的SQL语句为update user username=#{username} and where id=#{id} 很明显,这条SQL语句是错误的。

<foreach/>

<foreach/>标签用于在SQL语句中遍历List、数组、Map等集合。除此以外,该标签常用于SQL中的in查询。

<foreach/>标签常见属性及其作用如表5-2所示。

表5-2 < foreach />标签属性

属性

释义

collection

待遍历的集合

item

集合中被遍历的当前对象

index

当集合为List和数组时index是对象的索引。

当集合是Map时index是Map的键。

open

表示开始符号,其常用值为"("

separator

表示各元素之间的分隔符,其常用值为","

close

表示结束符号,其常用值为")"

<foreach>案例代码

<!-- 测试foreach遍历List -->
<select id="queryUserWithForeach1" resultType="User">
    select * from user where id in
    <foreach collection="userIDList" open="(" separator="," close=")" item="userID">
        #{userID}
    </foreach>
</select>

<!-- 测试foreach遍历数组 -->
<select id="queryUserWithForeach2" resultType="User">
    select * from user where id in
    <foreach collection="userIDArray" index="i" item="userID" open="(" separator="," close=")" >
        #{userID}
    </foreach>
</select>

<!-- 测试foreach与Map的使用 -->
<select id="queryUserWithForeach3" resultType="User">
    select * from user where gender = #{userMap.gender} and id in
    <foreach collection="userMap.userIDList" item="userID" index="key" open="(" separator="," close=")" >
        #{userID}
    </foreach>
</select>

<sql/>

<sql/>标签用于定义可重用的SQL片段,该标签常用属性为id作为SQL片段的唯一标识。

<include/>

<include/>标签常与<sql/>标签配合使用,即使用<include/>引用已经定义的SQL片段。<include/>标签的属性refid表示引用的<sql/>标签的id值。

<sql>、<include>案例代码

 

<!-- 测试动态SQL语句include -->
<select id="queryUserWithInclude" resultType="User">
    select <include refid="columns"/> from user
    <where>
        <if test="username !=null and username !=''">
            and username=#{username}
        </if>
        <if test="password !=null and password !=''">
            and password=#{password}
        </if>
    </where>
</select>

 

标签:username,password,标签,user,SQL,MyBatis,动态,id
From: https://www.cnblogs.com/shangeg/p/17743961.html

相关文章

  • mybatis配置
     修改yml文件:mybatis:mapper-locations:classpath*:mapper/*.xmltype-aliases-package:com.example.emos.wx.db.pojoconfiguration:log-impl:org.apache.ibatis.logging.stdout.StdOutImplmap-underscore-to-camel-case:truelogging:level:......
  • MyBatis过时了吗?
    是的,已经过时了,虽然中文社区仍有惯性,但新项目不建议使用。所有2022年以前的文章,说mybatis的优点,在hibernate4.x+jpa2.0已经被抹平。到spring-data-jpa的出现,就已经完全被超越了:多年前引以为傲的性能,已经有很多基准测试Q证明只比JPAImplementation快一丢丢自JPA标准也能......
  • MyBatis参数传递和接受(@Param)
    一、@Param注解传递多个普通类型参数在接收端便可使用#{别名}的方式接收参数。 简单地说,在接口文件中使用org.apache.ibatis.annotations.Param类型的注解@Param为参数定义别名;在映射文件中使用#{别名}获取参数。 在此,我们以查询为例讲解利用@Param注解传递和接收多个普通......
  • 动态规划--DP
    动态规划动态规划是一种通过把原问题分解为相对简单的子问题的方式求解复杂问题的方法。背包01背包每个物体只有两种可能的状态(取与不取),对应二进制中的\(0\)和\(1\),这类问题便被称为「0-1背包问题」。状态转移方程:\[f_{i,j}=\max(f_{i-1,j},f_{i-1,j-w_{i}}+v_{i})\]这......
  • SQL SERVER 存储过程执行日志记录方法(有案例)
    查询历史执行总体情况SELECTTOP100db_name(d.database_id)asDBName,s.nameas存储名称,s.type_descas存储类型,d.cached_timeasSP添加到缓存的时间,d.last_execution_timeas上次执行SP的时间,d.last_elapsed_timeas[上次执行SP所用的时间(μs)],......
  • MySQL知识点归纳
    1.索引  索引按照物理实现方式,可以分为两种:聚簇(聚集)和非聚簇(非聚集)索引。也把非聚集索引称为二级索引或者辅助索引。1.1聚簇索引  聚簇索引不是一种单独的索引类型,而是一种数据存储方式,即索引的存储方式(所有的用户记录都存储在了叶子节点),所以聚簇索引:索引即数据,数据即索......
  • mysql命令行执行sql文件
    方法一:mysql命令,直接在服务器上执行mysql–u用户名–p密码–D数据库<【sql脚本文件路径全名】例如:mysql-uroot-proot-Dtest</tmp/test.sql方法二:source命令,登录mysqlclient执行source【sql脚本文件路径全名】例如:source/tmp/test.sql......
  • mysql
    在命令提示符中先输入cdC:\ProgramFiles\MySQL\MySQLServer8.0\bin打开目录,然后输入mysql-hlocalhost-uroot-p登录数据库,再输入数据库密码6km,登录成功查看信息输入status命令可以查看MySQL的版本信息 ......
  • SqlSession与SqlSessionFactory
    2.2.3SqlSessionSqlSession是MyBatis框架中极其重要的接口。SqlSession类似于JDBC中的Connection,它代表MyBatis和数据库的一次会话,主要用于执行持久化操作。SqlSession对象底层封装了JDBC连接,所以可以直接使用SqlSession对象执行已映射的SQL语句。SqlSession中包含了所有执行SQL......
  • MyBatis运行原理
    MyBatis运行原理为了更加深入和清晰地掌握MyBatis,我们来深入了解MyBatis的运行原理,如图所示。 从上图可以看出,MyBatis框架在操作数据库时大致经过了10个步骤;具体如下:读取MyBatis全局配置文件mybatis-config.xml。该文件中存有MyBatis的运行信息以及数据库连接信息。加......