spring jdbctemplate entity column and data table column query

1. BeanPropertyRowMapper interface

  1. When I query, I use the following interface. The principle is that the result will be obtained when the field name of the entity class and the column name of the table are the same, otherwise the corresponding field is null
List<MusicSong> songList = jdbcTemplate.query(sql.toString(), 
				new Object[]{
    
    musicListId, startLine, endLine},
                new BeanPropertyRowMapper<>(MusicSong.class));

2. Create a mapper class to implement the interface

class MusicSongRowMapper implements RowMapper<MusicSong> {
    
    

    @Override
    public MusicSong mapRow(ResultSet rs, int rowNum) throws SQLException {
    
    
        MusicSong song = new MusicSong();

		// 设置结果集
        song.setSongId(rs.getLong("music_song_id"));
        song.setTitle(rs.getString("music_title"));
        song.setCoverImg(rs.getString("music_cover_img"));
        song.setSinger(rs.getString("music_singer"));
        song.setDetails(rs.getString("music_details"));
        song.setListeningVolume(rs.getLong("music_listening_volume"));
        song.setLyricsId(rs.getLong("lyrics_id"));
        song.setCreateDate(rs.getDate("music_create_date"));
        return song;
    }
}

The changes are as follows, with subclasses

List<MusicSong> songList = jdbcTemplate.query(sql.toString(), 
				new Object[]{
    
    musicListId, startLine, endLine},
                new MusicSongRowMapper());

Guess you like

Origin blog.csdn.net/qq_44783283/article/details/108108912
Recommended