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:关注一下本人公众号,每周都有新更新哦!

mybatis中SQL语句运用总结的更多相关文章

  1. mybatis中sql语句传入多个参数方法

    1 使用map <select id="selectRole" parameterType="map" resultType="RoleMap& ...

  2. MyBatis中sql语句

    一.select <!-- 查询学生,根据id --> <select id="getStudent" parameterType="String&qu ...

  3. mybatis中sql语句必须用${}而不能不用#{}的情况

    在mybatis中如果我们使用#{}的方式编写的sql时,#{} 对应的变量自动加上单引号 ' ' 例如: select * from #{param} 当我们给参数传入值为user时,他的sql是这 ...

  4. Mybatis 中 sql 语句的占位符 #{} 和 ${}

    #{} 表示一个占位符号,通过 #{} 可以实现 preparedStatement 向占位符中设置值,自动进行 java 类型和 jdbc 类型转换.#{} 可以有效防止   sql注入. #{}  ...

  5. Mybatis中sql语句中的in查询,一定要判断null的情况

    不严谨的写法,可能会报错:in (),这种情况不符合mysql的语法. select from loanwhere LOAN_ID in <foreach item="item&quo ...

  6. mybatis中sql语句查询操作

    动态sql where if where可以自动处理第一个and. <!-- 根据id查询用户信息 --> <!-- public User findUserById(int id) ...

  7. Mybatis中sql语句中的in查询,判断null和size为0的情况

    不严谨的写法,可能会报错:in (),这种情况不符合SQL的语法,导致程序报错. 如果简单只做非空判断,这样也有可能会有问题:本来in一个空列表,应该是没有数据才对,却变成了获取全部数据! 所以一个比 ...

  8. mybatis 中sql语句传递多个参数

    Available parameters are [2, 1, 0, param1, param2, param3] <select id="loginByTeacher"  ...

  9. mybatis中sql语句的批量插入

    <!-- 收件箱插入收件信息 -->    <insert id="insertReceiveemail">           <!-- 生成一条U ...

随机推荐

  1. 网络爬虫入门(二)模拟提交以及HttpClient修正

    模拟提交就是说我们不自己登陆到客户端,仅仅靠发送请求就模拟了客户端的操作,在现实使用的时候经常用来接收一些需要登录才能获取到的数据,来模拟表单的提交,所以很多时候也被称作虚拟登录,这次的例子是我自己为 ...

  2. SQL Server 2014 虚拟机的自动备份 (Resource Manager)

    自动备份将在运行 SQL Server 2014 Standard 或 Enterprise 的 Azure VM 上自动为所有现有数据库和新数据库配置托管备份到 Azure. 这样,便可以配置使用持 ...

  3. Azure 订阅和服务限制、配额和约束

    最后更新时间:2016年10月24日 概述 本文档指定一些最常见的 Azure 限制.请注意,本文档目前未涵盖所有 Azure 服务.一段时间后,将展开并更新这些限制以包含多个平台. NOTE: 如果 ...

  4. C#的抽象类和接口的区别,在什么时候使用才合适?

    理解抽象类  abstract class和interface在c#语言中都是用来进行抽象类(本文 中的抽象类并非从abstract class翻译而来,它表示的是一个抽象体,而abstract cl ...

  5. boost编译

    从boost官网( http://www.boost.org )上下载最新的boost版本,现在最新是1.64版本,解压到自定义目录(我解压到了E盘E:\boost_1_64_0\,最终的目录结构是E ...

  6. 基础知识:Promise(整理)

    基础知识:Promise(整理) (来自牛客网)下面关于promise的说法中,错误的是(D) A. resolve和reject都是直接生成一个进入相应状态的promise对象,其参数就是进入相应状 ...

  7. java list.remove移除失败

    1. resultList.remove(i) 移除失败说明 当i为Integer类型时,通过观察源码发现当找不到该类型时就会自动去找Object类型,即remove(object),因为集合中不存在 ...

  8. gluoncv faster_rcnn 参数修改

    https://github.com/dmlc/gluon-cv/blob/master/gluoncv/model_zoo/faster_rcnn/faster_rcnn.py 对你选用的模块,修改 ...

  9. 如何使用Loadrunner12录制WebSocket脚本

    简单说一下,关于Loadrunner12对WebSocket的支持可以参照以下文档: http://community.hpe.com/t5/LoadRunner-and-Performance/As ...

  10. 1、Spring Cloud - 微服务简介

    前言: 业界大牛马丁.福勒(Martin Fowler) 这样描述微服务: 论文网址:https://martinfowler.com/articles/microservices.html 就目前而 ...