GROM是Grails框架提供的一种O/R Mapping的解决方案。通过GROM可以摆脱繁琐的SQL书写,并且根据搜索到的结果自动生成Groovy实现,可以省去数据访问层代码的开发,减轻程序员的工作量。Java中的JDO、S2JDBC都是此类框架,而GROM的底层是Hibernate 3,是一种非常广泛用于Java开发的数据层框架。
(关于GROM的具体使用方法请参考 Grails官方的 《Grails用户手册》,这里不作过多描述。)
GROM提供了很多执行SQL语句的接口,包括用slect语句取得count的结果,如使用下面的代码,可以得到姓Simpson的人的数量。
def criteria = new DetachedCriteria(Person).build { eq 'lastName', 'Simpson' }
def results = criteria.list(max:4, sort:"firstName")
以上例子可以看出,这种提取搜索结果数量的方式,主要针对某一个表的搜索,而在实际的商业开发中,有很多数据搜索是相对复杂的。需要对多个表进行结合,或者,需要通过与子搜索结合起来才能得到搜索结果。
对于前者,可以通过设定目标之间的关系,通过载入功能就可以实现。但是,后者需要使用子查询的情况就是GORM难以实现的了。这一点是由于GORM内核本身是基于Hibernate的缘故,不但继承了强大的O/R优点,同时也继承了无法使用子查询的软肋。
这种情况,我个人的做法是使用Groovy访问数据库功能,直接执行select语句,提取执行结果,做法如下: 1.首先在相应的service类中定义 def dataSource,Grails在执行时会自动将你的DataSouce插入进去
2.在service类中相应的方法内,类似以下执行并返回结果
def sql = new Sql(dataSource)
def result = sql.firstRow('select count(*) as c from (select max(c.id) from loveapple_contents c, loveapple_site s\
where s.id = parent_site_id and s.id = ? and parent_category_alias = ? group by c.alias) t' ,
[site.id, categoryAlias]) return result
这段代码是 番茄CMS在提取分类中文章数量的一段处理。
(当然,通常的网站在搜索文章数量是并不需要这么复杂,因为番茄CMS的功能比较多,数据结构比较特殊,所以只能用子查询。) 以上是笔者在Grails开发中用过的搜索count的方法,希望能给大家帮助。
原文地址:http://www.loveapple.cn/app/Grails/grails-chips-gorm-sql-count
package org.ustc.statemode; public class Work { private State state; private boolean isFinished ; public Work() { state = new MoningState(); setFinished(false); } public State getState() { return state; } public void setState(State state) { this.state = state; } public int getHour() { return hour; } public void setHour(int hour) { this.hour = hour; } private int hour; public void writeProgram(){ state.writeProgram(this); } public void setFinished(boolean isFinished) { this.isFinished = isFinished; } public boolean isFinished() { return isFinished; } }package org.ustc.statemode; public interface State { public void writeProgram(Work w); }package org.ustc.statemode; public class MoningState implements State { @Override public void writeProgram(Work w) { // TODO Auto-generated method stub if(w.getHour()<12) System.out.println("精力充沛,time:"+w.getHour()); else{ w.setState(new AfternoonState()); w.writeProgram(); } } }
package org.ustc.statemode; public class AfternoonState implements State { @Override public void writeProgram(Work w) { // TODO Auto-generated method stub if(w.getHour()<17) System.out.println("下午犯困"); else{ w.setState(new EveningState()); w.writeProgram(); } } }
package org.ustc.statemode; public class EveningState implements State { @Override public void writeProgram(Work w) { // TODO Auto-generated method stub if(w.isFinished()==true){ w.setState(new RestState()); w.writeProgram(); }else{ if(w.getHour()<20) System.out.println("好累"); else{ w.setState(new SleepState()); w.writeProgram(); } } } }
package org.ustc.statemode; public class RestState implements State { @Override public void writeProgram(Work w) { // TODO Auto-generated method stub System.out.println("收工 下班咯"); } }
package org.ustc.statemode; public class SleepState implements State { @Override public void writeProgram(Work w) { // TODO Auto-generated method stub System.out.println("不行了睡着了"+w.getHour()); } }
package org.ustc.statemode; public class Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Work work = new Work(); //work.setHour(20); //work.setHour(9); // work.setHour(14); work.setFinished(true); work.setHour(20); work.writeProgram(); } }
作者:a17909863341 发表于2013-5-2 15:01:39 原文链接阅读:105 评论:0 查看评论
GOF 在《设计模式》:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
package com.google.abstractfactory;
//抽象工厂角色:工厂接口
public interface Gardener {
public Fruit createFruit(String name);
public Veggie createVeggie(String name);
}
package
com.google.abstractfactory;
//抽象水果产品角色:水果接口
public interface Fruit {
}
package com.google.abstractfactory;
// 抽象蔬菜产品角色:蔬菜接口
public interface Veggie {
}
packagecom.google.abstractfactory;
//具体产品角色:热带水果
public class TropicalFruit implements Fruit {
private String name;
public TropicalFruit(String name) {
System.out.println("热带工厂为您创建了:热带水果-"+name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
packagecom.google.abstractfactory;
//具体产品角色:热带蔬菜
public class TropicalVeggie implements Veggie {
private String name;
public TropicalVeggie(String name) {
System.out.println("热带工厂为您创建了:热带水果-"+name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
packagecom.google.abstractfactory;
//具体产品角色:亚热带水果
public class NorthernFruit implements Fruit {
private String name;
public NorthernFruit(String name) {
System.out.println("亚热带工厂为您创建了:亚热带水果-"+name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
packagecom.google.abstractfactory;
//具体产品角色:亚热带蔬菜
public class NorthernVeggie implements Veggie {
private String name;
public NorthernVeggie(String name) {
System.out.println("亚热带工厂为您创建了:亚热带蔬菜-"+name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.google.abstractfactory;
//具体工厂角色:热带工厂
public class TropicalGardener implements Gardener {
public Fruit createFruit(String name) {
return new TropicalFruit(name);
}
public Veggie createVeggie(String name) {
return new TropicalVeggie(name);
}
}
package com.google.abstractfactory;
//具体工厂角色:亚热带工厂
public class NorthernGardener implements Gardener {
public Fruit createFruit(String name) {
return new NorthernFruit(name);
}
public Veggie createVeggie(String name) {
return new NorthernVeggie(name);
}
}
package com.google.abstractfactory;
//工厂模式--抽象工厂模式--一般性模式(农场应用)
//测试类(客户端)
public class TestFactoryPattern {
private void test(){
Veggie tv,nv;
Fruit tf,nf;
TropicalGardener
tropicalGardener=new TropicalGardener();
NorthernGardener