当前位置:   article > 正文

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

sql标签

目录

1  <resultMap> 标签

2 <sql> 标签

3 <where> 标签

4 <if> 标签

5  <trim> 标签

6 <foreach> 标签

7 <set> 标签


1 <resultMap> 标签

比如以下代码:

  1. <resultMap type="SysCollege" id="SysCollegeResult">
  2. <result property="collegeId" column="college_id" />
  3. <result property="collegeCode" column="college_code" />
  4. <result property="collegeName" column="college_name" />
  5. <result property="collegeProvince" column="college_province" />
  6. <result property="collegeCity" column="college_city" />
  7. <result property="collegeDistrict" column="college_district" />
  8. <result property="collegePhone" column="college_phone" />
  9. <result property="collegeEmail" column="college_email" />
  10. <result property="collegeType" column="college_type" />
  11. <result property="collegeWebsite" column="college_website" />
  12. <result property="collegeIntroduced" column="college_introduced" />
  13. <result property="collegeLogo" column="college_logo" />
  14. <result property="collegeStudentNum" column="college_student_num" />
  15. <result property="collegeMajorNum" column="college_major_num" />
  16. <result property="collegeDeptNum" column="college_dept_num" />
  17. <result property="status" column="status" />
  18. </resultMap>

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


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

2 <sql> 标签

比如以下代码:

  1. <sql id="selectSysMajorVo">
  2. select major_id, major_code, major_name, major_type, major_degree, major_career from sys_major
  3. </sql>

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


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

3 <where> 标签

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

比如以下代码:

  1. <select id="selectSysMajorList" parameterType="SysMajor" resultMap="SysMajorResult">
  2. <include refid="selectSysMajorVo"/>
  3. <where>
  4. <if test="majorCode != null "> and major_code = #{majorCode}</if>
  5. <if test="majorName != null and majorName != ''"> and major_name like concat('%', #{majorName}, '%')</if>
  6. <if test="majorType != null and majorType != ''"> and major_type = #{majorType}</if>
  7. <if test="majorDegree != null "> and major_degree = #{majorDegree}</if>
  8. <if test="majorCareer != null and majorCareer != ''"> and major_career = #{majorCareer}</if>
  9. </where>
  10. </select>

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


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

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

4 <if> 标签

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

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

5 <trim> 标签

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

比如以下代码:

  1. <insert id="insertSysMajor" parameterType="SysMajor" useGeneratedKeys="true" keyProperty="majorId">
  2. insert into sys_major
  3. <trim prefix="(" suffix=")" suffixOverrides=",">
  4. <if test="majorCode != null">major_code,</if>
  5. <if test="majorName != null">major_name,</if>
  6. <if test="majorType != null">major_type,</if>
  7. <if test="majorDegree != null">major_degree,</if>
  8. <if test="majorCareer != null">major_career,</if>
  9. </trim>
  10. <trim prefix="values (" suffix=")" suffixOverrides=",">
  11. <if test="majorCode != null">#{majorCode},</if>
  12. <if test="majorName != null">#{majorName},</if>
  13. <if test="majorType != null">#{majorType},</if>
  14. <if test="majorDegree != null">#{majorDegree},</if>
  15. <if test="majorCareer != null">#{majorCareer},</if>
  16. </trim>
  17. </insert>

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

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

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


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

  1. insert into sys_major
  2. (major_code, major_name, major_type, major_degree, major_career)
  3. values
  4. (#{majorCode}, #{majorName}, #{majorType}, #{majorDegree}, #{majorCareer})

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

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

6 <foreach> 标签

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

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

  1. <delete id="deleteSysMajorByMajorIds" parameterType="String">
  2. delete from sys_major where major_id in
  3. <foreach item="majorId" collection="array" open="(" separator="," close=")">
  4. #{majorId}
  5. </foreach>
  6. </delete>

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

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

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

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

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

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

7 <set> 标签

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

比如以下代码:

  1. <update id="updateDictType" parameterType="SysDictType">
  2. update sys_dict_type
  3. <set>
  4. <if test="dictName != null and dictName != ''">dict_name = #{dictName},</if>
  5. <if test="dictType != null and dictType != ''">dict_type = #{dictType},</if>
  6. <if test="status != null">status = #{status},</if>
  7. <if test="remark != null">remark = #{remark},</if>
  8. <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
  9. update_time = sysdate()
  10. </set>
  11. where dict_id = #{dictId}
  12. </update>

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/855283
推荐阅读
相关标签
  

闽ICP备14008679号