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 >= #{startTime,jdbcType=INTEGER}
and trans_date <= #{endTime,jdbcType=INTEGER}union all
select pay_amount from t_pay_bill_file_detail
where
pay_channel_id = #{payChannelId,jdbcType=SMALLINT}and trans_date >= #{startTime,jdbcType=INTEGER}
and trans_date <= #{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 >= #{startTime,jdbcType=INTEGER}
and t1.trans_date <= #{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语句运用总结的更多相关文章
- mybatis中sql语句传入多个参数方法
1 使用map <select id="selectRole" parameterType="map" resultType="RoleMap& ...
- MyBatis中sql语句
一.select <!-- 查询学生,根据id --> <select id="getStudent" parameterType="String&qu ...
- mybatis中sql语句必须用${}而不能不用#{}的情况
在mybatis中如果我们使用#{}的方式编写的sql时,#{} 对应的变量自动加上单引号 ' ' 例如: select * from #{param} 当我们给参数传入值为user时,他的sql是这 ...
- Mybatis 中 sql 语句的占位符 #{} 和 ${}
#{} 表示一个占位符号,通过 #{} 可以实现 preparedStatement 向占位符中设置值,自动进行 java 类型和 jdbc 类型转换.#{} 可以有效防止 sql注入. #{} ...
- Mybatis中sql语句中的in查询,一定要判断null的情况
不严谨的写法,可能会报错:in (),这种情况不符合mysql的语法. select from loanwhere LOAN_ID in <foreach item="item&quo ...
- mybatis中sql语句查询操作
动态sql where if where可以自动处理第一个and. <!-- 根据id查询用户信息 --> <!-- public User findUserById(int id) ...
- Mybatis中sql语句中的in查询,判断null和size为0的情况
不严谨的写法,可能会报错:in (),这种情况不符合SQL的语法,导致程序报错. 如果简单只做非空判断,这样也有可能会有问题:本来in一个空列表,应该是没有数据才对,却变成了获取全部数据! 所以一个比 ...
- mybatis 中sql语句传递多个参数
Available parameters are [2, 1, 0, param1, param2, param3] <select id="loginByTeacher" ...
- mybatis中sql语句的批量插入
<!-- 收件箱插入收件信息 --> <insert id="insertReceiveemail"> <!-- 生成一条U ...
随机推荐
- Python中的基础数据类型
Python中基础数据类型 1.数字 整型a=12或者a=int(2),本质上各种数据类型都可看成是类,声明一个变量时候则是在实例化一个类. 整型具备的功能: class int(object): & ...
- 比较和排序(IComparable和IComparer以及它们的泛型实现)(转)
C#笔记25:比较和排序(IComparable和IComparer以及它们的泛型实现) 本文摘要: 1:比较和排序的概念: 2:IComparable和IComparer: 3:IComparabl ...
- Apache的主要目录和配置文件详解
一.Apache 主要配置文件注释Apache的主配置文件:/etc/httpd/conf/httpd.conf默认站点主目录:/var/www/html/Apache服务器的配置信息全部存储在主配置 ...
- 使用 docker-machine 管理 Azure 容器虚拟机
安装 docker-machine 请参见该链接(https://docs.docker.com/machine/install-machine "https://docs.docker.c ...
- .net core系列之《将.net core应用部署到Ubuntu》
1.首先准备一个演示项目. 2.然后将这个项目用FileZilla工具上传到Ubuntu中. 3.进入目标文件,接下来有两种方法来部署项目 a.用dotnet run命令 root@hhz-virtu ...
- C/C++面试问题分类大汇总 ZZ 【C++】
http://www.mianwww.com/html/2014/05/21208.html 指针和引用的区别 指针指向一块内存,它的内容是指向内存的地址:引用是某内存的别名 引用使用是无需解引用,指 ...
- 解决WPF图片模糊最佳方法
对于图片显示模糊相信很多人都遇到过,其实,有些图片会因为垂直分辨率/水平分辨率不同而造成在WPF界面上显示出现模糊. WPF默认是96Dpi,但有些图片可能是72DPI甚至更低或更高,这样就会出现图片 ...
- Python初学者第十二天 购物车程序小作业
12day 作业题目: 购物车程序 作业需求: 数据结构: goods = [ {"name": "电脑", "price": 1999}, ...
- Python学习---Django的request扩展[获取用户设备信息]
关于Django的request扩展[获取用户设备信息] settings.py INSTALLED_APPS = [ ... 'app01', # 注册app ] STATICFILES_DIRS ...
- Java学习---InetAddress类的学习
基础知识 1.InetAddress类 在网络API套接字,InetAddress类和它的子类型对象使用域名DNS系统,处理主机名到主机IPv4或IPv6地址的转换.如图1-1所示. 由于InetAd ...