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;
}
}
}
}
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;
}
}
}
}