经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库运维 » MySQL » 查看文章
mybatis中SQL语句运用总结
来源:cnblogs  作者:努力的小雨  时间:2018/9/25 19:49:44  对本文有异议

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

  1. <resultMap id="BaseResultMap" type="com.sprucetec.pay.etl.model.BillDetail">
  2. <id column="id" jdbcType="INTEGER" property="id"/>
  3. <result column="pay_order_no" jdbcType="VARCHAR" property="payOrderNo"/>
  4. <result column="pay_channel_id" jdbcType="TINYINT" property="payChannelId"/>
  5. <result column="pay_amount" jdbcType="INTEGER" property="payAmount"/>
  6. <result column="trans_date" jdbcType="INTEGER" property="transDate"/>
  7. <result column="trans_type" jdbcType="VARCHAR" property="transType"/>
  8. <result column="error_type" jdbcType="VARCHAR" property="errorType"/>
  9. <result column="is_check" jdbcType="TINYINT" property="isCheck"/>
  10. </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>

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

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

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

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

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

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

  1. <insert id="batchInsert" parameterType="java.util.List" >
  2. insert into t_pay_bill_file_detail (file_id,pay_order_no,third_trade_no,trans_type,
  3. pay_channel_id,pay_amount,trans_date)
  4. values
  5. <foreach collection="list" item="item" index="index" separator=",">
  6. (
  7. #{item.payOrderNo},
  8. #{item.transType},
  9. #{item.transDate}
  10. )
  11. </foreach>
  12. </insert>

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号