关联映射主要分为 一对一、一对多、多对多,下面学习一对一
在学习resultMap元素时,包含了一个association子元素,这个元素就是用来处理一对一关联关系的
下面通过一个案例来学习一下
1.建立数据表
create table tb_idcard(
id int primary key auto_increment,
code varchar(18)
);
insert into tb_idcard(code)
values('152221198711020624'),('152221198804058713');
create table tb_person(
id int primary key auto_increment,
name varchar(32),
age int,
sex varchar(8),
card_id int unique,
foreign key(card_id) references tb_idcard(id)
);
insert into tb_person(name,age,sex,card_id)
values('rose',29,'女',1);
insert into tb_person(name,age,sex,card_id)
values('tom',25,'男',2);
2.创建项目chapter09,并将所需jar包以及MybatisUtils工具类以及核心配置文件mybatis—config.xml赋值粘贴过来
package com.itheima.po;
public class IdCard {
private Integer id;
private String code;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Override
public String toString() {
return "IdCard [id="+id+",code="+code+"]";
}
}
package com.itheima.po;
public class Person {
private Integer id;
private String name;
private Integer age;
private String sex;
private IdCard card;
public Integer getId(