直接粗略总结项目中的应用,日后慢慢修改补充。
set集合配置:
场景1:一个firestation包含若干个region,region为简单的字符串类型。
在firestation的model中需要声明一个set集合:
/**
* @辖区坐标范围
*/
private Set<String> regions;
firestation的hbm.xml配置如下:
<set table="t_fire_mfs_region" order-by="s_station_id" name="regions"
lazy="false" cascade="save-update">
<key column="s_station_id" />
<element type="java.lang.String" column="s_region" />
</set>
其中table值是region的model对应的表,name就是firestation的model中声明的set变量名称。
key标签中的column是region表对应firestation表的外键名称,element是region表中的其他字段,可以写若干个。
至此配置完毕。
场景2:一个配置类configs包含多个条目configsitem
configs的model中声明一个configitem的set:
/**
* @配置项集合
*/
private Set<ConfigsItem> appSettings;
configs.hbm.xml配置:
<set name="appSettings" table="t_configs_item" cascade="all" lazy="false">
<key column="s_cfg_id" />
<composite-element class="com.fire.models.base.ConfigsItem">
<property name="name" column="s_name" type="java.lang.String" />
<property name="key" column="s_key" type="java.lang.String" />
<property name="value" column="s_value" type="java.lang.String" />
<property name="sequence" column="n_sequence" type="java.lang.Integer" />
</composite-element>
</set>
别的不说了,注意key标签的column的值是configitem中的列。
list集合配置:
场景:一个firestation包含若干个人员,每个人员都是一个自定义对象。
/**
* @站点人员列表
*/
private List<MFSPerson> stationpersons;
需要对人员建表,人员和station需要配置多对一映射。
firestation的hbm.xml配置如下:
<list name="stationpersons" table="t_mfsperson" cascade="all" lazy="false">
<key column="s_station_id"></key>
<index column="idx" type="int"></index>
<one-to-many class="com.fire.models.fire.MFSPerson" />
</list>
name的值就是我们firestation的model中声明的list变量名称。
table指向人员表,注意加上lazy="false"防止查询时报错。
key标签的column值是人员表的外键,此外键对应的是firestation表。
<!-- index代表key,element(或one-to-many)代表value-->
(注意:idx和s_station_id都是人员表的列)
one to many标签的class值是人员的model。
此外在人员model中声明一个firestation对象,并写上getset方法:
/**
* @对应消防站
*/
private MicroFireStation station;
在人员的hbm.xml中如下配置:
<many-to-one name="station"
column="s_station_id"
class="com.fire.models.fire.MicroFireStation"
cascade="none"
fetch="join">
</many-to-one>
不解释了,都很简单,至此配置完毕。