BaseDao

public abstract class BaseDao<T> {
private Class<T> entityClass;
private T t;
protected BeanUtil beanUtil = new BeanUtil();
protected String tableName = "";
@SuppressWarnings("unchecked")
public BaseDao() {
ParameterizedType pType = (ParameterizedType) this.getClass().getGenericSuperclass();
entityClass = (Class<T>) pType.getActualTypeArguments()[0];
try {
t = entityClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
tableName = getTableName();
}

public List<T> findAll() throws Exception{
return findAll("","");
}

private String getTableName(){
tableName = entityClass.getSimpleName().toUpperCase();
if(AppControler.DB_PREFIX !=null && AppControler.DB_PREFIX.length()>0){
tableName = AppControler.DB_PREFIX + tableName;
}
if(AppControler.DB_SCHEMA !=null && AppControler.DB_SCHEMA.length()>0){
tableName = AppControler.DB_SCHEMA + "." + tableName;
}
return tableName;
}

public List<T> findAll(String sortBy, String sortAs) throws SQLException {
List<T> result = new ArrayList<T>();
Connection conn = DBUtil.getConnection();
List<String> fieldNameList = beanUtil.toFieldNameList(t);
BeanListHandler<T> beanListHandler = new BeanListHandler<T>(entityClass);
QueryRunner queryRunner = new QueryRunner();
String sql;
if (null != sortBy && sortBy.length() > 0 && fieldNameList.contains(sortBy.toUpperCase())) {
if ("DESC".equalsIgnoreCase(sortAs)) {
sql = "select * from " + tableName + sortBy + " DESC";
} else {
sql = "select * from " + tableName + " order by " + sortBy;
}
} else {
sql = "SELECT * FROM " + AppControler.DB_SCHEMA + tableName;
}
result = queryRunner.query(conn, sql, beanListHandler); 
closeConnection(conn);
return result;
}

public List<T> findAllByCondition(String condition,Object... parms) throws SQLException{
if(condition ==null || condition.length() == 0){
throw new IllegalArgumentException("parm condition is empty");
}
Connection conn = DBUtil.getConnection();
List<T> result = new ArrayList<T>();
BeanListHandler<T> beanListHandler = new BeanListHandler<T>(entityClass);
QueryRunner queryRunner = new QueryRunner();
String sql = "SELECT * FROM " + tableName + " WHERE " + condition ;
result = queryRunner.query(conn, sql,beanListHandler,parms);
closeConnection(conn);
return result;
}

public List<Integer> getMaxLengthOfEntityFieldsList() throws Exception{
List<Integer> fieldLengthList = new ArrayList<Integer>();
Connection conn = DBUtil.getConnection();
String sql = "select * from " + tableName;
ResultSet rset = conn.createStatement().executeQuery(sql);
ResultSetMetaData data = rset.getMetaData();
for(int i = 1; i <= data.getColumnCount(); i++){
fieldLengthList.add(data.getColumnDisplaySize(i));
}
DBUtil.closeAll(conn, null, rset);
return fieldLengthList;
}

public Map<String, Integer> getMaxLengthOfEntityFieldsMap() throws Exception{
Map<String, Integer> fieldLengthMap = new HashMap<String, Integer>();
Connection conn = DBUtil.getConnection();
String sql = "select * from " + tableName;
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery(sql);
ResultSetMetaData data = rset.getMetaData();
for(int i = 1; i < data.getColumnCount(); i++){
fieldLengthMap.put(data.getColumnName(i),data.getColumnDisplaySize(i));
}
DBUtil.closeAll(conn, stmt, rset);
return fieldLengthMap;
}

protected static void closeConnection(Connection conn){
if(conn !=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
conn = null;
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值