一、Idcard类
1-1:
package com.springmvc.pojo;
/**
* @author YuusukeUchiha
* @date 2020/8/1 19:40
* @ide IntelliJ IDEA
*/
public class Idcard {
private int id;
private String cno;
@Override
public String toString() {
return "Idcard{" +
"id=" + id +
", cno='" + cno + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCno() {
return cno;
}
public void setCno(String cno) {
this.cno = cno;
}
}
1-2:
package com.springmvc.mapper;
import com.springmvc.pojo.Idcard;
/**
* @author YuusukeUchiha
* @date 2020/7/23 22:54
* @ide IntelliJ IDEA
*/
public interface IdcardMapper {
Idcard findIdcardById(int id); // 根据编号查询用户信息
}
1-3:
<?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="com.springmvc.mapper.IdcardMapper">
<!-- 根据id查询身份证信息-->
<select id="findIdcardById" parameterType="int" resultType="Idcard">
select * from idcard where id = #{id}
</select>
</mapper>
1-4:
// @Test
public void testFindIdcardById() {
// 获得UserInfoMapper接口的代理对象
IdcardMapper idcardMapper = sqlSession.getMapper(IdcardMapper.class);
// 直接调用接口的方法,查询编号为1的UserInfo对象
Idcard idcard = idcardMapper.findIdcardById(1);
// 打印输出结果
System.out.println(idcard .toString());
}
二、Person类
2-1:
package com.springmvc.pojo;
/**
* @author YuusukeUchiha
* @date 2020/8/1 19:41
* @ide IntelliJ IDEA
*/
public class Person {
private int id;
private String name;
private int age;
private String sex;
// 关联属性
private Idcard idcard;
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", idcard=" + idcard +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Idcard getIdcard() {
return idcard;
}
public void setIdcard(Idcard idcard) {
this.idcard = idcard;
}
}
2-2:
package com.springmvc.mapper;
import com.springmvc.pojo.Person;
/**
* @author YuusukeUchiha
* @date 2020/7/23 22:54
* @ide IntelliJ IDEA
*/
public interface PersonMapper {
Person findPersonById(int id); // 根据编号查询用户信息
}
2-3:
<?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="com.springmvc.mapper.PersonMapper">
<!-- 根据id查询身份证信息-->
<!-- <select id="findPersonById" parameterType="int" resultType="Person">-->
<select id="findPersonById" parameterType="int" resultMap="personMap">
select * from person where id = #{id}
</select>
<resultMap id="personMap" type="Person">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="age" column="age" />
<result property="sex" column="sex"/>
<!-- 一对一关联映射 -->
<association property="idcard" column="cid"
select="com.springmvc.mapper.IdcardMapper.findIdcardById"
javaType="Idcard"
/>
</resultMap>
</mapper>
2-4:
@Test
public void testFindPersonById() {
// 获得UserInfoMapper接口的代理对象
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
// 直接调用接口的方法,查询编号为1的UserInfo对象
Person person = personMapper.findPersonById(1);
// 打印输出结果
System.out.println(person .toString());
}