Java操作mysql,指定返回的类型

  1. public Map<String, GtStation> getGtAllStation() {
  2. return mysqlJdbcTemplate.query("SELECT AREA_CODE,STATIONID,STATIONNAME,ESTLGTUD,NORLATUD FROM gt_station;", new AllGtStation());
  3. }
  4. #创建内部类,指定返回的类型
  5. class AllGtStation implements ResultSetExtractor<Map<String, GtStation>> {
  6. @Override
  7. public Map<String, GtStation> extractData(ResultSet result) throws SQLException, DataAccessException {
  8. Map<String, GtStation> stationMap = new HashMap<>();
  9. while (result.next()) {
  10. GtStation station = new GtStation();
  11. String area_code = result.getString("AREA_CODE");
  12. String stationid = result.getString("STATIONID");
  13. String stationname = result.getString("STATIONNAME");
  14. float estlgtud = result.getFloat("ESTLGTUD");
  15. float norlatud = result.getFloat("NORLATUD");
  16. station.setAreaCode(area_code);
  17. station.setStationId(stationid);
  18. station.setStationName(stationname);
  19. station.setLg(estlgtud);
  20. station.setLt(norlatud);
  21. stationMap.put(stationid, station);
  22. }
  23. return stationMap;
  24. }
  25. }