温故而知新,说道mybatis的一对多和一对一就要说道两个 标签,collection标签用来实现一对多,association则用来实现一对一,而这两种方式实际上都涉及到两种查询分别是联合查询和嵌套子查询,所谓联合查询的意思就是对数据库进行一次查询,查出所有数据,而嵌套查询则是要先查出前面的‘’一‘’,再根据‘’一‘’中的某个属性去另一张表的一或者多。
private Integer id;
private String name;
private String sex;
private StudentCard studentCard;
private List<NameShort> nameShorts;
private int id;
private int studentId;
private Date startDate;
private Date endDate;
name与学生表里的name一直,是关联的条件
private String name;
private String shortName;
<resultMap id="re" type="com.example.mybatisdemo01.demo01.entiry.Student">
<result column="id" property="id">result>
<result column="name" property="name">result>
<result column="sex" property="sex">result>
<association property="studentCard" select="com.example.mybatisdemo01.demo01.dao.StudentIdMapper.selectOne" column="id">
association>
<collection property="nameShorts" select="com.example.mybatisdemo01.demo01.dao.NameShortMapper.listNameShorts" column="name">collection>
resultMap>
<select id="listStudents" resultMap="re">
SELECT * FROM Student
select>
<select id="selectOne" resultType="com.example.mybatisdemo01.demo01.entiry.StudentCard" parameterType="int">
SELECT * FROM Studentcard where studentId=#{id}
select>
<select id="listNameShorts" resultType="com.example.mybatisdemo01.demo01.entiry.NameShort" >
SELECT * FROM NameShort where name=#{name}
select>
意思是一次查询所有的结果
<resultMap id="re02" type="com.example.mybatisdemo01.demo01.entiry.Student">
<result column="id" property="id">result>
<result column="name" property="name">result>
<result column="sex" property="sex">result>
<association property="studentCard" select="com.example.mybatisdemo01.demo01.dao.StudentIdMapper.selectOne" column="id" >
association>
<collection property="nameShorts" javaType="java.util.List" ofType="com.example.mybatisdemo01.demo01.entiry.NameShort" >
<result column="a" property="name">result>
<result column="na" property="shortName">result>
collection>
resultMap>
<select id="listStudentsOnce" resultMap="re02">
SELECT s.* ,n.name a,n.shortName na FROM Student s,nameShort n where s.name=n.name;
select>
初入门径,如有错误,感谢指出。