/** * Associate the specified Getter for the specified property. * @param key the property * @param getter the getter * @return the current factory */ public JdbcMapperFactory addCustomGetter(String key, Getter<ResultSet, ?> getter) { return addColumnDefinition(key, FieldMapperColumnDefinition.<JdbcColumnKey>customGetter(getter)); }
/** * Associate the specified Getter for the specified property. * @param key the property * @param getter the getter * @return the current factory */ public JdbcMapperFactory addCustomGetter(String key, Getter<ResultSet, ?> getter) { return addColumnDefinition(key, FieldMapperColumnDefinition.<JdbcColumnKey>customGetter(getter)); }
@Test public void testMapInnerObjectWithColumnDefinition() throws Exception { JdbcMapperBuilder<Db1DeepObject> builder = JdbcMapperFactoryHelper.asm().newBuilder(Db1DeepObject.class); FieldMapperColumnDefinition<JdbcColumnKey> columnDefinition = FieldMapperColumnDefinition.customGetter(new Getter<ResultSet, String>() { @Override public String get(ResultSet target) throws Exception { return "ov1"; } }); builder.addMapping("db_object_name", columnDefinition); final JdbcMapper<Db1DeepObject> mapper = builder.mapper(); ResultSet rs = mock(ResultSet.class); when(rs.getString(1)).thenReturn("name1"); when(rs.next()).thenReturn(true, false); Db1DeepObject next = mapper.iterator(rs).next(); assertEquals("ov1", next.getDbObject().getName()); }
@Test public void testAsmFactoryJdbcMapperCache() throws Exception { SourceMapper<ResultSet, DbObject> mapper1 = getSubMapper(JdbcMapperFactoryHelper.asm().newBuilder(DbObject.class).addMapping("id").addMapping("name").mapper()); SourceMapper<ResultSet, DbObject> mapper2 = getSubMapper(JdbcMapperFactoryHelper.asm().newBuilder(DbObject.class).addMapping("id").addMapping("name").mapper()); final FieldMapperColumnDefinition<JdbcColumnKey> columnDefinition = FieldMapperColumnDefinition.customGetter(new StaticLongGetter<ResultSet>(3)); SourceMapper<ResultSet, DbObject> mapper3 = getSubMapper( JdbcMapperFactoryHelper.asm().fieldMapperErrorHandler(new LogFieldMapperErrorHandler<JdbcColumnKey>()).newBuilder(DbObject.class).addMapping("id", columnDefinition).addMapping("name") .mapper()); assertNotSame(mapper1, mapper2); assertSame(mapper1.getClass(), mapper2.getClass()); assertNotSame(mapper1.getClass(), mapper3.getClass()); assertTrue(mapper1.getClass().getSimpleName().startsWith("AsmMapperFrom")); assertTrue(mapper2.getClass().getSimpleName().startsWith("AsmMapperFrom")); assertTrue(mapper3.getClass().getSimpleName().startsWith("AsmMapperFrom")); }