sexta-feira, 28 de setembro de 2018

Return Generic Type with jdbcTemplate.queryForObject - Generic Dao with jdbcTemplate


public Class TestClass  implements Serializable{
private int id;
private Usuario usuario;
private Date      data_hora;
private Fila id_fila;
private Integer sap;
...
 //omitted getter and setters  }

##################################################################

Method getObjectByID:

public <T> T getObjectByID(Class<? extends T> classe, String tableName, Integer id){

Object response= null;

try{
StringBuffer sql = new StringBuffer();
                        sql.append("SELECT * FROM );
                        sql.append(tableName);
                        sql.append(" WHERE ID = ?");

                        Object[] parametros = new Object[] {id};

response= jdbcTemplate.queryForObject(sql.toString(),parametros, new GenericRowMapper(classe));

}catch(Exception ex){
LOGGER.error("Error "+ex.getMessage(), ex);
}
return (T) response;
}
}

##################################################################
Class GenericRowMapper:

public class GenericRowMapper<T> implements RowMapper<T> {

  private Class<T> mappedClass;

  public GenericRowMapper(Class<T> mappedClass) {
    this.mappedClass = mappedClass;
  }

  @Override
  public T mapRow(ResultSet rs, int rowNum) throws SQLException {

    T mappedObject = BeanUtils.instantiateClass(this.mappedClass);
    BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject);

    bw.setAutoGrowNestedPaths(true);

    ResultSetMetaData meta_data = rs.getMetaData();
    int columnCount = meta_data.getColumnCount();

    for (int index = 1; index <= columnCount; index++) {

      try {

        String column = JdbcUtils.lookupColumnName(meta_data, index);
        Object value = JdbcUtils.getResultSetValue(rs, index, Class.forName(meta_data.getColumnClassName(index)));

        bw.setPropertyValue(column, value);

      } catch (TypeMismatchException | NotWritablePropertyException | ClassNotFoundException e) {
         // Ignore
      }
    }

    return mappedObject;
  }
}

##################################################################
search --->
TestClass  testClass  =  (TestClass ) genericDAO.getObjectByID(TestClass.class,"TableName",1);

#######################################################

Method  getListByStatus :

public <T> List<T> getListByStatus(Class<? extends T> classe, String tableName, String status) {
Object response = null;
try{
sql.append("SELECT * FROM );
                        sql.append(tableName);
                        sql.append(" WHERE status = ?");

                       response = jdbcTemplate.query(sql.toString(), new GenericRowMapper<T>((Class<T>) classe));

}catch(Exception ex){
LOGGER.error("Error "+ex.getMessage(), ex);
}
return (List<T>) response;
}

#######################################################

search:
List<TestClass> testList  =  (List<TestClass>) genericDAO.getListByStatus(TestClass.class,"TableName", StatusEnum.cancel);




Nenhum comentário:

Postar um comentário