论坛首页 Java企业应用论坛

准备发布Jert的第一个版本

浏览 70596 次
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2005-01-29  
Quake Wang 写道
Tao 写道

1.SQL的参数设置没有必要搞得这么复杂,像 where 1 =1 这样的形式也不大雅观.
其实只是个SQL技巧的问题,对于你所说的动态的非固定参数查询,只需要用下面的SQL就可达到同样的功能:
select * from user where (age > #age# or #age# isnull) and (sex = #sex# or #sex# isnull)

嗯,我也觉得这种动态sql查询目前写起来极其不友好,但是想不出有什么好方法可以改进。
你说的这种方法我看不明白,如果用户在页面上只输入了age = 18,而 sex留空,那么翻译出来的sql是什么呢:
select * from user where (age > 18 or 18 is null) and (sex = ??? or ??? is null)

这部分的使用,准备等用户反馈,再来做改进,可能格式会推倒,重新实现。



嗯,我模仿hibernate的criteria做了一个针对sql的criteria:

package poca.database.sql;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author joe
 *
 */
public class Criteria {
	private static final Log log = LogFactory.getLog(Criteria.class);;
	
	public static final String GE = ">=";
	public static final String GT = ">";
	public static final String EQ = "=";
	public static final String LT = "<";
	public static final String LE = "<=";
	public static final String IN = "in";
	public static final String LIKE = "like";
	
	private List crit = new ArrayList();;
	private List para = null;
	private String sql = null;
	
	public void add(String field, String compare, Object value); {
		if (value == null); return;
		else if (value instanceof List); {
			List list = (List); value;
			if (list.size(); == 0); return;
		}
		String cp = compare;
		Object va = value;
		if (LIKE.equals(compare);); {
			if (value instanceof java.lang.String); {
				String s = (String); value;
				if (StringUtils.isBlank(s);); return; else s = s.trim();;
				if (s.charAt(s.length(); - 1); != '%'); {
					cp = EQ; va = s;
				}
			}
		} else if (IN.equals(compare);); {
			if (!(value instanceof List);); {
				Object[] tmp = (Object[]); value;
				if (tmp.length == 0); return;
				List list = new ArrayList();;
				for (int j = 0; j < tmp.length; j++); {
					list.add(tmp[j]);;
				}
				va = list;
			}
		} else if (value instanceof java.util.Date); {
			java.util.Date d = (java.util.Date); value;
			java.sql.Date dd = new java.sql.Date(d.getTime(););;
			va = dd;
		}
		crit.add(new Object[] {field, cp, va});;
	}
	
	public void execute(); {
		para = new ArrayList();;
		StringBuffer buff = new StringBuffer("");;
		for (int i = 0; i < crit.size();; i++); {
			Object[] os = (Object[]); crit.get(i);;
			String field = (String); os[0];
			String compare = (String); os[1];
			Object value = os[2];
			if (buff.length(); > 0); buff.append(" and ");;
			buff.append(field);.append(" ");.append(compare);.append(" ? ");;
			para.add(value);;
		}
		sql = buff.toString();;
	}
	
	public String getSqlString(); {
		return sql;
	}
	
	public List getParamValue(); {
		return para;
	}
	
}




而在程序中是这样调用的:

private Criteria prepareCriteria(Map param); {
	Criteria crit = new Criteria();;
	String station = (String); param.get("station");;
	Boolean sub = (Boolean); param.get("subStation");;
	if (sub.booleanValue();); {
		crit.add("station", Criteria.LIKE, station + "%");;
	} else {
		crit.add("station", Criteria.EQ, station);;
	}
	Date date = (Date); param.get("startDate");;
	crit.add("opdate", Criteria.GE, DateUtil.getFirstDayOfMonth(date););;
	crit.add("opdate", Criteria.LE, DateUtil.getLastDayOfMonth(date););;
	crit.execute();;
	return crit;
}



然后设置sql参数并执行:

StringBuffer buff = new StringBuffer(SQL_HEAD);;
buff.append(" where ");.append(crit.getSqlString(););;
buff.append(SQL_TAIL);;
String s = buff.toString();;
log.debug(s);;
stmt = session.connection();.prepareStatement(s);;
for (int i = 0; i < crit.getParamValue();.size();; i++); {
	stmt.setObject(i+1, crit.getParamValue();.get(i););;
}
rs = stmt.executeQuery();;



这样做基本上code看起来还是蛮顺眼的:)
0 请登录后投票
   发表时间:2005-01-29  
towjzhou 写道
to thejoe:
   用excel做报表模板是个好方法,我怎么就没想到呢,我打算好好想一下可行性。我被ireport那个界面吓到了,这个界面敢让客户用嘛。


用excel做模版工具是不错,但是报表生成你就要多考虑了,目前poi的功能还是不够强,比如你在excel中手工插入一行时,sum函数会自动把这一行包括进去,而你用poi插入时就必须自己调整了。

还有一个想法是报表生成仍然用现成的报表引擎,这样的话问题就集中在如何把excel模版转成jasper或其他模版,另一个问题是现在的报表引擎export出的excel文件都比较丑陋,肯定不如直接用poi填充的excel表。

所以我设想的解决方法是:excel做模版工具,编制excel转jasper工具,扩展japser引擎当export到excel时用poi实现。当然这些现在只是想想,仅供参考:)
0 请登录后投票
   发表时间:2005-01-29  
inprise_lyj 写道
奇怪,我把jer.war copy到resin2.1.4下面,运行http://localhost:8080/jert/setup/index.action
出现如下错误:

--------------------------------------------------------
500 Servlet Exception
org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'sessionFactory' defined in class path resource [com/javaeye/jert/application_context.xml]:
Initialization of bean failed; nested exception is net.sf.hibernate.MappingException:
org.dom4j.DocumentException: Validation not supported for XMLReader: com.caucho.xml.Xml@1ba94d
Nested exception: http://xml.org/sax/features/validation Nested exception:
Validation not supported for XMLReader: com.caucho.xml.Xml@1ba94d Nested
exception: http://xml.org/sax/features/validation
net.sf.hibernate.MappingException: org.dom4j.DocumentException: Validation
not supported for XMLReader: com.caucho.xml.Xml@1ba94d Nested exception:
http://xml.org/sax/features/validation Nested exception: Validation not
supported for XMLReader: com.caucho.xml.Xml@1ba94d Nested exception: http://xml.org/sax/features/validation
at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:296)
at org.springframework.orm.hibernate.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:383)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:990)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:275)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:193)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:240)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:163)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:230)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:304)
at org.springframework.web.context.support.XmlWebApplicationContext.refresh(XmlWebApplicationContext.java:131)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:167)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:101)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:48)
at com.caucho.server.http.Application.init(Application.java:1838)
at com.caucho.server.http.VirtualHost.startApplication(VirtualHost.java:1192)
at com.caucho.server.http.VirtualHost.getInvocation(VirtualHost.java:991)
at com.caucho.server.http.ServletServer.getInvocation(ServletServer.java:1189)
at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:218)
at com.caucho.server.http.HttpRequest.handleConnection(HttpRequest.java:163)
at com.caucho.server.TcpConnection.run(TcpConnection.java:137)
at java.lang.Thread.run(Thread.java:534)
Caused by: org.dom4j.DocumentException: Validation not supported for XMLReader:
com.caucho.xml.Xml@1ba94d Nested exception: http://xml.org/sax/features/validation
Nested exception: Validation not supported for XMLReader: com.caucho.xml.Xml@1ba94d
Nested exception: http://xml.org/sax/features/validation
at org.dom4j.io.SAXReader.read(SAXReader.java:358)
at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:286)
... 20 more


--------------------------------------------------------------------------------
Resin 2.1.4 (built Fri Aug 2 14:16:52 PDT 2002)

换3.03马上解决
0 请登录后投票
   发表时间:2005-07-27  
我这几天又在作报表中转悠,前几天看iReport /jasperreport/JFreechart 的组合,也做了些例子,感觉在发布时遇到了些问题;
所以这几天有在看OpenReports,她对发布这块支持的还可以,但开发工具的中文好像支持的不好。

如果搞好了,我也从她们那里寻找咪咪,作个好丈夫!哈哈
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics