@Override @SuppressWarnings("deprecation") public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) { RegisterBeanMapper registerBeanMapper = (RegisterBeanMapper) annotation; Class<?> beanClass = registerBeanMapper.value(); String prefix = registerBeanMapper.prefix(); RowMappers mappers = registry.get(RowMappers.class); if (prefix.isEmpty()) { mappers.register(BeanMapper.factory(beanClass)); } else { mappers.register(BeanMapper.factory(beanClass, prefix)); } }
/** * Returns a mapper for the given bean class * * @param <T> the type to find the mapper for * @param type the mapped class * @return a mapper for the given bean class */ public static <T> RowMapper<T> of(Class<T> type) { return BeanMapper.of(type, DEFAULT_PREFIX); }
/** * Returns a mapper for the given bean class * * @param <T> the type to find the mapper for * @param type the mapped class * @param prefix the column name prefix for each mapped bean property * @return a mapper for the given bean class */ public static <T> RowMapper<T> of(Class<T> type, String prefix) { return new BeanMapper<>(type, prefix); }
Person person = session.load(...); BeanMapper mapper = ...; Person cleanPerson = mapper.map(person, Person.class);
private Optional<PropertyDescriptor> descriptorForColumn(String columnName, List<ColumnNameMatcher> columnNameMatchers) { for (PropertyDescriptor descriptor : info.getPropertyDescriptors()) { String paramName = paramName(descriptor); for (ColumnNameMatcher strategy : columnNameMatchers) { if (strategy.columnNameMatches(columnName, paramName)) { return Optional.of(descriptor); } } } return Optional.empty(); }
descriptorByColumnCache.computeIfAbsent(name, n -> descriptorForColumn(n, columnNameMatchers));
/** * Returns a mapper factory that maps to the given bean class * * @param type the mapped class * @return a mapper factory that maps to the given bean class */ public static RowMapperFactory factory(Class<?> type) { return RowMapperFactory.of(type, BeanMapper.of(type)); }
@Override protected BeanMapper<?> createNestedMapper(StatementContext ctx, PojoProperty<T> property, String nestedPrefix) { return new BeanMapper<>(GenericTypes.getErasedType(property.getQualifiedType().getType()), nestedPrefix); } }
@Before public void setUp() throws Exception { assumeFalse(BindDao.class.getMethod("getByIdImplicitBindPositional", int.class).getParameters()[0].isNamePresent()); h = dbRule.getSharedHandle(); h.registerRowMapper(BeanMapper.factory(Something.class)); h.execute("insert into something (id, name) values (1, 'Elsie Hughes')"); }
/** * Returns a mapper factory that maps to the given bean class * * @param type the mapped class * @param prefix the column name prefix for each mapped bean property * @return a mapper factory that maps to the given bean class */ public static RowMapperFactory factory(Class<?> type, String prefix) { return RowMapperFactory.of(type, BeanMapper.of(type, prefix)); }
/** * Returns a mapper for the given bean class * * @param <T> the type to find the mapper for * @param type the mapped class * @param prefix the column name prefix for each mapped bean property * @return a mapper for the given bean class */ public static <T> RowMapper<T> of(Class<T> type, String prefix) { return new BeanMapper<>(type, prefix); }
@Test public void testColumnNameAnnotation() { Handle handle = dbRule.getSharedHandle(); handle.registerRowMapper(BeanMapper.factory(ColumnNameBean.class)); handle.execute("insert into something (id, name) values (1, 'foo')"); ColumnNameBean bean = handle.createQuery("select * from something") .mapTo(ColumnNameBean.class) .findOnly(); assertThat(bean.getI()).isEqualTo(1); assertThat(bean.getS()).isEqualTo("foo"); }
/** * Maps this result set to a {@link ResultIterable} of the given element type, using {@link BeanMapper}. * * @param type the bean type to map the result set rows to * @param <T> the bean type to map the result set rows to * @return a {@link ResultIterable} of the given type. */ @SuppressWarnings("deprecation") default <T> ResultIterable<T> mapToBean(Class<T> type) { return map(BeanMapper.of(type)); }
@Test public void beanMapper() { // tag::beanMapper[] handle.registerRowMapper(BeanMapper.factory(UserBean.class)); List<UserBean> users = handle .createQuery("select id, name from user") .mapTo(UserBean.class) .list(); // end::beanMapper[] assertThat(users).extracting("name").contains("Alice", "Bob", "Charlie", "Data"); }
@Test public void shouldSetValuesInSuperClassProperties() throws Exception { mockColumns("longField", "blongField"); Long aLongVal = 100L; Long bLongVal = 200L; when(resultSet.getLong(1)).thenReturn(aLongVal); when(resultSet.getLong(2)).thenReturn(bLongVal); when(resultSet.wasNull()).thenReturn(false); RowMapper<DerivedBean> mapper = BeanMapper.of(DerivedBean.class); DerivedBean derivedBean = mapper.map(resultSet, ctx); assertThat(derivedBean.getLongField()).isEqualTo(aLongVal); assertThat(derivedBean.getBlongField()).isEqualTo(bLongVal); }
@Test public void testNestedNotReturned() { Handle handle = dbRule.getSharedHandle(); handle.registerRowMapper(BeanMapper.factory(NestedBean.class)); assertThat(handle .createQuery("select 42 as testValue") .mapTo(NestedBean.class) .findOnly()) .extracting("testValue", "nested") .containsExactly(42, null); }
/** * Returns a mapper for the given bean class * * @param <T> the type to find the mapper for * @param type the mapped class * @return a mapper for the given bean class */ public static <T> RowMapper<T> of(Class<T> type) { return BeanMapper.of(type, DEFAULT_PREFIX); }
@Test public void testNestedPrefixNotReturned() { Handle handle = dbRule.getSharedHandle(); handle.registerRowMapper(BeanMapper.factory(NestedPrefixBean.class)); assertThat(handle .createQuery("select 42 as integerValue") .mapTo(NestedPrefixBean.class) .findOnly()) .extracting("integerValue", "nested") .containsExactly(42, null); }
/** * Returns a mapper factory that maps to the given bean class * * @param type the mapped class * @return a mapper factory that maps to the given bean class */ public static RowMapperFactory factory(Class<?> type) { return RowMapperFactory.of(type, BeanMapper.of(type)); }