Java レイヤーがストアド プロシージャを使用して DB と通信するようにします。ストアド プロシージャは互換性レイヤーとして機能するため、同じデータベース上で 2 つの異なるスキーマを想定して、2 つの異なるバージョンのアプリケーションを実行できます。
そのために、Orika を使用して、JDBC ResultSet から Bean にすばやくマップしたいと考えました。
これまでにこのテスト コードを作成しました。 @Test public void testSelectAndMap() throws Exception { Assert.assertNotNull(dataSource); try (接続 con = dataSource.getConnection()) { try(Statement stmt = con.createStatement()) {
try (ResultSet result = stmt.executeQuery("select 1 as internalTestPojoId, CURRENT_TIMESTAMP as now")) {
result.next();
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
mapperFactory.classMap(ResultSet.class, InternalTestPojo.class)
.byDefault()
.field("internalTestPojoId:{getInt('internalTestPojoId')|getInt('internalTestPojoId')|type=" + int.class.getName() + "}", "internalTestPojoId")
.field("now:{getTimestamp('now')|getTimestamp('now')|type=" + Timestamp.class.getName() + "}", "now")
.register();
MapperFacade mapper = mapperFactory.getMapperFacade();
InternalTestPojo pojo = mapper.map(result, InternalTestPojo.class);
Assert.assertEquals(pojo.internalTestPojoId, 1);
Assert.assertEquals(pojo.now, new Timestamp(new Date().getTime() / 1000 * 1000));
}
これは高速でうまく機能しますが、自分で ResultSet を Bean コードに書き込むよりもコーディングにそれほど時間はかかりません。しかし、マッピングを自動的に生成できれば、時間を大幅に節約できます。
私は見たIntrospectorPropertyResolver
。次のようなコードを書きました。
protected Property getProperty(java.lang.reflect.Type type, String expr,
boolean isNestedLookup, Property owner) throws MappingException {
Property property = null;
try {
property = super.getProperty(type, expr, isNestedLookup, null);
} catch (MappingException e) {
try {
property = super.resolveInlineProperty(type,
expr + ":{getInt('" + expr + "')|getInt('" + expr + "')|type=" + int.class);
} catch (MappingException subE) {
throw e; // throw the original exception
}
}
return property;
}
これはいいですね。Orika は自動的に Bean のプロパティ名を決定し、それを expr で提供します。しかし、それは私にタイプを教えてくれません。また、何にマップしようとしているのかもわかりません。この場合、ターゲットが ResultSet であるふりをする必要があります。
- データを入れようとしている expr の型を知るにはどうすればよいですか? の場合は
String
、インライン バインディング呼び出しResultSet.getString("expr")
を行い、Orika に を使用するように指示しますjava.lang.String
。タイムスタンプの場合は、インライン バインディング呼び出しResulset.getTimestamp("expr")
を行い、Orika に使用するように指示しますTimestamp
ResultSet
からへInternalTestPojo
、たとえばMap
からにマップしようとしていることをどのように知ることができInternalTestPojo
ますか?