首页 > 数据库 >简述MyBatis动态SQL

简述MyBatis动态SQL

时间:2023-10-11 21:44:52浏览次数:28  
标签:username 标签 Param 简述 SQL MyBatis password where

简述MyBatis动态SQL


前言

  MyBatis是一个用于Java持久层的开源框架,它提供了一种简化数据库访问的方式。MyBatis的动态SQL功能允许我们根据不同的条件动态生成SQL语句,以实现更灵活的数据库操作。


在MyBatis中,我们经常使用以下标签来编写动态SQL:

< if/>

作用:用于实现简单的条件判断,例如:非空判断,空值判断等。

注意:如果< if/>标签test属性的值为true则标签中的内容会执行;反之标签中的内容不会得到执行。使用< if/>时请注意,在test的属性值中不必再使用占位符#{参数名}的形式获取参数值,直接使用参数名获取对应的参数值即可。

List<User> queryUserWithIf(@Param("username") String username, @Param("password") String password);

<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/>

作用:

1.如果满足条件,那么标签将自动添加where关键字并自动删除查询条件中最前方多余的and或or关键字

2.如果不满足条件,那么标签失效而且不添加and或or关键字

3.如果没有where子句则不生成where关键字

注意:在< if/>标签的案例中为了防止SQL语句拼接后的意外报错特意加入了“where 1=1”的查询条件,以此避免了where后面第一个单词是and或者or之类的关键字。为了更合理地处理类似的状况,我们可以使用< where/>标签。

List<User> queryUserWithWhere(@Param("username") String username, @Param("password") String password);

<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/>

作用:
< choose/>标签常与< when/>标签、< otherwise/>标签搭配使用以实现多分支选择,其使用方式和作用非常类似于Java语言中的switch-case-default语句,它只从多个分支中选一个执行。

List<User> queryUserWithChoose(@Param("username") String username, @Param("password") String password);

<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/>

作用:

1.如果满足条件,那么标签将自动添加set关键字

2.如果不满足条件,那么标签失效也不会添加set关键字

3.删除set子句中多余的逗号

注意:通常情况下,< set/>标签结合< if/>标签一起使用,主要用于管理映射文件中语句的set子句。

int updateUserWithSet(User user);

<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语句前后添加或删除一些内容。

注意:
标签常用属性及其作用如下:

1.prefix:在SQL语句前添加内容。

2.prefixOverrides:删除SQL语句前多余的关键字或字符。

3.suffix:在SQL语句后添加内容。

4.suffixOverrides:删除SQL语句后多余的关键字或字符。

int updateUserWithTrim(User user);

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

< bind/>

作用:< bind/>标签用于数据绑定,常用于模糊查询。

List<User> queryUserWithBind(@Param("username") String username);

<select id="queryUserWithBind" resultType="User">
    select * from user
    <where>
        <if test="username !=null and username !=''">
            <bind name="compositeUsername" value="'%'+username+'%'"/>
            username like #{compositeUsername}
        </if>
    </where>
</select>

< foreach/>

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

注意:
标签常用属性及其作用如下:

1.collection:待遍历的集合。

2.item:当集合为List和数组时,index是对象的索引。
当集合是Map时,index是Map的键。

3.index:在SQL语句后添加内容。

4.open:表示开始符号,其常用值为"("。

5.separator:表示各元素之间的分隔符,其常用值为","。

6.close:表示结束符号,其常用值为")"。

// foreach遍历List
List<User> queryUserWithForeach1(@Param("userIDList") List<Integer> userIDList);
<!-- 测试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遍历数组
List<User> queryUserWithForeach2(@Param("userIDArray") int[] userIDArray);
<!-- 测试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的使用
List<User> queryUserWithForeach3(@Param("userMap") Map<String ,Object> userMap);
<!-- 测试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值。

List<User> queryUserWithInclude(@Param("username") String username, @Param("password") String password);

<!-- 定义SQL片段 -->
<sql id="columns">id,username,password,gender</sql>

<!-- 测试动态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>

总结

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

标签:username,标签,Param,简述,SQL,MyBatis,password,where
From: https://www.cnblogs.com/cjw2002/p/17758273.html

相关文章

  • Docker 安装mysql8.0
    环境CentOS7、MySQL8、SQLyog12需求在Docker中部署MySQL8步骤1、从docker仓库中拉去mysql8.0dockerpullmysql:8.0如果使用dockerpullmysql默认拉取的是最新版本的mysql上面我拉去的是8.0的版本,最后拉取过来的是8.0.27如果有想要指定的版本,可以直接写指定版本,......
  • 好用的IDEA插件之Mybatis Log Free
    今天看到一个公众号说的插件,可以记录MyBatis执行的SQL语句,不用去拼接sql语句,可以直接看到sql语句。没用插件之前: 可以看到那个分页查询需要去拼接sql语句,把2拼接上。使用了插件后; 不用自己拼接,可以直接看见sql语句。非常方便,也可以帮助排查sql语句错误。最后那个插件样......
  • MySQL 8.0参数最佳实践
    MySQL8.0.34版本my.cnf最佳实践参考实例[client]port=3307socket=/data/mysql/3307/run/mysql.sockdefault-character-set=utf8mb4[mysql]port......
  • 软件测试|深入理解SQL RIGHT JOIN:语法、用法及示例解析
    引言在SQL中,JOIN是一种重要的操作,用于将两个或多个表中的数据关联在一起。SQL提供了多种JOIN类型,其中之一是RIGHTJOIN。RIGHTJOIN用于从右表中选择所有记录,并将其与左表中匹配的记录组合在一起。本文将深入探讨SQLRIGHTJOIN的语法、用法以及通过实例解析来说明其作用。RIGH......
  • 软件测试|深入理解SQL FULL JOIN:语法、用法及示例解析
    简介在SQL中,JOIN是一个强大的操作,它允许将两个或多个表中的数据进行关联。SQL提供了多种JOIN类型,其中之一是FULLJOIN。FULLJOIN允许从左表和右表中选择所有记录,并将它们组合在一起。本文将深入探讨SQLFULLJOIN的语法、用法,并通过实例解析来说明其作用。FULLJOIN基本语法......
  • 软件测试|教你学会SQL INNER JOIN的使用
    简介当我们在处理关系型数据库时,经常会涉及到将多个表进行连接操作。而其中最常见的连接操作是INNERJOIN。本文将详细介绍INNERJOIN的概念、语法和用法,以及一些实际应用示例。INNERJOIN的概念INNERJOIN是一种SQL操作,它通过共享相同值的列将两个或多个表连接在一起......
  • 软件测试|教你学会SQL LEFT JOIN的使用
    简介当我们在处理关系型数据库时,常常需要从多个表中获取数据,并且可能存在某些表之间没有完全匹配的记录。在这种情况下,LEFTJOIN操作可以派上用场。本文将详细介绍LEFTJOIN的概念、语法和用法,以及一些实际应用示例。LEFTJOIN的概念LEFTJOIN是一种SQL操作,它通过连接......
  • 2023_10_11_MYSQL_DAY_03_笔记_下
    2023_10_11_MYSQL_DAY_03_笔记_下#截断表的作用是把原来的表摧毁,重新创建一个结构和原来一模一样的新表,语法如下:TRUNCATETABLEtable;#TRUNCATE和DELETE区别#1、TRUNCATE是DDL命令,使用ROLLBACK不可以回滚。而DELETE是DML命令,使用ROLLBACK可以回滚。#2、DELETE可以通过指定......
  • ADO.NET读取MySQL数据库的三种方式:DataReader、DataSet、DataView
    https://blog.csdn.net/lilongsy/article/details/127351421ADO.NET读取MySQL数据库有多种方式:DataReader、DataSet、DataView。Command对象的ExecuteScalar方法查询数据库获取某个单个值,但是如果获取多行、多列可以用ExcecuteReader,ExcecuteReader返回一个DataReader的数据流对......
  • sql server导入表的一些函数使用
    truncatetableJC_BMDA;insertintoJC_BMDA(bh,mc,qdmc,pym,ty)selectright('0'+rtrim(convert(varchar(5),id)),2)bh,bz,'jnx',upper(dbo.fGG_GetPY(bz)),0from(selectROW_NUMBER()over(orderbyxh)asid,*from(selectconvert(int,......