已经有orbroker做过好几个小东西了,前几天在一套旧系统上做了些小花絮,实战了一把orbroker的one-to-one了。
表结构大致如下
CREATE TABLE T_INFO(
CONTENT_ID VARCHAR2(50) NOT NULL,
S_TITLE VARCHAR2(300),
S_CONTENT VARCHAR2(4000),
S_USERNAME VARCHAR2(20),
S_USERADDRESS VARCHAR2(200),
S_USEREMAIL VARCHAR2(50),
S_USERTELPHONE VARCHAR2(20)
);
据此,我定义了两个类
public class Info {
private String id;
private String title;
private String content;
/**
* 发布者
*/
private Publisher publisher;
}
public class Publisher {
private String name;
private String address;
private String email;
private String telphone;
}
以findByPrimeryKey这个方法为例,在orbroker.xml中做了条配置
<sql-statement id="findByPrimeryKey" result-object="Info" external-source="/orbroker/findByPrimeryKey.sql" />
findByPrimeryKey.sql的内容:
SELECT t.content_id AS id,
t.s_title AS title,
t.s_content AS content,
t.s_username AS userName,
t.s_useraddress AS userAddress,
t.s_useremail AS userEmail,
t.s_usertelphone AS userTelphone
FROM t_info t
WHERE 1 = 1
<#if id?exists>
AND t.content_id = :id
</#if>
one-to-one的处理,主要功夫还是在orbroker.xml定义Result-Object上面,参照orbroker user-guide中“Mapping associations”一部分,我做了这样的配置
<result-object id="Info" class="domain.Info" key-columns="id">
<property name="id">
<column name="id" />
</property>
<property name="title">
<column name="title" />
</property>
<property name="content">
<column name="content" />
</property>
<property name="publisher">
<map-with result-object="Publisher" />
</property>
</result-object>
<result-object id="Publisher" class="domain.Publisher" key-columns="id">
<property name="name">
<column name="userName" />
</property>
<property name="email">
<column name="userEmail" />
</property>
<property name="address">
<column name="userAddress" />
</property>
<property name="telphone">
<column name="userTelphone" />
</property>
</result-object>
测试通过,哈哈!