spring-projects / spring-data-relational

Spring Data Relational. Home of Spring Data JDBC and Spring Data R2DBC.
https://spring.io/projects/spring-data-jdbc
Apache License 2.0
774 stars 348 forks source link

Provide a way to get EntityRowMapper #1950

Open VietAnh14 opened 1 week ago

VietAnh14 commented 1 week ago

I want to reuse the EntityRowMapper to query using JdbcTemplate, currently I can't find an easy way to get the mapper for an entity class and have to copy paste code from DefaultDataAccessStrategy to create one. It would be nice if you could support this some how.

mp911de commented 1 week ago

Have you consulted the documentation?

Other than that, we do not accept RowMapper query method arguments.

VietAnh14 commented 1 week ago

I think you miss understand me, I'm not using @Query annotation but using JdbcTemplate/NamedParameterJdbcTemplate to execute some query built at runtime. This query will return the entity so it's would be nice if I can reuse the EntityRowMapper class of spring data jdbc to map the result to entity.

Basically like this:

      var entityRowMapper = //Some how get spring data jdbc row mapper for entity class
      var user = namedParameterJdbcTemplate.queryForObject(
              "select * from users where id = :id", 
              Map.of("id", "0001"), 
              entityRowMapper
      );