映射set集合
set集合存放不重复的数据。
class A{//存放图片,图片名不能重复,没有图片类
private Set images=new HashSet();
}
image表
A_id
imagename
<set name="images" table="image" lazy="true">
<key column="A_id">
<element column="imagename" type="string">
</set>
映射Bag包
Bag集合中不按特定方式排序,当允许有重复对象。Java中没有Bag接口,所以用List模拟。class A{//存放图片,图片名字可以重复,没有图片类
private List images = new ArrayList();
}
image表
id
A_id
imagename
<idbag name="images" table="image" lazy="true">
<collection-id type="long" column="id" >//映射image表的主键
<generator class="increment">
</collection-id>
<key column="A_id">
<element column="imagename" type="string">
</idbag>
映射List集合
可以再image表中加一个索引字段,那么images集合可以按照索引位置排序。
<list name="images" table="image" lazy="treu">
<key column="A_id">
<list-index column="索引字段">
<element column="imagename" type="string">
</list>
映射Map集合
class A{//存放图片,图片名字可以重复,但是保存的键不能重复,没有图片类
private Map images = new HashMap();
}
image表
A_id
imagename
filename
<map name="images" table="image" lazy="true">
<key column="A_id"/>
<map-key column="imagename" type="string"/>
<element column="filename" type="string"/>
</map>
对集合进行排序
order-by属性(数据库排序):<set>支持,<idbag>支持,<list>不支持,<map>支持
sort属性(内存排序):<set>支持,<idbag>不支持,<list>不支持,<map>支持
<set name="" table="" order-by="字段 desc" >
....
</set>
sort="natural"表示自然排序。sort="自定义排序类"。
映射组件类型集合
image有类,但是没有OID,是一种组件类型,也是值类型。上面的例子没有类。
映射文件如下
<set name="images" table="image" lazy="true">//image表没有主键
<key column="A_id">
<composite-element class="图片类">
<parent name="父类字段">
<property >
<property >
<property >
</composite-element>
</set>