今天在做项目开发的时候,发现数据库查询返回的数据列名顺序不对,查询语句为select id,name from info_user,按道理返回的列顺序应该是id,name,但是由于HashMap的无序性,造成返回的数据是name,id,对此,我重写了MapListHandler的handleRow方法,故将代码备份如下:
public class LinkMapListHandler extends MapListHandler{ @Override protected Map<String, Object> handleRow(ResultSet rs) throws SQLException { Map<String, Object> result = new LinkedHashMap<String, Object>(); ResultSetMetaData rsmd = rs.getMetaData(); int cols = rsmd.getColumnCount(); for ( int i = 1; i <= cols; i++) { result.put(rsmd.getColumnName(i), rs.getObject(i)); } return result; } }
调用语句:
list = (List<Map<String, Object>>) queryRunner.query(sql, new LinkMapListHandler(), params );
简单方便…