5

私は豆のクラスを持っています

public class Group{string name;Type type; }

そして別の豆

public class Type{String name;}

今、jdbi @BindBean を使用してグループをバインドしたい

@SqlBatch("INSERT INTO (type_id,name) VALUES((SELECT id FROM type WHERE name=:m.type.name),:m.name)")
@BatchChunkSize(100)
int[] insertRewardGroup(@BindBean ("m") Set<Group> groups);

ユーザー定義オブジェクトのプロパティを Bean のメンバーとしてバインドするにはどうすればよいですか??

4

2 に答える 2

4

ここで独自の Bind アノテーションを実装できます。この回答に採用しているものを実装しました。すべてのものをアンラップしTypeます。

もう少し作業をすれば、完全に汎用的にできると思います。

m.type.nameコードは次のようになります ( に変更されていることに注意してくださいm.type)。

@SqlBatch("INSERT ... WHERE name=:m.type),:m.name)")
@BatchChunkSize(100)
int[] insertRewardGroup(@BindTypeBean ("m") Set<Group> groups);

これは注釈になります:

@BindingAnnotation(BindTypeBean.SomethingBinderFactory.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface BindTypeBean {
    String value() default "___jdbi_bare___";

    public static class SomethingBinderFactory implements BinderFactory {
        public Binder build(Annotation annotation) {
            return new Binder<BindTypeBean, Object>() {
                public void bind(SQLStatement q, BindTypeBean bind, Object arg) {
                    final String prefix;
                    if ("___jdbi_bare___".equals(bind.value())) {
                        prefix = "";
                    } else {
                        prefix = bind.value() + ".";
                    }

                    try {
                        BeanInfo infos = Introspector.getBeanInfo(arg.getClass());
                        PropertyDescriptor[] props = infos.getPropertyDescriptors();
                        for (PropertyDescriptor prop : props) {
                            Method readMethod = prop.getReadMethod();
                            if (readMethod != null) {
                                Object r = readMethod.invoke(arg);
                                Class<?> c = readMethod.getReturnType();
                                if (prop.getName().equals("type") && r instanceof Type) {
                                    r = ((Type) r).getType();
                                    c = r.getClass();
                                }
                                q.dynamicBind(c, prefix + prop.getName(), r);
                            }
                        }
                    } catch (Exception e) {
                        throw new IllegalStateException("unable to bind bean properties", e);
                    }


                }
            };
        }
    }
}
于 2015-06-29T22:39:04.747 に答える
1

JDBI でこれを行うことはできません。プロパティを取り出して引数にする必要があります。

于 2015-06-25T04:02:08.963 に答える