例如,这两个表之间应该有一个中间表
<class name="pojo.Person" table="person">
<id column="p_id" name="personID">
<generator class="increment" />
</id>
<property name="personName" column="p_name" />
<set name="addressSet" table="map">
<key column="p_id" />
<many-to-many class="pojo.Address" column="a_id" />
</set>
</class>
<class name="pojo.map" table="map">
<composite-id>
<key-property name="person_ID" column="p_id"/>
<key-property name="address_ID" column="a_id"/>
</composite-id>
<property name="person_ID" type="java.lang.Long" insert="false" update="false">
<column name="p_id"/>
</property>
<property name="address_ID" type="java.lang.Long" insert="false" update="false">
<column name="a_id"/>
</property>
</class>
<class name="pojo.Address" table="address">
<id column="a_id" name="addressID">
<generator class="assigned"/>
</id>
<property name="address" column="address" />
</class>
并有一个用于映射器的 bean 类
public class map{
private long person_ID;
private long address_ID;
public long getPerson_ID() {
return person_ID;
}
public void setPerson_ID(long person_ID) {
this.person_ID= person_ID;
}
public long getAddress_ID() {
return address_ID;
}
public void setAddress_ID(long address_ID) {
this.address_ID= address_ID;
}
}
在这里
<set name="addressSet" table="map">
<key column="p_id" />
<many-to-many class="pojo.Address" column="a_id" />
</set>
关键列
<key column="p_id" />
表示 person 表中的“id”列和
<many-to-many class="pojo.Address" column="a_id" />
此列是地址表中的“id”列