mybatis集合查询

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper PUBLIC    
"-//mybatis.org//DTD Mapper 3.0//EN"    
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.neusoft.searchservice.xztwopublic.model.CountColumn">


<select id="getCommitOrSaveCount" parameterType="map" resultType="countColumn">
<!--行政许可提交 -->
select count(0) as Count from t_xzxk t where commitorsave='1' and isremove='0'
<if test="countColumn.startDate != null ">
AND TO_CHAR(SJC, 'yyyy-MM-dd')<![CDATA[ >= ]]> TO_CHAR(#{countColumn.startDate}, 'yyyy-MM-dd')
</if>
<if test="countColumn.endDate != null ">
AND TO_CHAR(SJC, 'yyyy-MM-dd')<![CDATA[ <= ]]> TO_CHAR(#{countColumn.endDate}, 'yyyy-MM-dd')
</if>
<if test="dpIdsArray != null">
AND T.DEPARTID IN 
<foreach collection="dpIdsArray" item="dpids" open="(" separator="," close=")">
#{dpids}   
    </foreach>
   </if>
   <!--行政许可保存  -->
union all 
   select count(0) as Count from t_xzxk t where commitorsave='0' and isremove='0'
<if test="countColumn.startDate != null ">
AND TO_CHAR(SJC, 'yyyy-MM-dd')<![CDATA[ >= ]]> TO_CHAR(#{countColumn.startDate}, 'yyyy-MM-dd')
</if>
<if test="countColumn.endDate != null ">
AND TO_CHAR(SJC, 'yyyy-MM-dd')<![CDATA[ <= ]]> TO_CHAR(#{countColumn.endDate}, 'yyyy-MM-dd')
</if>
<if test="dpIdsArray != null">
AND T.DEPARTID IN 
<foreach collection="dpIdsArray" item="dpids" open="(" separator="," close=")">
#{dpids}   
    </foreach>
   </if>
   <!-- 行政处罚提交 -->
union all 
select count(0) as Count from t_xzcf  where commitorsave='1' and isremove='0'
<if test="countColumn.startDate != null ">
AND TO_CHAR(SJC, 'yyyy-MM-dd')<![CDATA[ >= ]]> TO_CHAR(#{countColumn.startDate}, 'yyyy-MM-dd')
</if>
<if test="countColumn.endDate != null ">
AND TO_CHAR(SJC, 'yyyy-MM-dd')<![CDATA[ <= ]]> TO_CHAR(#{countColumn.endDate}, 'yyyy-MM-dd')
</if>
<if test="dpIdsArray != null">
AND DEPARTID IN 
<foreach collection="dpIdsArray" item="dpids" open="(" separator="," close=")">
#{dpids}   
    </foreach>
   </if>
    <!-- 行政处罚保存 -->
union all 
select count(0) as Count from t_xzcf  where commitorsave='0' and isremove='0'
<if test="countColumn.startDate != null ">
AND TO_CHAR(SJC, 'yyyy-MM-dd')<![CDATA[ >= ]]> TO_CHAR(#{countColumn.startDate}, 'yyyy-MM-dd')
</if>
<if test="countColumn.endDate != null ">
AND TO_CHAR(SJC, 'yyyy-MM-dd')<![CDATA[ <= ]]> TO_CHAR(#{countColumn.endDate}, 'yyyy-MM-dd')
</if>
<if test="dpIdsArray != null">
AND DEPARTID IN 
<foreach collection="dpIdsArray" item="dpids" open="(" separator="," close=")">
#{dpids}   
    </foreach>
   </if>
</select>

</mapper>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis 中的嵌套查询是指在一个 SQL 查询语句中嵌套执行另一个 SQL 查询语句。这使得我们可以在一个查询中获取更复杂的数据结果。 在 MyBatis 中,可以通过两种方式实现嵌套查询:使用嵌套结果映射和使用嵌套查询语句。 1. 嵌套结果映射: - 在主查询的结果映射中,使用 `<association>` 或 `<collection>` 元素嵌套定义关联对象或集合对象。 - 在关联对象或集合对象的结果映射中,使用 `<select>` 元素定义需要执行的嵌套查询语句。 - 使用 `select` 属性指定需要执行的嵌套查询语句的 ID。 - 在主查询中,使用 `resultMap` 属性指定主查询结果集的映射。 下面是一个示例,展示如何在 MyBatis 中进行嵌套查询: ```xml <resultMap id="userResultMap" type="User"> <id property="id" column="id"/> <result property="username" column="username"/> <association property="profile" resultMap="profileResultMap"/> </resultMap> <resultMap id="profileResultMap" type="Profile"> <id property="id" column="id"/> <result property="email" column="email"/> </resultMap> <select id="getUserWithProfile" resultMap="userResultMap"> SELECT u.id, u.username, p.id, p.email FROM user u INNER JOIN profile p ON u.profile_id = p.id </select> ``` 2. 嵌套查询语句: - 在主查询的 SQL 语句中使用`select` 关键字执行嵌套查询。 - 使用 `<foreach>` 元素构建需要传递给嵌套查询的参数。 - 在嵌套查询中,使用 `#{}` 占位符引用传递的参数。 下面是一个示例,展示如何在 MyBatis 中使用嵌套查询语句: ```xml <select id="getUserWithOrders" resultMap="userResultMap"> SELECT u.id, u.username, (SELECT COUNT(*) FROM orders o WHERE o.user_id = u.id) AS order_count FROM user u </select> ``` 这就是 MyBatis 中嵌套查询的使用方法。你可以根据具体的业务需求选择适合的方式来实现嵌套查询功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值