1. Add a method to convert Map to UserInfo in UserInfo.java
public static UserInfo toObject(Map map) {UserInfo userInfo = new UserInfo();userInfo.setId((Integer) map.get(id));userInfo.setUname((String) map.get(uname));userInfo.setUnumber((Integer) map.get(unumber));userInfo.setUnumber((Integer) map.get(unumber));userInfo.setuRegisterTime((Date) map.get(uregister_time));return userInfo;}public static List toObject(List> lists){List userInfos = new ArrayList();for (Map map : lists) {UserInfo userInfo = UserInfo.toObject(map);if (userInfo != null) {userInfos.add(userInfo);}}return userInfos;}Dao layer implementation:
public List findAll() { String sql = SELECT * FROM user_info; List<Map<String,Object>> lists = jdbcTemplate.queryForList(sq); return UserInfo.toObject(lists); }Summary: This method can be implemented, but the speed is very slow.
2. Use jdbcTemplate.query(sql,RowMapper) to implement:
Dao layer implementation
jdbcTemplate.query(sql, new RowMapper<UserInfo>() { @Override public UserInfo mapRow(ResultSet rs, int rowNum) throws SQLException { UserInfo userInfo = new UserInfo(); userInfo.setUname(rs.getString("uname")); userInfo.setUnumber(rs.getInt("unumber")); userInfo.setuRegisterTime(rs.getDate("uregister_time")); return userInfo; } });Summary: It cannot be reused in other query methods.
3. Use RowMapper to implement the interface method and override the mapRow method:
public class UserInfo implements RowMapper, Serializable{ @Override public UserInfo mapRow(ResultSet rs, int rowNum) throws SQLException { UserInfo userInfo = new UserInfo(); userInfo.setId(rs.getInt(id)); userInfo.setUname(rs.getString(uname)); userInfo.setUnumber(rs.getInt(unumber)); userInfo.setUnumber(rs.getInt(unumber)); userInfo.setuRegisterTime(rs.getDate(uregister_time)); return userInfo; } }Dao layer implementation:
public UserInfo getById(Integer id) { String sql = SELECT * FROM user_info WHERE id = ?; UserInfo userInfo = jdbcTemplate.queryForObject(sql, new UserInfo(), new Object[] { id }); return userInfo; } public List findAll() { String sql = SELECT * FROM user_info; List userInfos = jdbcTemplate.query(sql, new UserInfo()); return userInfos; }4. Use the dao layer
jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<UserInfo>(Qsfymxb.class));
Spring provides a convenient RowMapper implementation ----BeanPropertyRowMapper
It can automatically map a row of data to the instance of the specified class. It first instantiates the class and then maps it to the attribute by matching the name.
For example: The attribute name (vehicleNo) matches the same name column or the same name column (VEHICLE_NO) with an underlined column (VEHICLE_NO). If a property does not match, the value of the property is Null
The above is all the content of this article about using jdbcTemplate query to return a custom object collection code example. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!