????????如果沒有用ORM工具,這些又不能省了不寫。于是我考慮用代碼來生成這些sql 和 setXXX及getXXX方法。
??????? 生成代碼有許多方法,比如可以用腳本語言(個(gè)人喜歡用perl),也可以用模板技術(shù)。發(fā)現(xiàn)java里面已經(jīng)有很多模板技術(shù)可以直接使用了,比如velocity、freemaker等。我一開始是直接用perl來生成代碼的,方法比較原始,就是字符串拼湊在一起。 后來發(fā)現(xiàn)有許多的模板技術(shù)可以利用。現(xiàn)在打算用velocity來生成代碼。說不定可以直接生成DAO、Biz、Bean、XML等一大堆東西,呵呵。等有空要好好研究一下。
??? 下面是我在就業(yè)網(wǎng)重構(gòu)時(shí)用到的一個(gè)java類,其中就是對(duì)BeanUtils進(jìn)行了簡(jiǎn)單的封裝。
???import java.sql.Date;
???import java.util.Map;
?? import org.apache.commons.beanutils.BeanUtils;
?? import org.apache.commons.beanutils.ConvertUtils;
?? import org.apache.commons.beanutils.converters.SqlDateConverter;
?? public class NullSafeBeanUtils {
??
?? public final static String EMPTY_STRING = "";
?
?? public static boolean isNull(Object obj) {
?????? return obj == null;
?? }
?? public static String getProperty(Object bean, String property) {
????? ?if (bean == null) {
?????????? ?return EMPTY_STRING;
????? ?}
??? ?try {
???????? ?String str = BeanUtils.getProperty(bean, property);
???????? ?if (str == null) {
????????????? return EMPTY_STRING;
??????? ?}
??????? return str;
???? } catch (Exception e) {
??????? ?return EMPTY_STRING;
?? }
? }
??public static void populate(Object bean, Map props) {
??? ?if (bean == null) {
??????? return;
???? }
?? ?try {
???? SqlDateConverter con = new SqlDateConverter(new Date(System.currentTimeMillis()));
????? ConvertUtils.register(con, java.sql.Date.class);
????? BeanUtils.populate(bean, props);
?? } catch (Exception e) {
??? ?e.printStackTrace();
?? }
?}
?//?此處省略了一些其他代碼
?}
?
??? 在這里,poplulate方法就是我用來自動(dòng)填充參數(shù)的。要實(shí)現(xiàn)自動(dòng)填充,只需簡(jiǎn)單調(diào)用此方法就行了。看一個(gè)例子:
??? JobExperience jobExp = new JobExperience();
??? NullSafeBeanUtils.populate(jobExp, request.getParameterMap());
?? 是不是簡(jiǎn)單了許多?要注意的是表單的各輸入字段名要和bean的各屬性名對(duì)應(yīng)才能自動(dòng)填充。另外NullSafeBeanUtils 的getProperty方法也很有用,可以避免寫
?? if (bean != null) {
????? yyy = bean.getXXX()==null?"":bean.getXXX()
???}
?? 這樣的代碼,直接寫NullSafeBeanUtils.getProperty(bean, "XXX")就可以了。
UNIQ(1)????User Commands???????? UNIQ(1)
?
NAME
?????? uniq - remove duplicate lines from a sorted file
SYNOPSIS
?????? uniq [OPTION]... [INPUT [OUTPUT]]
DESCRIPTION
?????? Discard all but one of successive identical lines from INPUT (or stan-
?????? dard input), writing to OUTPUT (or standard output).
?????? Mandatory arguments to long options are mandatory? for? short? options
?????? too.
?????? -c, --count
?????? prefix lines by the number of occurrences
?????? -d, --repeated
?????? only print duplicate lines
?????? -D, --all-repeated[=delimit-method] print all duplicate lines
?????? delimit-method={none(default),prepend,separate}? Delimiting? is
?????? done with blank lines.
?????? -f, --skip-fields=N
?????? avoid comparing the first N fields
?????? -i, --ignore-case
?????? ignore differences in case when comparing
?????? -s, --skip-chars=N
?????? avoid comparing the first N characters
?????? -u, --unique
?????? only print unique lines
?????? -w, --check-chars=N
?????? compare no more than N characters in lines
?????? --help display this help and exit
?????? --version
?????? output version information and exit
?????? A field is? a? run? of? whitespace,? then? non-whitespace? characters.
?????? Fields are skipped before chars.
AUTHOR
?????? Written by Richard Stallman and David MacKenzie.
REPORTING BUGS
?????? Report bugs to <bug-coreutils@gnu.org>.
COPYRIGHT
?????? Copyright ? 2004 Free Software Foundation, Inc.
?????? This? is? free software; see the source for copying conditions.?There
?????? is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICU-
?????? LAR PURPOSE.
SEE ALSO
?????? The full documentation for uniq is maintained as a Texinfo manual.? If
?????? the info and uniq programs are properly installed at? your? site,? the
?????? command
?????? info coreutils uniq
?????? should give you access to the complete manual.