[置顶] SPL讲解(6)--Condition篇
SmartPersistenceLayer 2.0 之 Condition篇
原理
强大的Condition功能是SPL的一个特性,可以使用Condition完成绝大部分的条件定义,使用也是最广泛的,如RetrieveCriteria,UpdateCriteria,DeleteCriteria,Query等,只要用到查询条件的地方,都会使用此Condition类.
Condition的实例化方式:
RetrieveCriteriarc=new RetrieveCriteria(typeof(StudentEntity));
Conditionc=rc.GetNewCondition();
或
UpdateCriteria uc=newUpdateCriteria (typeof(StudentEntity));
Conditionc=uc.GetNewCondition();
或
DeleteCriteria dc=newDeleteCriteria (typeof(StudentEntity));
Conditionc=dc.GetNewCondition();
或
Query q=newQuery(typeof(StudentEntity));
Condition c=q.GetQueryCondition();
Condition内部是and关系,Condition之间,是以”Or”的方式组合的
如:
RetrieveCriteriarc=new RetrieveCriteria(typeof(StudentEntity));
Conditionc=rc.GetNewCondition();
c.AddEqualTo(StudentEntity.Name,”tintown”);
c.AddNotEqualTo(StudentEntity.Sex,’男’);
Conditionc2=rc.GetNewCondition();
c2.AddEqualTo(StudentEntity.Id,”1”);
这样会产生”(Name=’tintown’ andSex<>’男’) or Id=1”这样的结果.
常用条件
下面以RetrieveCriteriarc=new RetrieveCriteria(typeof(StudentEntity));
Condition c=rc.GetNewCondition();
AddEqualTo (=)
相等比较:会生成类似”where name=’tintown’”的语句, 例如:
c.AddEqualTo(“Name”,”tintown”);
AddGreaterThan(>)
大于比较:会生成类似” where price > 1000”的语句
c.AddGreaterThan(“Price”,1000);
AddGreaterThanOrEqualTo(>=)
大于等于比较: 会生成类似” where price >= 1000”的语句
c.AddGrearterThanOrEqualTo(“Price”,1000);
AddNotEqualTo(<>)
不等于比较: 会生成类似” where name<>’tintown’”的语句
c.AddNotEqualTo(“Name”,”tintown”);
AddLessThan(<)
小于比较: 会生成类似” where price < 1000”的语句
c.AddLessThan(“Price”,1000);
AddLessThanOrEqualTo(<=)
小于等于比较: 会生成类似” where price <= 1000”的语句
c.AddLessThanOrEqualTo(“Price”,1000);
AddMatch(like ‘%A%’)
匹配比较: 会生成类似” where name like ‘%刘%’”的语句
c.AddMatch(“Name”,”刘”);
AddMatchPrefix(like ‘A%’)
前匹配比较: 会生成类似” where name like ‘刘%”的语句
c.AddMatchPrefix(“Name”,”刘”);
AddIn(in (A))
IN比较: 会生成类似” where name in (‘K’,’ I’)”的语句
string[] where=new string[]{“K”,”I”}
c.AddIn(“Name”,where);
OrGroup条件类[2.0新增功能]
在常规中,我们注意到,很难实现A and B and ( C or D)这样的效果,所以在这里添加了OrGroup类。
OrGroup是指Or的组,在OrGroup内,关系都是Or,所以可以通过以下代码要实现:
RetrieveCriteria rc=new RetrieveCriteria(typeof(StudentEntity));
Condition c=rc.GetNewCondition();
c.AddEqualTo(….) //条件A
c.AddEqualTo(….) //条件B
OrGroup og=rc. GetNewOrGroup(); //实例一个OrGroup
og.AddEqualTo(…); //条件C
og.AddEqualTo(…); //条件 D
C和D形成一个Or组,这样就实现了A and B and ( C or D)
通过以上方式可以实现更强的条件定义
字段与字段比较[2.0新增功能]
为了字段与字段的比较,为此扩展了查询条件,
字段间相等AddEqualToField
c.AddEqualToField(“field1”,field2);
这会生成类似”where field1=field2”
字段间大于AddGreaterThanField
c. AddGreaterThanField (“field1”,field2);
这会生成类似”where field1>field2”
字段间大于等于AddGreaterThanOrEqualToField
c. AddGreaterThanOrEqualToField (“field1”,field2);
这会生成类似”where field1>=field2”
字段间不等于AddNotEqualToField
c. AddNotEqualToField (“field1”,field2);
这会生成类似”where field1<>field2”
字段间小于AddLessThanField
c. AddLessThanField (“field1”,field2);
这会生成类似”where field1<field2”
字段间小于等于AddLessThanOrEqualToField
c. AddLessThanOrEqualToField (“field1”,field2);
这会生成类似”where field1<=field2”
总结
SPL虽然提供了很强的条件定义功能,如果遇到特别复杂的条件,还是需要自己手写SQL语句进行查询的。
[置顶] SPL讲解(6)--Condition篇的更多相关文章
- [置顶] SPL讲解(4)--Criteria操作篇
概念 以前一篇文章中,描述了实体Entity的操作,很明显,仅仅实体的操作是远远不够的.如:我们经常会根据查询条件从数据库中获取记录集并绑定到DataGrid上,会根据条件进行批量的Update和De ...
- [置顶] SPL讲解(7)--Query高级篇
SmartPersistenceLayer 2.0 之Query高级查询篇 总述 在看了前面的功能后,大家都会考虑到多表之间的查询怎么办.在这里,我想先讲一下查询在应用系统中的复杂性/重要性/可行性. ...
- [知了堂学习笔记]_css3特效第二篇--行走的线条&&置顶导航栏
一.行走的线条. 效果图(加载可能会慢一点儿,请稍等...): html代码: <div class="movingLines"> <img src=" ...
- css3特效第二篇--行走的线条&&置顶导航栏
一.行走的线条. 效果图(加载可能会慢一点儿,请稍等...): html代码: <div class="movingLines"> <img src=" ...
- 让WPF的Popup不总置顶的解决方案
使用WPF的Popup的时候会发现有一个问题,它总是会置顶,只要Popup的StayOpen不设置为False,它就一直呆在最顶端,挡住其他的窗口. 解决方案是继承Popup重新定义控件PopupEx ...
- [置顶] 创建GitHub技术博客全攻略
[置顶] 创建GitHub技术博客全攻略 分类: GitHub2014-07-12 13:10 19710人阅读 评论(21) 收藏 举报 githubio技术博客网站生成 说明: 首先,你需要注册一 ...
- About me & 一些置顶的博文
About me 一只历史上最弱的 \(\text{hnoier}\) ... 身在 \(\text{hn}\) 弱校,除了在四大名校夹缝中生存,还要受到同校 \(\text{Julao}\) 的鄙视 ...
- Ionic-wechat项目边开发边学(四):可伸缩输入框,下拉刷新, 置顶删除
摘要 上一篇文章主要介绍了ion-list的使用, ion-popup的使用, 通过sass自定义样式, localStorage的使用, 自定义指令和服务. 这篇文章实现的功能有消息的置顶与删除, ...
- thinkphp内置标签简单讲解
thinkphp内置标签简单讲解 1.volist循环 name 需要遍历的数据 id 类似于foreach中 value offset 截取数据起始位置 length 截取数据的个数 mod 奇偶数 ...
随机推荐
- libeXosip2(2-1) -- eXosip2 configuration API
eXosip2 configuration API General purpose API. Data Structures struct eXosip_dns_cache struct eX ...
- UVa1587.Digit Counting
题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=247&p ...
- 《数据通信与网络》笔记--IPSec
1.IP层的安全:IPSec IP层安全(IP security,IPsec)是由因特网工程任务组(IETF)设计用来为IP层的分组提供安全的一组协议.IPsec帮助 生成经过鉴别的与安全的IP层的分 ...
- Hive 9、Hive 在表中添加正则匹配
在Hive中还有一项比较好用的功能,也是非常重要的功能:在建表的时候可以不指定表的行.字段.列的分隔方式,通过给表指定一段正则表达式,让Hive自动去匹配: 1.创建表 CREATE TABLE ap ...
- EditText 文本内容输入限制
实现InputFilter过滤器,需要覆盖一个叫filter的方法. public abstract CharSequence filter ( CharSequence source, int st ...
- hdu 5676 ztr loves lucky numbers(dfs+离线)
Problem Description ztr loves lucky numbers. Everybody knows that positive integers are lucky if the ...
- python urllib基础学习
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #使用python创建一个简单的WEB客户端 import urll ...
- JBoss AS 7性能调优(三)
原文:http://www.mastertheboss.com/jboss-performance/jboss-as-7-performance-tuning/page-4 调优Webserver线程 ...
- 《TCP/IP具体解释卷2:实现》笔记--4种不同类型的mbuf
mbuf的主要用途是保存子进程和网络接口间互相传递的用户数据.但mbuf也用于保存其它各种数据:源于目的地址.插口 选项等等. 以下介绍我们要遇到的四种类型的mbuf,它们根据在成员m_flag中填写 ...
- boost::asio译文
Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENS ...