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

2014-11-24 07:23:27 · 作者: · 浏览: 7
}
return pkValuePk;
}

public String createInsertSql(T instance){
String sql = cache.getInsertSqlPrefix();
for (PropertyDescriptor descriptor : cache.getProperties()) {
try {
sql=sql.replaceFirst("[ ]", descriptor.getReadMethod().invoke(instance, null).toString());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
return sql;
}

public String createUpdateSql(T instance){
String sql = cache.getUpdateSqlPrefix();
for (PropertyDescriptor descriptor : cache.getProperties()) {
try {
sql=sql.replaceFirst("[ ]", descriptor.getReadMethod().invoke(instance, null).toString());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
return sql.replaceFirst("[ ]",getPK(instance).toString());
}

public String createDeleteSql(T instance){
String sql = cache.getDeleteSqlPrefix();
return sql.replaceFirst("[ ]",getPK(instance).toString());
}

public String createDeleteSqlByPKValue(PK pkValue){
String sql = cache.getDeleteSqlPrefix();
return sql.replaceFirst("[ ]",pkValue.toString());
}

public String createSearchSqlByPKValue(PK pkValue){
String sql = cache.getSearchByIDSqlPrefix();
return sql.replaceFirst("[ ]",pkValue.toString());
}

public String createSearchSqlByCondition(String condition){
String sql = cache.getSearchSqlPrefix();
return sql+condition;
}

public String createCountSql() {
return cache.getCountSqlPrefix();
}

public String createCountSqlByCondition(String condition){
return cache.getCountByConditionSqlPrefix()+condition;
}

private String getHoldFlag(PropertyDescriptor descriptor) {
return (isNeedWarpQuotes(descriptor)==true "' '":" ");
}

private boolean isNeedWarpQuotes(PropertyDescriptor descriptor){
String type = descriptor.getPropertyType().getSimpleName();
if (type.compareToIgnoreCase("String")==0) {
return true;
}else if (type.compareToIgnoreCase("Date")==0) {
return true;
}else{
return false;
}
}

}
[java]
package com.nbport.xk.dao; 
[java]
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapperResultSetExtractor;
import org.springframework.jdbc.support.Gener