Mysql:重点且常用的 SQL 标签整理

目录

[1 <resultMap> 标签](#1 <resultMap> 标签)

[2 <sql> 标签](#2 <sql> 标签)

[3 <where> 标签](#3 <where> 标签)

[4 <if> 标签](#4 <if> 标签)

[5 <trim> 标签](#5 <trim> 标签)

[6 <foreach> 标签](#6 <foreach> 标签)

[7 <set> 标签](#7 <set> 标签)


1 <resultMap> 标签

比如以下代码:

XML 复制代码
    <resultMap type="SysCollege" id="SysCollegeResult">
        <result property="collegeId"    column="college_id"    />
        <result property="collegeCode"    column="college_code"    />
        <result property="collegeName"    column="college_name"    />
        <result property="collegeProvince"    column="college_province"    />
        <result property="collegeCity"    column="college_city"    />
        <result property="collegeDistrict"    column="college_district"    />
        <result property="collegePhone"    column="college_phone"    />
        <result property="collegeEmail"    column="college_email"    />
        <result property="collegeType"    column="college_type"    />
        <result property="collegeWebsite"    column="college_website"    />
        <result property="collegeIntroduced"    column="college_introduced"    />
        <result property="collegeLogo"    column="college_logo"    />
        <result property="collegeStudentNum"    column="college_student_num"    />
        <result property="collegeMajorNum"    column="college_major_num"    />
        <result property="collegeDeptNum"    column="college_dept_num"    />
        <result property="status"    column="status"    />
    </resultMap>

上述是 <resultMap> 标签来映射查询结果到 SysCollege 对象的示例,该代码定义了一个名为 SysCollegeResult 的结果映射,用于将查询结果中的列与 SysCollege 对象的属性进行映射,这样定义了结果映射之后,当执行查询语句时,MyBatis 将会根据该映射将查询结果中的列值赋给 SysCollege 对象的对应属性。


使用定义好的 <resultMap>标签:

2 <sql> 标签

比如以下代码:

XML 复制代码
    <sql id="selectSysMajorVo">
        select major_id, major_code, major_name, major_type, major_degree, major_career from sys_major
    </sql>

这个就是定义好的sql代码块,可以在其它操作中直接使用,可以减少重复代码的编写,是非常方便的。


使用定义好的 <sql>标签:

3 <where> 标签

<where> 大多数情况下使用在,根据条件动态生成查询条件,生成 WHERE 子句

比如以下代码:

XML 复制代码
    <select id="selectSysMajorList" parameterType="SysMajor" resultMap="SysMajorResult">
        <include refid="selectSysMajorVo"/>
        <where>  
            <if test="majorCode != null "> and major_code = #{majorCode}</if>
            <if test="majorName != null  and majorName != ''"> and major_name like concat('%', #{majorName}, '%')</if>
            <if test="majorType != null  and majorType != ''"> and major_type = #{majorType}</if>
            <if test="majorDegree != null "> and major_degree = #{majorDegree}</if>
            <if test="majorCareer != null  and majorCareer != ''"> and major_career = #{majorCareer}</if>
        </where>
    </select>

比如:如果 majorCode 不为空,则生成 and major_code = #{majorCode} 的查询条件,其它也是一样。


where和<where>标签有什么区别:

WHERE 关键字是静态的,需要手动编写每个查询条件,并使用连接符(例如 "AND" 或 "OR")来拼接条件。而 <where> 标签是动态的,可以根据条件的存在与否来动态生成 WHERE 子句,并自动处理条件之间的连接符(比如上述代码中的and).

4 <if> 标签

<if> 标签通常用于在动态 SQL 中根据条件判断是否包含某部分 SQL 片段

比如跟上述<where>标签配合使用

5 <trim> 标签

<trim> 元素的作用是根据条件动态生成 SQL 语句的部分内容

比如以下代码:

XML 复制代码
    <insert id="insertSysMajor" parameterType="SysMajor" useGeneratedKeys="true" keyProperty="majorId">
        insert into sys_major
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="majorCode != null">major_code,</if>
            <if test="majorName != null">major_name,</if>
            <if test="majorType != null">major_type,</if>
            <if test="majorDegree != null">major_degree,</if>
            <if test="majorCareer != null">major_career,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="majorCode != null">#{majorCode},</if>
            <if test="majorName != null">#{majorName},</if>
            <if test="majorType != null">#{majorType},</if>
            <if test="majorDegree != null">#{majorDegree},</if>
            <if test="majorCareer != null">#{majorCareer},</if>
         </trim>
    </insert>

prefix="SET":指定生成的 SET 子句的前缀为 "SET"。

suffixOverrides=",":指定如果 SET 子句的最后一个字符是逗号(,),则将其移除。

prefix="(" 指定了插入语句的列名部分的前缀为左括号 (suffix=")" 则指定了该部分的后缀为右括号 ),即 () 之间就是列名列表


当上述的值都不为空,生成的sql语句是:

XML 复制代码
insert into sys_major
(major_code, major_name, major_type, major_degree, major_career)
values
(#{majorCode}, #{majorName}, #{majorType}, #{majorDegree}, #{majorCareer})

在这段代码中,useGeneratedKeys="true" 表示要从数据库中获取自动生成的主键值,keyProperty="majorId" 则指定了自动生成的主键值将赋给 SysMajor 对象中的 majorId 属性。

如果执行插入操作成功,并且数据库返回了自动生成的主键值,MyBatis 会将该值赋给 majorId 属性,作为插入操作的返回值。因此,如果程序需要获取插入操作生成的主键值,则可以通过 majorId 属性来获取。如果插入操作失败或者没有生成主键值,则 majorId 属性的值不会改变。

6 <foreach> 标签

<foreach> 标签,用于遍历传入的数组

比如以下代码:实现批量删除!

XML 复制代码
    <delete id="deleteSysMajorByMajorIds" parameterType="String">
        delete from sys_major where major_id in 
        <foreach item="majorId" collection="array" open="(" separator="," close=")">
            #{majorId}
        </foreach>
    </delete>

item="majorId":指定遍历过程中当前元素的别名为 majorId

collection="array":指定要遍历的集合为 array,即传入的 majorId 数组。

open="(":指定循环开始时的字符为 (

separator=",":指定每个元素之间的分隔符为 ,

close=")":指定循环结束时的字符为 )

#{majorId}:表示当前遍历到的 majorId 元素,会被替换为对应的值。

7 <set> 标签

<set> 标签用于定义一个包含更新字段内容的 SQL 片段

比如以下代码:

XML 复制代码
 	<update id="updateDictType" parameterType="SysDictType">
 		update sys_dict_type
 		<set>
 			<if test="dictName != null and dictName != ''">dict_name = #{dictName},</if>
 			<if test="dictType != null and dictType != ''">dict_type = #{dictType},</if>
 			<if test="status != null">status = #{status},</if>
 			<if test="remark != null">remark = #{remark},</if>
 			<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
 			update_time = sysdate()
 		</set>
 		where dict_id = #{dictId}
	</update>

update_time = sysdate() 表示更新 update_time 字段为当前时间

相关推荐
E___V___E1 小时前
MySQL数据库入门到大蛇尚硅谷宋红康老师笔记 高级篇 part 2
数据库·笔记·mysql
m0_748254882 小时前
mysql之如何获知版本
数据库·mysql
小金的学习笔记2 小时前
如何在本地和服务器新建mysql用户和密码
运维·服务器·mysql
星星点点洲3 小时前
【操作幂等和数据一致性】保障业务在MySQL和COS对象存储的一致
java·mysql
_院长大人_4 小时前
Docker Mysql 数据迁移
mysql·adb·docker
爱编程的小庄4 小时前
web网络安全:SQL 注入攻击
前端·sql·web安全
史迪仔01126 小时前
【SQL】SQL多表查询
数据库·sql
Quz6 小时前
MySQL:修改数据库默认存储目录与数据迁移
数据库·mysql
lozhyf6 小时前
基于 Flask 与 MySQL 构建简单的博客系统
python·mysql·flask
焱焱枫7 小时前
自适应SQL计划管理(Adaptive SQL Plan Management)在Oracle 12c中的应用
数据库·sql·oracle