用JDBCTemplate实现的单表通用DAO,实现增删改查和统计(二)

2014-11-24 07:23:27 · 作者: · 浏览: 1
turn searchByIDSqlPrefix;
}

public void setSearchByIDSqlPrefix(String searchByIDSqlPrefix) {
this.searchByIDSqlPrefix = searchByIDSqlPrefix;
}

public String getCountSqlPrefix() {
return countSqlPrefix;
}

public void setCountSqlPrefix(String countSqlPrefix) {
this.countSqlPrefix = countSqlPrefix;
}

public String getCountByConditionSqlPrefix() {
return countByConditionSqlPrefix;
}

public void setCountByConditionSqlPrefix(String countByConditionSqlPrefix) {
this.countByConditionSqlPrefix = countByConditionSqlPrefix;
}

}

private TableCache cache = null;

public Table(Class beanClass,RowMapper rowMapper,String tableName,String tableKey,boolean autoGeneratePK,SimpleDateFormat dateFormat){
this.beanClass = beanClass;
this.rowMapper = rowMapper;
this.tableName = tableName;
this.tableKey = tableKey;
this.autoGeneratePK = autoGeneratePK;
this.dateFormat = dateFormat;
init();
}

public Table(Class beanClass,RowMapper rowMapper,String tableName,String tableKey){
this(beanClass,rowMapper, tableName, tableKey, true, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
}

public void init() {
cache = new TableCache();
BeanWrapper beanWrapper = null;
try {
beanWrapper = new BeanWrapperImpl(beanClass.newInstance());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
cache.setKeyDescriptor( beanWrapper.getPropertyDescriptor(tableKey) );
cache.setProperties(new ArrayList());
{
for (PropertyDescriptor descriptor : beanWrapper.getPropertyDescriptors()) {
String name = descriptor.getName();
if (!beanWrapper.isWritableProperty(name)
|| (getAutoGeneratePK() && name.compareToIgnoreCase(getTableKey())==0)){
continue;
}
cache.getProperties().add(descriptor);
}
}
cache.setKeyGetMethod(cache.getKeyDescriptor().getReadMethod());
cache.setKeySetMethod(cache.getKeyDescriptor().getWriteMethod());
cache.setDeleteSqlPrefix("delete from "+tableName+" where "+tableKey+"="+getHoldFlag(cache.getKeyDescriptor()));
cache.setSearchByIDSqlPrefix("select * from "+tableName+" where "+tableKey+"="+getHoldFlag(cache.getKeyDescriptor()));
cache.setSearchSqlPrefix("select * from "+tableName+" where ");
cache.setCountSqlPrefix("select count(1) from "+tableName);
cache.setCountByConditionSqlPrefix("select count(1) from "+tableName+" where ");
{
StringBuffer tempColumns = new StringBuffer();
StringBuffer tempValueHolds = new StringBuffer();
for (PropertyDescriptor descriptor : cache.getProperties()) {
String name = descriptor.getName();
if (!beanWrapper.isWritableProperty(na