当前位置: 首页 > news >正文

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

目录

1  <resultMap> 标签

2 <sql> 标签

3 <where> 标签

4 <if> 标签

5  <trim> 标签

6 <foreach> 标签

7 <set> 标签


1 <resultMap> 标签

比如以下代码:

    <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> 标签

比如以下代码:

    <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 子句

比如以下代码:

    <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 语句的部分内容

比如以下代码:

    <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语句是:

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> 标签,用于遍历传入的数组

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

    <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 片段

比如以下代码:

 	<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 字段为当前时间

http://www.lryc.cn/news/283957.html

相关文章:

  • 云锁防火墙编译安装nginx-plugin模块
  • 【服务器数据恢复】服务器迁移数据时lun数据丢失的数据恢复案例
  • 6.4.2转换文件
  • 智能驾驶新浪潮:SSD与UFS存储技术如何破浪前行?-UFS篇
  • TS 学习笔录(持续更新中)
  • RabbitMQ安装和使用
  • 使用pyechart创建折线图
  • Vue3+Ts:使用i18n实现国际化与全局动态下拉框框切换语言
  • 多目标优化中常用的差分进化算法DE【2】
  • 游卡:OceanBase在游戏核心业务的规模化降本实践
  • LightDB - oracle_fdw 过滤条件下推增强【24.1】
  • 【计算机网络】HTTP协议以及简单的HTTP服务器实现
  • 04 SpringBoot整合Druid/MyBatis/事务/AOP+打包项目
  • C++程序编译时的_GLIBCXX_USE_CXX11_ABI参数的值选择,适配昇腾Transformer推理加速库与LLM推理模型库
  • 什么是站群服务器?
  • 《WebKit 技术内幕》之四(3): 资源加载和网络栈
  • vue3-模板引用
  • 聚类模型评估指标
  • 测试 ASP.NET Core 中间件
  • 智能小程序小部件(Widget)媒体组件属性说明和示例代码汇总
  • enum的比较
  • 网工每日一练(1月15日)
  • henauOJ 1113: 计算x的n次方
  • 64.Spring事件监听的核心机制是什么?
  • 《C++大学教程》3.12Account类
  • 【工作记录】基于springboot3+springsecurity实现多种方式登录及鉴权(二)
  • CSS笔记III
  • Bit.Store 加密卡集成主流 BRC20通证,助力 BTC 生态流动性
  • openssl3.2 - 官方demo学习 - mac - siphash.c
  • (六)深入理解Bluez协议栈之“GATT Client Profile”