Spring JDBC RowMapper usage for eager fetches

后端 未结 4 1593
执念已碎
执念已碎 2020-12-14 10:26

The question is about the best practice usage for RowMapper in master/detail scenarios where we want to eagerly fetch details using spring jdbc.

Assume that we have

4条回答
  •  醉酒成梦
    2020-12-14 11:26

    Easiest Method

    You can simply use this library - SimpleFlatMapper has already solved that problem. All you need to do is create a ResultSetExtractor using the JdbcTemplateMapperFactory.

    import org.simpleflatmapper.jdbc.spring.JdbcTemplateMapperFactory;
    
    private final ResultSetExtractor> resultSetExtractor = 
        JdbcTemplateMapperFactory
            .newInstance()
            .addKeys("id") // the column name you expect the invoice id to be on
            .newResultSetExtractor(Invoice.class);
    
    String query = "SELECT * FROM INVOICE inv JOIN INVOICE_LINE line on inv.id = line.invoice_id"
    
    List results = jdbcTemplate.query(query, resultSetExtractor);
    

    Add this dependancey to the pom.xml

    
        org.simpleflatmapper
        sfm-springjdbc
        8.2.1
    
    

    Here is an article to refer - https://arnaudroger.github.io/blog/2017/06/13/jdbc-template-one-to-many.html

    Here are examples for different usages - https://github.com/arnaudroger/SimpleFlatMapper/blob/master/sfm-springjdbc/src/test/java/org/simpleflatmapper/jdbc/spring/test/JdbcTemplateMapperFactoryTest.java

提交回复
热议问题