首页 > 数据库 >【SQL用法】Mybatis框架中的xml文件中经常使用的sql语句

【SQL用法】Mybatis框架中的xml文件中经常使用的sql语句

时间:2023-05-24 21:04:00浏览次数:47  
标签:xml customer name sql site status SQL id CONCAT


本文目录

一、insert语句

二、select查询语句

三、批量添加

四、与时间比较相关的


项目中经常会用到的sql语句有:

一、insert语句

<!--保存用户信息-->
<insert id="save">
        insert into mainsite_product_message
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="customerName != null and customerName != ''">
                customer_name,
            </if>
            <if test="customerPhone != null and customerPhone != ''">
                customer_phone,
            </if>
            <if test="customerEmail != null and customerEmail != ''">
                customer_email,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="customerStatus != null and customerStatus != ''">
                customer_status,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="customerName != null and customerName != ''">
                #{customerName},
            </if>
            <if test="customerPhone != null and customerPhone != ''">
                #{customerPhone},
            </if>
            <if test="customerEmail != null and customerEmail != ''">
                #{customerEmail},
            </if>
            <if test="createTime != null">
                #{createTime},
            </if>
            <if test="customerStatus != null and customerStatus != ''">
                #{customerStatus},
            </if>
        </trim>
    </insert>

    <!--修改用户信息-->
    <update id="update">

    </update>

    <!--批量更改客户状态-->
    <update id="updateStatus">
        update mainsite_product_message set customer_status = 1 where id in
        <foreach collection="array" item="id" open="(" separator="," close=")" index="index">
            #{id}
        </foreach>
    </update>

    <!--根据主键删除留言,物理删除-->
    <delete id="deleteByPrimaryKey">
        delete from mainsite_product_message where id = #{id}
    </delete>

    <!--查询留言列表-->
    <select id="selectByCondition"
            resultType="com.uiotsoft.mainsite.module.productmessage.entity.ProductMessage">
        select
        id as id,
        site_id as siteId,
        site_name as siteName,
        customer_name as customerName,
        customer_phone as customerPhone,
        customer_email as customerEmail,
        product_want as productWant,
        create_time as createTime,
        customer_status as customerStatus,
        search_keyword as searchKeyword
        from mainsite_product_message
        <trim prefix="WHERE (" suffix=")" prefixOverrides="AND|OR">
            <if test="customerName != null and customerName != ''">
                customer_name like CONCAT('%', #{customerName}, '%')
            </if>
            <if test="customerPhone != null and customerPhone != ''">
                and customer_phone = #{customerPhone}
            </if>
            <if test="createTime != null">
                and create_time = #{createTime}
            </if>
            <if test="customerEmail != null and customerEmail != ''">
                and customer_email = #{customerEmail}
            </if>
            <if test="productWant != null and productWant != ''">
                and product_want = #{productWant}
            </if>
            <if test="siteId != null and siteId != '' and siteId != 1">
                and site_id = #{siteId}
            </if>
            <if test="siteName != null and siteName != ''">
                and site_name = #{siteName}
            </if>
            <if test="customerStatus != null and customerStatus != ''">
                and customer_status = #{customerStatus}
            </if>
            <if test="searchKeyword != null and searchKeyword != ''">
                and search_keyword = #{searchKeyword}
            </if>
        </trim>
        ORDER BY create_time DESC
    </select>

 <trim><choose><when></when><otherwise></otherwise></choose></trim>

二、select查询语句

<select id="selectByContentListBySiteIdAndCategoryIds"   resultType="java.util.Map">
        SELECT
        c.*,
        m.*,
        u.avatar avatar,
        u.username username
        FROM
        site_model_content c LEFT JOIN  site_model_${tableName}  m
        ON
        c.content_id = m.content_id
        LEFT JOIN
        t_sys_user u ON u.user_id = c.user_id
        <trim prefix="WHERE ("  suffix=")" prefixOverrides="AND|OR">
            c.status=1
            <if test="siteId!=null">
               AND  c.site_id = #{siteId}
            </if>
            <if test="categoryIds!=null">
               AND
                c.category_id in
                 <foreach item="item" index="index" collection="categoryIds" open="(" separator="," close=")">
                   #{item}
                 </foreach>
            </if>
            <if test='isRecommend!=null and isRecommend==1'>
                AND
                c.recommend = 1
            </if>
            <if test='whereParam!=null and valueParam!=null'>
                AND  m.${whereParam} = #{valueParam}
            </if>
            <if test='isPic!=null and isPic==1'>
                AND
                c.thumb!=''
            </if>
            <if test='isPic!=null and isPic==0'>
                AND
                c.thumb=''
            </if>
            <if test='siteType!=null'>
                AND
                c.site_type like CONCAT('%', #{siteType}, '%')
            </if>

        </trim>

        <choose>
            <when test="orderBy==1">
                order by c.content_id
            </when>
            <when test="orderBy==2">
                order by c.inputdate DESC
            </when>
            <when test="orderBy==3">
                order by c.inputdate
            </when>
            <when test="orderBy==4">
                order by c.updatedate DESC
            </when>
            <when test="orderBy==5">
                order by c.updatedate
            </when>
            <when test="isHot==1">
                order by c.view_num DESC
            </when>
            <otherwise>
                order by c.content_id DESC
            </otherwise>
        </choose>
    </select>

三、批量添加

<!-- 批量添加 -->
    <insert id="insertMore" useGeneratedKeys="true" keyProperty="siteId">
        INSERT INTO
            t_cms_site(
            site_name,
            `domain`,
            template,
            is_mobile,
            mobile_tpl,
            status
            )
            values
            <foreach item="item" collection="list" separator=",">
                (#{item.siteName},#{item.domain},#{item.template},
                #{item.isMobile},#{item.mobileTpl},#{item.status})
            </foreach>
    </insert>

四、与时间比较相关的

SELECT
        count(*)
        FROM
        crm_contract
        <where> isDel = 0
            <if test="departmentId != null and departmentId != null">
                and departmentId LIKE CONCAT(#{departmentId},'%')
            </if>
            <if test="directorAccount != null and directorAccount !='' ">
                and directorAccount = #{directorAccount}
            </if>
            <if test="executeState != null and executeState != ''">
                and executeState = #{executeState}
            </if>
            <if test="auditState != null and auditState != ''">
                and auditState = #{auditState}
            </if>
            <if test="contractType != null and contractType != ''">
                and contractType = #{contractType}
            </if>
            <if test="categoryId != null and categoryId != ''">
                and categoryId = #{categoryId}
            </if>
            <if test="customerName != null and customerName != ''">
                and customerName like CONCAT("%",#{customerName},"%")
            </if>
            <if test="director != null and director != ''">
                and director like CONCAT("%",#{director},"%")
            </if>
            <if test="signUser != null and signUser != ''">
                and signUser like CONCAT("%",#{signUser},"%")
            </if>
            <if test="startDate != null">
                <![CDATA[ and DATE_FORMAT(endDate,"%Y-%m-%d")  >= DATE_FORMAT(#{startDate},"%Y-%m-%d") ]]>
            </if>
            <if test="endDate != null">
                <![CDATA[ and DATE_FORMAT(endDate,"%Y-%m-%d")  <= DATE_FORMAT(#{endDate},"%Y-%m-%d")]]>
            </if>
        </where>
select
        c.*
        from crm_contract c left join crm_customer_info i on c.customerId =
        i.customerId
        <where>
            c.isDel = 0
            <if test="startDate != null and startDate != ''">
                and c.endDate >= #{startDate}
            </if>
            <if test="endDate != null and endDate != ''">
                and c.endDate <= #{endDate}
            </if>
            <if test="customerName != null and customerName != ''">
                and c.customerName like CONCAT(#{customerName},"%")
            </if>
            <if test="director != null and director != ''">
                and c.director like CONCAT(#{director},"%")
            </if>
            <if test="auditState != null and auditState != ''">
                and c.auditState = #{auditState}
            </if>
        </where>
        limit #{start},#{end}
<select id="selectAll" resultType="java.util.Map">
        SELECT
        log_id AS logId,
        page_name AS pageName,
        template_name AS templateName,
        publish_status AS publishStatus,
        consuming_time AS consumingTime,
        C.description,
        create_time AS createTime,
        C.site_id AS siteId,
        C.category_id AS categoryId,
        C.site_name as siteName,
        D.category_name as categoryName
        FROM
            (
                SELECT
                    log_id,
                    page_name,
                    template_name,
                    publish_status,
                    consuming_time,
                    A.description,
                    create_time,
                    A.site_id,
                    category_id,
                    site_name
                FROM
                    mainsite_publish_log A
                LEFT JOIN t_cms_site B ON A.site_id = B.site_id
            ) C
        LEFT JOIN t_cms_category D ON (C.category_id = D.category_id)
        <where>
            <if test="startTime != null">
                <![CDATA[ and DATE_FORMAT(create_time,"%Y-%m-%d %H:%i:%s")  >= DATE_FORMAT(#{startTime},"%Y-%m-%d %H:%i:%s") ]]>
            </if>
            <if test="endTime != null">
                <![CDATA[ and DATE_FORMAT(create_time,"%Y-%m-%d %H:%i:%s")  <= DATE_FORMAT(#{endTime},"%Y-%m-%d %H:%i:%s") ]]>
            </if>
        </where> order by log_id desc
    </select>

 

拓展:

Mysql动态SQL语句标签

 

 

 

完结!

标签:xml,customer,name,sql,site,status,SQL,id,CONCAT
From: https://blog.51cto.com/u_16128050/6342864

相关文章

  • MySQL学习基础篇Day9
    6.事务6.1事务简介事务是一组操作的集合,它是一个不可分割的工作单位,事务会把所有的操作作为一个整体一起向系统提交或撤销操作请求,即这些操作要么同时成功,要么同时失败。就比如:张三给李四转账1000块钱,张三银行账户的钱减少1000,而李四银行账户的钱要增加1000。这一组操......
  • 多种数据库注入复线-墨者学院SQL手工注入漏洞测试(Access数据库)
    SQL手工注入漏洞测试(Access数据库)1.判断注入点数字型报错,发现注入点开注2.用orderby获取列数在5时报错,判断为4列联合查询用递增的数字判断占位,发现无法回显题目给了是access数据库,access数据库的SQL手工注入,在联合语句显示可显字段时,必须要from表名因此,我们需要猜测表名3.......
  • 多种数据库注入复线-墨者学院SQL手工注入漏洞测试(Oracle数据库)
    SQL手工注入漏洞测试(Oracle数据库)1.判断注入点判断注入类型为数字型2.用orderby获取列数2时正常,3时报错,得到列数为23.联合查询获取占位符常规联合查询报错这是由于oracle数据库语法十分严格,在后面需要指定数据表和准确的字符类型我们使用dual表,dual是oracle中的一个实际存......
  • SQL高级语法学习总结(二)
    SQL高级语法学习总结(一)。现在我们接着说sql的高级用法。SQLCREATEDATABASE语法CREATEDATABASEdbname;CREATEDATABASE语句用于创建数据库。 SQLCREATETABLE语法CREATETABLEtable_name(column_name1data_type(size),column_name2data_type(size),column_name3dat......
  • SQL高级语法学习总结(一)
    基础语法呢,就是简单的对行列进行增删改。SQL基础语法学习总结,高级用法无非是条件更多,能实现的需求更多,其中涉及到非常多的关键字,本篇博客就进行一下总结。本文所有用法均在mysql环境下测试通过。其他数据库可能某些关键字会有不同。SQLSELECTLIMIT子句 SELECTLIMIT子句用于规......
  • mysqld_multi实现单机主从复制
    文档课题:mysqld_multi实现单机主从复制.数据库:mysql5.7.18系统:rhel7.3安装包:mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz1、理论知识mysqld_multi用于在一台服务器上管理多个mysqld进程,这些进程使用不同的socket文件并监听不同端口.mysqld_multi可以批量启动、关闭、或报告m......
  • MySQL数据基础知识整理—5
        今天是MySQL数据库基础知识整理的最后一章,本次我们要整理的是隔离性和MVCC多版本并发控制技术。隔离性    在我们进行多事务的处理时,难免会出现多个事务由于不当的操作导致事务发生不可逆转的错误结果。因此,MySQLS数据库为了防止这样的事情发生,使多个并发事物之......
  • Cause: java.sql.SQLException: Invalid value for getInt()
    Cause:java.sql.SQLException:InvalidvalueforgetInt()-'锛?';SQL[];InvalidvalueforgetInt()-'锛';nestedexceptionisjava.sql.SQLException:InvalidvalueforgetInt()-'锛?' 在对错误进行排查时,有以下原因可能导致的原因:出现此问题的原因是因为MyB......
  • 【IntelliJ IDEA】idea中的插件之一:Free Mybatis plugin跳转插件的使用(方便在Dao接口
    本文目录一、安装二、使用最近在使用一个非常好用的跳转插件,用着很顺手,效率比之前提高了很多。之前使用MyBatis框架或者是在IDEA中,发现Mapper接口和XML文件之间跳转十分的麻烦,我之前经常的操作是在Mapper接口中将接口名称复制一下,然后去查找对应的XML文件,打开后CRTL+F查找对应的xml......
  • 多种数据库注入复线-墨者学院Sql Server数据库手工注入
    SQL手工注入漏洞测试(SqlServer数据库)1.测试注入点点击公告报错,存在数字型的sql注入开注2.orderby查询列数2正常回显,3报错,但是4又正常,5以上报错3.联合查询判断占位但是报错union用于合并两个或多个select语句的结果集,并去除表中任何重复行且union内部的s......