码迷,mamicode.com
首页 > 数据库 > 详细

Example查询SQL之or查询and查询

时间:2020-02-12 12:57:49      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:tno   env   用户id   写法   需要   eva   value   bis   between   

  • 问题描述

通过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>

 

Example查询SQL之or查询and查询

标签:tno   env   用户id   写法   需要   eva   value   bis   between   

原文地址:https://www.cnblogs.com/cxxiao/p/12298414.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!