Let BeanPropertyRowMapper subclasses customize mapped names
Closes gh-32199
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,6 +16,12 @@
|
||||
|
||||
package org.springframework.jdbc.core;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.util.Date;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
@@ -155,6 +161,16 @@ class BeanPropertyRowMapperTests extends AbstractRowMapperTests {
|
||||
mock.verifyClosed();
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryWithCustomNameMatchOnBirthDate() throws Exception {
|
||||
Mock mock = new Mock(MockType.FOUR);
|
||||
Person person = mock.getJdbcTemplate().queryForObject(
|
||||
"select name, age, birthdate, balance from people",
|
||||
new CustomBeanPropertyRowMapper());
|
||||
verifyPerson(person);
|
||||
mock.verifyClosed();
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryWithUnderscoreInColumnNameAndPersonWithMultipleAdjacentUppercaseLettersInPropertyName() throws Exception {
|
||||
Mock mock = new Mock();
|
||||
@@ -179,4 +195,36 @@ class BeanPropertyRowMapperTests extends AbstractRowMapperTests {
|
||||
assertThat(mapper.underscoreName(input)).isEqualTo(expected);
|
||||
}
|
||||
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@interface MyColumnName {
|
||||
|
||||
String value();
|
||||
}
|
||||
|
||||
private static class CustomPerson extends Person {
|
||||
|
||||
@MyColumnName("birthdate")
|
||||
public void setBirth_date(Date date) {
|
||||
super.setBirth_date(date);
|
||||
}
|
||||
}
|
||||
|
||||
private static class CustomBeanPropertyRowMapper extends BeanPropertyRowMapper<CustomPerson> {
|
||||
|
||||
public CustomBeanPropertyRowMapper() {
|
||||
super(CustomPerson.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Set<String> mappedNames(PropertyDescriptor pd) {
|
||||
Set<String> mappedNames = super.mappedNames(pd);
|
||||
MyColumnName customName = pd.getWriteMethod().getAnnotation(MyColumnName.class);
|
||||
if (customName != null) {
|
||||
mappedNames.add(customName.value());
|
||||
}
|
||||
return mappedNames;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user