多对一 配置信息
<many-to-one name="province" column="provincecode" class="com.qy.domain.Province" property-ref="code" cascade="save-update"/>
cascade="save-update" 级联保存更新
cascade="delete" 级联删除
casecade="all" save+update+delete
name表示当前类的属性名
column表示主表与从表关联的列名
class表示从表实例类
property-ref指定关联类的属性名,这个属性将会和本类的主键相对应。如果没有指定,会使用对方关联类的主键
当前主表实例类
public class City {
private int id;
private String code;
private String name;
// private String provincecode;
private Province province;//表示从表对象
public Province getProvince() {
return province;
}
public void setProvince(Province province) {
this.province = province;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
一对多配置信息
<set name="citySet" cascade="all">
<key column="provincecode" foreign-key="code" property-ref="code"></key>
<one-to-many class="com.qy.domain.City"></one-to-many>
</set>
name:City类中属性名province
column:city表中对应的列名
class:属性名province对应的对象类
如果通过主键关联不需要配置property-ref
如果通过非主键关联,需要配置property-ref:指向Province类中的参照的属性名
cascade="save-update" 级联保存更新
cascade="delete" 级联删除
casecade="all" save+update+delete
当前主表实例类
public class Province {
private int id;
private String code;
private String name;
private Set<City> citySet = new HashSet<>();//存储从表信息,因为从表有多个所以用set
public Set<City> getCitySet() {
return citySet;
}
public void setCitySet(Set<City> citySet) {
this.citySet = citySet;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Province() {
}
public Province(int id, String code, String name) {
this.id = id;
this.code = code;
this.name = name;
}
}