Apa itu Boot Musim Semi:
Spring Boot adalah kerangka kerja web Java berbasis layanan mikro sumber terbuka.
RowMapper:
Antarmuka RowMapper digunakan oleh JdbcTemplate untuk memetakan baris ResultSet pada basis per baris.
Pengantar
Di sini kita akan menggunakan custom row mapper ketika kita hanya menginginkan satu objek sebagai tipe pengembalian
Masalah di RowMapper untuk Kelas bersarang:
Kami tidak dapat secara langsung menetapkan parameter yang dikembalikan dari DB ke model kelas karena kami tidak pernah dapat menentukan variabel tipe data primitif individual karena bersarangnya kelas.
Diagram Kelas yang digunakan dalam Contoh ini:
Kelas Sederhana :Bukan Kelas Bersarang
SportsMasterDTO.java
package workspace;
public class SportsMasterDTO {
private int id;
private String sportsCode;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSportsCode() {
return sportsCode;
}
public void setSportsCode(String sportsCode) {
this.sportsCode = sportsCode;
}
}
Kelas SportsMetaDTO:
package workspace;
public class SportsMetaDTO {
private int id;
private int sportsName;
private int sportsMasterId;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getSportsName() {
return sportsName;
}
public void setSportsName(int sportsName) {
this.sportsName = sportsName;
}
public int getSportsMasterId() {
return sportsMasterId;
}
public void setSportsMasterId(int sportsMasterId) {
this.sportsMasterId = sportsMasterId;
}
@Override
public String toString() {
return "SportsMetaDTO [id=" + id + ", sportsName=" + sportsName + ", sportsMasterId="
+ sportsMasterId + "]";
}
}
Kelas ambang batas DTO
package workspace;
public class ThresholdDTO {
private int id;
private int sportsMetaDataId;
private String value;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getSportsMetaDataId() {
return sportsMetaDataId;
}
public void setSportsMetaDataId(int sportsMetaDataId) {
this.sportsMetaDataId = sportsMetaDataId;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "ThresholdDTO [id=" + id + ", sportsMetaDataId=" + sportsMetaDataId + ", value=" + value
+ "]";
}
}
Kelas Bersarang:
Kelas Bersarang:SportsThresholdCommonDTO
package workspace;
public class SportsThresholdCommonDTO {
private SportsMasterDTO sportsMasterDTO;
private SportsMetaDTO sportsMetaDTO;
private ThresholdDTO thresholdDTO;
public SportsMasterDTO getSportsMasterDTO() {
return sportsMasterDTO;
}
public void setSportsMasterDTO(SportsMasterDTO sportsMasterDTO) {
this.sportsMasterDTO = sportsMasterDTO;
}
public SportsMetaDTO getSportsMetaDTO() {
return sportsMetaDTO;
}
public void setSportsMetaDTO(SportsMetaDTO sportsMetaDTO) {
this.sportsMetaDTO = sportsMetaDTO;
}
public ThresholdDTO getThresholdDTO() {
return thresholdDTO;
}
public void setThresholdDTO(ThresholdDTO thresholdDTO) {
this.thresholdDTO = thresholdDTO;
}
@Override
public String toString() {
return "SportsThresholdCommonDTO [sportsMasterDTO=" + sportsMasterDTO + ", sportsMetaDTO="
+ sportsMetaDTO + ", thresholdDTO=" + thresholdDTO + "]";
}
}
Cara menggunakan rowmapper jika mysql mengembalikan satu objek untuk kelas bersarang:
Kueri MYSQL Yang berisi gabungan pada 3 tabel dengan batas 1
select
sm.id as sportsMasterId,
sm.sportsCode,
smd.id as sportsMetaId,
smd.sportsName,
smd.sportsMasterId,
t.id as thresholdId,
t.sportsMetaDataId,
t.value
from
sportsMaster
inner join sportsMetaData smd on sm.id = smd.sportsMasterId
inner join threshold t on t.sporsMetaDataId = smd.id limit 1
Cara Mengakses Basis Data:
Metode Java
public SportsThresholdCommonDTO getSportsThresholdCommonObject() {
String sql =
"select sm.id as sportsMasterId, sm.sportsCode, smd.id as sportsMetaId, smd.sportsName, smd.sportsMasterId, t.id as thresholdId, t.sportsMetaDataId, t.value from sportsMaster inner join sportsMetaData smd on sm.id = smd.sportsMasterId inner join threshold t on t.sporsMetaDataId = smd.id limit 1";
return jdbcTemplate.queryForObject(sql, SportsThresholdCommonDTO.class);
}
Penantian Berakhir Disini !!!
Kelas Pemeta
package workspace;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.tree.RowMapper;
public class SportsThresholdCommonMapper implements RowMapper<SportsThresholdCommonDTO> {
@Override
public SportsThresholdCommonDTO mapRow(ResultSet rs, int rowNum) throws SQLException {
SportsThresholdCommonDTO sportsThresholdCommonDTO = new SportsThresholdCommonDTO();
SportsMasterDTO sportsMasterDTO = new SportsMasterDTO();
SportsMetaDTO sportsMetaDTO = new SportsMetaDTO();
ThresholdDTO thresholdDTO = new ThresholdDTO();
sportsMasterDTO.setId(rs.getInt("sportsMasterId"));
sportsMasterDTO.setSportsCode(rs.getString("sportsCode"));
sportsMetaDTO.setId(rs.getInt("sportsMetaId"));
sportsMetaDTO.setSportsName(rs.getString("sportsName"));
sportsMetaDTO.setSportsMasterId(rs.getInt("sportsMasterId"));
thresholdDTO.setId(rs.getInt("thresholdId"));
thresholdDTO.setSportsMetaDataId(rs.getInt("sportsMetaId"));
thresholdDTO.setValue(rs.getString("value"));
sportsThresholdCommonDTO.setSportsMasterDTO(sportsMasterDTO);
sportsThresholdCommonDTO.setSportsMetaDTO(sportsMetaDTO);
sportsThresholdCommonDTO.setThresholdDTO(thresholdDTO);
return sportsThresholdCommonDTO;
}
}
Cara Alternatif:
- Buat model baru yang berisi variabel semua kelas
- Buat Alias untuk memetakan model t dengan benar
- Hasilnya akan menjadi kelas tidak bersarang dengan semua data.
Kelemahan membuat model lain
- Overhead pemeliharaan
- Membuat alias dan menyimpan nama yang berbeda di semua model
Kesimpulan
Kami bisa mendapatkan objek kelas bersarang dari mysql dengan bantuan custom row mapper.