samedi 22 avril 2023

Set up properties dynamically in DTO object from the output of RowCallBackHandler in spring-boot application

Below is my DTO object and I am constructing SQL query based on the user selection.So,it can be having any set of properties from the DTO class.

Eg: Sample SQL queries: (Any combination)
select res.NAME from RESPONSE res ;
select res.NAME , res.CITY from RESPONSE res ; 
select res.NAME , res.PRICE from RESPONSE res ;
....
select res.* from RESPONSE res ; 


@Entity
@Table(name="RESPONSE")    
public class Response{
    
    @Cloumn("NAME")
    private String name;
    @Cloumn("CITY")
    private String city;
    @Cloumn("AMOUNT")
    private BigDecimal amount;
    ...
    @Cloumn("PRICE")
    private BigDecimal price;
    }
    
    List<String> userSelectionList=getUserSelectionList();
    String sql=generateSqlBasedOnUserSelection();
    List<Response> responseList=new ArrayList();
    jdbcTemplate.query(sql,new RowCallBackHandler(){
    
    public void processRows(ResultSet resultSet) throws SqlException{
     Response response=new Response();
    //How will set the properties in Response object from ResultSet.

     responseList.add(response);

    }




Aucun commentaire:

Enregistrer un commentaire