• Example查询SQL之or查询and查询


    • 问题描述

    通过Example实现如下SQL语句功能

    SELECT T.* FROM DAA_ORGINFO T WHERE T.DAA_ZTNO='参数' AND T.ID = '参数' OR T.DAA_ID_SJ='参数'



    因为Example‘没有直接写or的方法,所以需要通过and来构造改SQL语句,首先我们分析一下,进行改造如下:

    SELECT T.* FROM DAA_ORGINFO T WHERE (T.DAA_ZTNO='参数' AND T.ID = '参数') OR (T.DAA_ZTNO='参数' AND T.DAA_ID_SJ='参数')
    • 解决方法

    实现方式如下:

       //1.根据订单ID查出用户ID
                Example example=new Example(TOrderOrder.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("id",orderId)
                         .andGreaterThan("mark",0)
                        .andEqualTo("type",1)
                        .andEqualTo("isReturn",0);
                Example.Criteria criteria2 = example.createCriteria();
                criteria.andEqualTo("id",orderId)
                        .andGreaterThan("mark",0)
                        .andEqualTo("type",2)
                        .andEqualTo("isReturn",0);
                example.or(criteria2);
                List<TOrderOrder> orderInfolist= tOrderOrderDao.getListByExample(example);
    • xml中的SQL写法,因项目问题,example重新构造,可参考如下:
     1 <select id="getDataGridByExample" resultType="com.ctg.pbis.partymanage.daaorginfo_x.entity.DaaOrginfo">
     2 SELECT T.*, GET_NAME_BYDYID(T.DAB_ID_LXR) DAB_ID_LXR_NAME, GET_NAME_BYDYID(T.DAB_ID_ZZSJ) DAB_ID_ZZSJ_NAME
     3 FROM DAA_ORGINFO T
     4 WHERE 1=1
     5 <if test="example != null">
     6 <trim suffix="" suffixOverrides="and">
     7 <trim suffix="" suffixOverrides="()">
     8 AND
     9 <trim prefix="(" suffix=")">
    10 <foreach collection="example.oredCriteria" item="criteria" separator="or">
    11 <if test="criteria.valid">
    12 <trim prefix="(" suffix=")" prefixOverrides="and">
    13 <foreach collection="criteria.criteria" item="criterion">
    14 <choose>
    15 <when test="criterion.noValue">
    16 and ${criterion.condition}
    17 </when>
    18 <when test="criterion.singleValue">
    19 and ${criterion.condition} #{criterion.value}
    20 </when>
    21 <when test="criterion.betweenValue">
    22 and ${criterion.condition} #{criterion.value} and
    23 #{criterion.secondValue}
    24 </when>
    25 <when test="criterion.listValue">
    26 and ${criterion.condition}
    27 <foreach collection="criterion.value" item="listItem" open="(" close=")"
    28 separator=",">
    29 #{listItem}
    30 </foreach>
    31 </when>
    32 </choose>
    33 </foreach>
    34 </trim>
    35 
    36 </if>
    37 </foreach>
    38 </trim>
    39 </trim>
    40 </trim>
    41 </if>
    42 
    43 <if test="example.orderByClause != null">
    44 order by ${example.orderByClause}
    45 </if>
    46 
    47 </select>
  • 相关阅读:
    vue 手动挂载 $amount()
    Redis 主从配置
    DMA分区管理
    C# 构造函数里的base和this的区别
    SQL Server 数据库性能优化
    TCP和UDP的优缺点及区别
    Django框架初步应用简述
    前端vue框架应用雏形
    接口mock之moco
    python进阶(九)~~~协程、进程池、线程/进程/协程对比
  • 原文地址:https://www.cnblogs.com/cxxiao/p/12298414.html
Copyright © 2020-2023  润新知