Mybatis的oracle的经典SQL语句(分页-新增的id自增-批量新增-动态修改-union all)

本文介绍了使用MyBatis操作Oracle数据库的一些经典SQL技巧,包括分页查询、ID自增长插入记录、批量插入数据、动态更新及联合查询等实用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mybatis的oracle的经典SQL语句(分页-新增的id自增-批量新增-动态修改-union all)
分页:
 
  <!-- 获取维权信息 -->
    <select id="getAdviceList" resultMap="adviceRVO">
        select *
          from (select  t.*,rownum rn
                  from     (select id,
                                trueName,
                                createDate
                          from t_sp_advice
                         where delflag = '0' and STORE_ID=#{store_id}
                          <if test="status != null and status != ''">
                              and status = #{status}
                          </if>
                          <if test="content !=null and content != ''">
                                and content like '%'||#{content}||'%'
                          </if>
                          <if test="startDate != null and startDate != ''">
                                and createdate >= to_date(#{startDate},'yyyy-MM-dd')
                          </if>
                          <if test="endDate != null and endDate != ''">
                                and createdate <= to_date(#{endDate},'yyyy-MM-dd')
                          </if>
                         order by status,createdate desc) t
                where rownum <=#{page}*#{count}) t1
         where t1.rn >(#{page}-1)*#{count}
    </select>


新增的id自增
  
 <!-- 新增轮播图信息 -->
    <insert id="addBanner" parameterMap="bannerDto">
        <selectKey keyProperty="id" resultType="int" order="BEFORE">
            SELECT
            SEQ_T_SP_BANNER.NEXTVAL id FROM DUAL
        </selectKey>
        INSERT INTO T_SP_BANNER(
        id,
        banner_name,
        createdate,
        )
        VALUES(
        #{id},
        #{banner_name},
        sysdate
    </insert>


批量新增:
 
  <!-- 批量新增热门目的地 -->
    <insert id="addDestination" parameterType="java.util.List">
        INSERT INTO t_sp_hot_county t
                (t.id,
                t.county_name,
                t.createDate
                )
        SELECT seq_t_sp_hot_county.NEXTVAL id, A.*
        FROM(
        <foreach collection="list" item="item" index="index"
            separator="UNION ALL">
            SELECT
            #{item.county_name},
            sysdate createDate
            FROM dual
        </foreach>
        )A
    </insert>


修改动态sql:
 
  <!-- 修改密码或者角色-->
    <update id="updateManager">
        update T_SP_SYSUSER
        <set>
        <if test="password != null and password != ''">
        password=#{password},
        </if>
        <if test="role_id != null and role_id != ''">
        role_id=#{role_id}
        </if>
        </set>
        where id=#{id}
        and delflag = '0'
    </update>


union all连接三张不同表格不同搜索条件获取三行相同类型的数据
 
   <!-- 获取总金额 -->
    <select id="getAllData" resultMap="orderSumRVO">
        select '景点' tableName, sum(p.totalprice) totalPrice
        from t_poi_orderrecord p
        where  p.delflag = '0'
        and  p.sp_store_id=#{store_id}
        and  p.orderdate  >= to_date(#{startDate},'yyyy-mm-dd')
        and  p.poiType=1
           union all
           select '酒店' tableName, sum(p.totalprice) totalPrice
        from t_poi_orderrecord p
        where  p.delflag = '0'
        and  p.sp_store_id=#{store_id}
        and  p.orderdate  >= to_date(#{startDate},'yyyy-mm-dd')
        and  p.poiType=5
           union all
           select '线路' tableName,sum(f.totalprice) totalPrice
        from t_f1_order f
        where  f.delflag = '0'
        and  f.sp_store_id=#{store_id}
        and  f.createdate  >= to_date(#{startDate},'yyyy-mm-dd')
    </select>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值