mybatis中SQL语句运用总结

union 连接查询  连接两个表后会过滤掉重复的值

<resultMap id="BaseResultMap" type="com.sprucetec.pay.etl.model.BillDetail">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="pay_order_no" jdbcType="VARCHAR" property="payOrderNo"/>
        <result column="pay_channel_id" jdbcType="TINYINT" property="payChannelId"/>
        <result column="pay_amount" jdbcType="INTEGER" property="payAmount"/>
        <result column="trans_date" jdbcType="INTEGER" property="transDate"/>
        <result column="trans_type" jdbcType="VARCHAR" property="transType"/>
        <result column="error_type" jdbcType="VARCHAR" property="errorType"/>
        <result column="is_check" jdbcType="TINYINT" property="isCheck"/>
    </resultMap>

  <sql id="condition">
  <if test="transType != null">
    and trans_type = #{transType,jdbcType=TINYINT}
  </if>
  <if test="payChannelId != null">
    and pay_channel_id = #{payChannelId,jdbcType=TINYINT}
  </if>

 </sql>

<select id="queryList" parameterType="com.pay.BillCheckQuery" resultMap="BaseResultMap">
       select a.pay_order_no,a.trans_date,a.pay_amount,a.pay_channel_id,a.trans_type,a.error_type
       from (select pay_order_no,trans_date,pay_amount,pay_channel_id,trans_type,"无结果" as error_type
       from t_pay_core_order
       where 1 = 1 <include refid="condition"/>
       union 
       select pay_order_no,trans_date,pay_amount,pay_channel_id,trans_type,"缺失" as error_type
       from t_pay_bill_file_detail 
       where 1 = 1 <include refid="condition"/>
       ) as a
       order by a.trans_date desc
       limit #{pageSize} offset #{startRecord}
    </select>

union all 才可以将所有的值都查询出来,自己将所有的值查询完总是少,才发现是这个问题

<select id="queryCountAndSum" parameterType="com.BillCheckQuery" resultMap="BaseResultMap">
       select sum(a.pay_amount) as trans_amount,count(1) as trans_num from 
       (select pay_amount from t_pay_core_order
       where 
        pay_channel_id = #{payChannelId,jdbcType=SMALLINT}and trans_date &gt;= #{startTime,jdbcType=INTEGER}
        and trans_date &lt;= #{endTime,jdbcType=INTEGER}union all
       select pay_amount from t_pay_bill_file_detail 
       where 
        pay_channel_id = #{payChannelId,jdbcType=SMALLINT}and trans_date &gt;= #{startTime,jdbcType=INTEGER}
        and trans_date &lt;= #{endTime,jdbcType=INTEGER}
       ) as a
    </select>

传入对象中有list需要使用时,需要进行遍历,我在in语句中使用

<update id="updateByNum" parameterType="com.query.BillCheckQuery">
         update t_pay_core_order t1,t_pay_bill_file_detail t2
           set t1.is_check = 1,t2.is_check = 1
        WHERE 
        t1.pay_order_no = t2.pay_order_no
        and t2.pay_order_no in
        <foreach item="payOrderNo" index="index" collection="orderlist" open="(" separator="," close=")">
                #{payOrderNo,jdbcType=VARCHAR}
        </foreach>
        and t1.trans_date &gt;= #{startTime,jdbcType=INTEGER} 
        and t1.trans_date &lt;= #{endTime,jdbcType=INTEGER}</update>

或者直接在list中储存对象也可以遍历取出值

<insert id="batchInsert" parameterType="java.util.List" >
        insert into t_pay_bill_file_detail (file_id,pay_order_no,third_trade_no,trans_type,
        pay_channel_id,pay_amount,trans_date)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.payOrderNo},
            #{item.transType},
            #{item.transDate}
            )
        </foreach>
    </insert>

 ps:关注一下本人公众号,每周都有新更新哦!

相关推荐

  1. MyBatis动态SQL语句

    2024-06-15 17:44:03       43 阅读
  2. Mybatis 动态Sql标签使用总结

    2024-06-15 17:44:03       11 阅读
  3. MYSQL 二、SQL语句总结

    2024-06-15 17:44:03       14 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-15 17:44:03       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-15 17:44:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-15 17:44:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-15 17:44:03       20 阅读

热门阅读

  1. Redis(基础篇)

    2024-06-15 17:44:03       8 阅读
  2. 无回显XXE攻击:隐秘的数据泄露技术

    2024-06-15 17:44:03       10 阅读
  3. 深度解析:基于C++的CNN图像检索实现

    2024-06-15 17:44:03       10 阅读
  4. CSP 第34次认证第四题 货物调度

    2024-06-15 17:44:03       9 阅读
  5. 关于编程思想

    2024-06-15 17:44:03       9 阅读
  6. 数列求和、统计输入正数个数 题目

    2024-06-15 17:44:03       9 阅读
  7. 查看队列资源限额和使用情况

    2024-06-15 17:44:03       9 阅读
  8. 一些常见的显示接口

    2024-06-15 17:44:03       12 阅读