jdbcTemplate 泛型 查询

时间:2022-05-19 21:56:25

1. dao层方法使用泛型

    public  <T> List<T> findDistinctListByCondition(String str, Class<T> fieldClass) {
String sql = "select distinct " + str + " from checkin_request";
List<T> list = (List<T>) jdbcTemplate.query(sql, new RowMapper<Object>(){
@Override
public T mapRow(ResultSet rs, int rowNum) throws SQLException { return (T)rs.getObject();
}
});
return list;
}

2. action调用

        List<Integer> mediatypeList = checkInRequestService.findDistinctListByCondition("mediatype", Integer.class);
List<String> currentStatusList = checkInRequestService.findDistinctListByCondition("current_status", String.class);