String sql =“ SELECT” + columnList +“ FROM结果”;
列表结果= jdbcTemplate.query(sql,新的ResultRowMapper());
结果类具有很多字段。我只想返回json的列列表中指定的那些字段,无论是否为null。我想出的解决方案是使用列列表中的字段动态创建一个临时类,然后像下面这样使用它:
列表结果= jdbcTemplate.query(sql,新的ResultRowMapper());
关于如何创建此Temp类的任何建议?字段将根据用户输入而更改。
感谢您的时间。
从上面的查询和评论中,您指出SELECT
子句中的列列表是动态的,这意味着即使RowMapper
也会根据列表进行更改。
让我们创建一个扩展ResultRowMapper
的接口org.springframework.jdbc.core.RowMapper
,如下所示:
public interface ResultRowMapper extends RowMapper<Person> {
default List<String> getColumnList(){
return Arrays.asList("id", "first_name", "last_name", "dob", "place_of_birth", "address");
}
default String getColumnListAsString(){
return getColumnList().stream().collect(Collectors.joining(", "));
}
@Override
default Person mapRow(ResultSet resultSet, int i) throws SQLException {
Person p = new Person();
p.setId(resultSet.getLong("id"));
p.setFirstName(resultSet.getString("first_name"));
p.setLastName(resultSet.getString("last_name"));
p.setAddress(resultSet.getString("address"));
p.setPlaceOfBirth(resultSet.getString("place_of_birth"));
return p;
}
}
然后,根据我们的需求,我将创建此ResultRowMapper
接口的不同实现:
@Component("DefaultResultRowMapper")
public class DefaultResultRowMapper implements ResultRowMapper {
}
@Component("NameResultRowMapper")
public class NameResultRowMapper implements ResultRowMapper {
@Override
public List<String> getColumnList(){
return Arrays.asList("id", "first_name", "last_name");
}
@Override
public Person mapRow(ResultSet resultSet, int i) throws SQLException {
Person p = new Person();
p.setId(resultSet.getLong("id"));
p.setFirstName(resultSet.getString("first_name"));
p.setLastName(resultSet.getString("last_name"));
return p;
}
}
@Component("AddressResultRowMapper")
public class AddressResultRowMapper implements ResultRowMapper {
@Override
public List<String> getColumnList(){
return Arrays.asList("id", "dob", "place_of_birth", "address");
}
@Override
public Person mapRow(ResultSet resultSet, int i) throws SQLException {
Person p = new Person();
p.setId(resultSet.getLong("id"));
p.setAddress(resultSet.getString("address"));
p.setPlaceOfBirth(resultSet.getString("place_of_birth"));
return p;
}
}
然后在Spring Boot应用程序中,我将通过以下方式使用它:
@Slf4j
@SpringBootApplication
public class CustomRowMapperApplication implements ApplicationRunner {
@Autowired @Qualifier("NameResultRowMapper")
ResultRowMapper nameResultRowMapper;
@Autowired @Qualifier("DefaultResultRowMapper")
ResultRowMapper defaultResultRowMapper;
@Autowired @Qualifier("AddressResultRowMapper")
ResultRowMapper addressResultRowMapper;
@Autowired
JdbcTemplate jdbcTemplate;
public static void main(String[] args) {
new SpringApplication(CustomRowMapperApplication.class).run(args);
}
@Override
public void run(ApplicationArguments args) throws Exception {
List<Person> peopleWithName = jdbcTemplate.query("SELECT " + nameResultRowMapper.getColumnListAsString()
+ " FROM person", nameResultRowMapper);
log.info("People with name: {} ", peopleWithName);
List<Person> people = jdbcTemplate.query("SELECT " + defaultResultRowMapper.getColumnListAsString()
+ " FROM person", defaultResultRowMapper);
log.info("People with all detail: {} ", people);
List<Person> peopleWithAddress = jdbcTemplate.query("SELECT " + addressResultRowMapper.getColumnListAsString()
+ " FROM person", addressResultRowMapper);
log.info("People with address: {} ", peopleWithAddress);
}
}