IDCard
package domain;
public class IDCard {
//自有属性
private String cardid;
private String address;
//为了根据身份证号查询 身份证和人的信息
//多添加一个关联属性
private Person person;
public IDCard() {}
public IDCard(String cardid, String address, Person person) {
this.cardid = cardid;
this.address = address;
this.person = person;
}
@Override
public String toString() {
return "IDCard{" +
"cardid='" + cardid + '\'' +
", address='" + address + '\'' +
", person=" + person +
'}';
}
public String getCardid() {
return cardid;
}
public void setCardid(String cardid) {
this.cardid = cardid;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
Person
package domain;
public class Person {
//自有属性
private Integer pid;
private String pname;
//关联属性 身份证号(身份证对象中的一个属性)
private IDCard idcard;
public Person() {}
public Person(Integer pid, String pname, IDCard idcard) {
this.pid = pid;
this.pname = pname;
this.idcard = idcard;
}
@Override
public String toString() {
return "Person{" +
"pid=" + pid +
", pname='" + pname + '\'' +
", idcard=" + idcard +
'}';
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public IDCard getIdcard() {
return idcard;
}
public void setIdcard(IDCard idcard) {
this.idcard = idcard;
}
}
PersonService
public class PersonService {
private PersonDao dao = new PersonDao();
public Person selectOne(Integer pid){
return dao.selectOne(pid);
}
}
PersonDao
public class PersonDao {
private SqlSession sqlSession = MyUtil.getSqlSession(true);
//设计一个方法 根据一个人的pid 查询人+身份证的所有信息
public Person selectOne(Integer pid){
return sqlSession.selectOne("selectOne",pid);
}
}
PersonMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.PersonDao">
<!--自己定义一个赋值的规则 id是整个赋值规则的名字 -->
<resultMap id="selectPerson" type="domain.Person">
<id property="pid" column="pid"></id>
<result property="pname" column="pname"></result>
<association property="idcard" javaType="domain.IDCard" select="selectIDCardForPerson" column="cardid"></association>
</resultMap>
<!--这个select标签是为了Person对象中的那个idcard属性再次查询用的-->
<select id="selectIDCardForPerson" resultType="domain.IDCard">
select * from idcard where cardid = #{cardid}
</select>
<!--=======================================================================-->
<!--dao调用的是下面这个标签对应的语句-->
<select id="selectOne" resultMap="selectPerson">
select pid,pname,cardid from person where pid = #{pid}
</select>
</mapper>
网友评论