MySql关于XML的运用记录

特殊字符

符号 转译 描述
< &lt; 小于
<= &lt;= 小于等于
> &gt; 大于
>= &gt;= 大于等于
& &amp;
' &apos; 单引号
" &quot; 双引号

IF()或者if()

IF(condition, if_true, if_false)

描述

  • condition : 是要检查的条件
  • if_true : 是在条件为真时要执行的语句
  • if_false : 是在条件为假时要执行的语句

CASE WHEN

SELECT 
CASE WHEN STATUS='1' THEN '状态1'
WHEN STATUS='2' THEN '状态2'
WHEN STATUS='3' THEN '状态3'
WHEN STATUS='0' THEN '状态4'
ELSE '状态5' END AS '状态' 
FROM table;

IFNULL

描述:IFNULL函数是MySQL控制流函数之一,它接受两个参数,如果不是NULL,则返回第一个参数。 否则,IFNULL函数返回第二个参数。

IFNULL(expression_1,expression_2);

date(字段)

描述:date()函数提取日期或日期/时间表达式的日期部分。

SELECT DATE('2023-9-11 11:02:24') as dateValue 

新增表字段

ALTER TABLE table ADD column `字段` int(11) DEFAULT NULL COMMENT '字段描述';
ALTER TABLE 表名 ALTER COLUMN 字段名 SET DEFAULT 默认值;

描述

  • table : 表名
  • 字段 :新增字段 例如:project_name
  • int(11) : 字段类型
  • DEFAULT NULL : 默认为空(NULL)
  • NOT NULL : 默认不为空(NULL)

XML参数判空

整型

<if test="type != null">
</if>

字符串

<if test="type != null and type != ''">
</if>

参数是否相等

整型

 <if test="xxx !=null and xxx !='' or xxx == 0">
 </if >

字符串:方法一

 <if test="params.contractCategory != null and params.contractCategory != '' and params.contractCategory == '2'.toString()">
 </if >

字符串:方法二

<if test=' delFlag == "2" '>
    a.del_flag = #{delFlag}
</if>

字符串:方法三

<if test=' delFlag.equals("2") '>
    a.del_flag = #{delFlag}
</if>

CASE WHEN使用

<choose>
    <when test="map.userId != null and map.userId != ''">
        and register.user_id = #{map.userId}
    </when>
    <when test="map.contractCode != null and map.contractCode != ''">
        and register.contract_code = #{map.contractCode}
    </when>
    <when test="map.entryAuditDate != null and map.entryAuditDate != '' and map.exitAuditDate != null and map.exitAuditDate != '' ">
        and register.entry_audit_date &gt;= #{map.entryAuditDate} and register.exit_audit_date &lt;= #{map.exitAuditDate}
    </when>
    <otherwise>
        and register.attendance_identification = 0
    </otherwise>
</choose>

List使用

<foreach collection="array" index="index" item="item" open="(" separator="," close=")">
    #{item}
</foreach>

描述

  • array : 定义的数组名

批量新增

insert into `tableName`(fieldName1, fieldName2)
    values
    <foreach collection="ledgerList" item="ledger" separator=",">
        (#{ledger.fieldName1},#{ledger.fieldName2})
    </foreach>

描述

  • tableName : 表名称
  • fieldName1 : 数据库字段(示例 : id)
  • fieldName2 : 数据库字段(示例 : user_name)
  • ledgerList :List集合
  • ledger.fieldName1 :fieldName1 映射实体参数(示例 : id)
  • ledger.fieldName2 :fieldName2 映射实体参数(示例 : userName)

List的使用

 <if test="paramsMap.projectIdList != null and paramsMap.projectIdList.size > 0">
                AND sfp.project_id in
                <foreach collection="paramsMap.projectIdList" item="projectId" separator="," open="(" close=")">
                    #{projectId}
                </foreach>
            </if>

描述

  • paramsMap: xml实体别名 示例:@Param("paramsMap")实体类 实体类别名
  • projectIdList:List类型集合

List、foreach 、多个参数模糊查询的使用

<if test="paramsMap.orgLevel != null and paramsMap.orgLevel.size > 0">
            AND
            <foreach collection = "paramsMap.orgLevel" item = "level" open = '(' close = ')' separator = 'OR'>
                org.level LIKE CONCAT(#{level}, '%')
            </foreach>
        </if>

描述

  • paramsMap: 别名 示例:@Param("paramsMap")参数集合 别名
  • orgLevel:List类型集合
  • paramsMap:Map<String,Object>类型

Integer[]的使用

<if test="paramsMap.auditStatusList != null and paramsMap.auditStatusList.length > 0">
                AND sfp.audit_status in
                <foreach collection="paramsMap.auditStatusList" item="auditStatus" separator="," open="(" close=")">
                    #{auditStatus}
                </foreach>
            </if>

描述

  • paramsMap: xml实体别名 示例:@Param("paramsMap")实体类 实体类别名
  • auditStatusList:Integer[]类型集合
posted @ 2023-09-04 17:42  晓之羽  阅读(39)  评论(0编辑  收藏  举报