zoukankan      html  css  js  c++  java
  • [置顶] 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语句进行查询的。

  • 相关阅读:
    单例模式
    HashSet、LinkedHashSet、SortedSet、TreeSet
    ArrayList、LinkedList、CopyOnWriteArrayList
    HashMap、Hashtable、LinkedHashMap
    andrew ng machine learning week8 非监督学习
    andrew ng machine learning week7 支持向量机
    andrew ng machine learning week6 机器学习算法理论
    andrew ng machine learning week5 神经网络
    andrew ng machine learning week4 神经网络
    vue组件监听属性变化watch方法报[Vue warn]: Method "watch" has type "object" in the component definition. Did you reference the function correctly?
  • 原文地址:https://www.cnblogs.com/james1207/p/3328909.html
Copyright © 2011-2022 走看看