一对一查询
每一个雇员都有唯一的身份证号
一: resultType=“map”
//接口
public Map<String, Object> oneToOne(int id);
<!--xml-->
<select id="oneToOne" resultType="map">
select last_name,number from jobnum j,employee e where e.jobid=j.id AND j.id=#{id}
</select>
//测试代码
Map<String,Object> map=dao.oneToOne(1);
System.out.println(map);
//输出结果{number=2016512981, last_name=wang}
二: resultType=“employee+jobnum” (不推荐)
三:级联属性(把jobnum作为一个新字段包括在Employess这个类里面)
//接口
public Employee oneToOne(int id);
<!-- 一对一连表关联-->
<select id="oneToOne" resultMap="jobnum">
select * from jobnum j,employee e where e.jobid=j.id AND j.id=#{id}
</select>
<resultMap id="jobnum" type="pojo.Employee" autoMapping="true">
<!--一对一映射时,对象成员使用association-->
<!--property映射的对象成员-->
<association property="jobNum" javaType="pojo.JobNum" autoMapping="true">
</association>
</resultMap>
//测试代码
Employee employee=dao.oneToOne(1);
System.out.println(employee);
//Employee{id=1, email='1', gender='woman', did=1, last_name='wang'}
一对多关联
多个人对应相同的薪水
<!--一对多查询-->
<select id="querySalaryAndEmployee" resultMap="qsae">
SELECT * FROM salary s, employee e WHERE e.sid = s.id AND s.id=#{id}
</select>
<resultMap id="qsae" type="pojo.Salary" autoMapping="true">
<collection property="list" ofType="pojo.Employee" autoMapping="true"></collection>
</resultMap>
//接口
public List<Salary> querySalaryAndEmployee(int id);
List<Salary> s=new ArrayList<> ();
s= (List<Salary>) dao.querySalaryAndEmployee(1);
System.out.println(s);
//[Salary{id=1, grade='1', money=5000.0, bonus='500', list=[Employee{id=1, email='1', gender='woman', did=1, last_name='wang', department=null, salary=null, jobNum=null}]}, Salary{id=1, grade='1', money=5000.0, bonus='500', list=[Employee{id=1, email='1', gender='woman', did=1, last_name='zhao', department=null, salary=null, jobNum=null}]}]