实体类:
Emp 类:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Emp {
private int id;
private String lastname;
private String email;
private int gender;
private int did;
private Dept dept;
private Date birth = new Date();
}
Dept类:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dept {
private int id;
private String dname;
}
Mapper接口
EmpMapper:
//这个注解表示这是一个mybatis的mapper类
@Mapper
@Repository
public interface EmpMapper {
void addEmp(Emp emp);
void deleteEmp(int id);
void updateEmp(Emp emp);
Emp queryEmpById(int id);
List<Emp> queryEmpList();
}
DeptMapper:
@Mapper
@Repository
public interface DeptMapper {
List<Dept> queryDeptList(@Param("cid") int cid);
}
EmpMapper.xml 配置文件
<mapper namespace="com.acoffee.mapper.EmpMapper">
<resultMap id="EmpMap" type="Emp">
<id column="id" property="id"></id>
<id column="lastname" property="lastname"></id>
<id column="email" property="email"></id>
<id column="gender" property="gender"></id>
<association property="dept" select="com.acoffee.mapper.DeptMapper.queryDeptList" column="did"></association>
</resultMap>
<select id="queryEmpList" resultMap="EmpMap">
select * from emp_dept.employees
</select>
</mapper>
DeptMapper.xml配置文件
<mapper namespace="com.acoffee.mapper.DeptMapper">
<select id="queryDeptList" resultType="Dept">
select * from emp_dept.department where id = #{id};
</select>
</mapper>
前端页面(部分)
<tr th:each="emp:${emps}">
<td th:text="${emp.getId()}"></td>
<td th:text="${emp.getLastname()}"></td>
<td th:text="${emp.getEmail()}"></td>
<td th:text="${emp.getGender()==0?'女':'男'}"></td>
<td th:text="${emp.dept.getDname()}"></td>
<td th:text="${#dates.format(emp.getBirth(),'yyyy-MM-dd HH:mm:ss')}"></td>
<td>
<a class="btn btn-sm btn-primary" th:href="@{/emp/{id}/(id=${emp.getId()})}">编辑</a>
<a class="btn btn-sm btn-danger" th:href="@{/delemp/{id}/(id=${emp.getId()})}">删除</a>
</td>
</tr>
查询结果:
上述我们采用的是分步查询。
我们下面使用association嵌套映射
其实这里查询会出现一个奇怪的事情,
因为我们现在两个表中都有id这个字段,所以我们在映射时使用<result property="id" column="id"></result>
去查找部门的id时就发现查出来的是员工的id,就是因为员工的id与部门的id字段名重名了?
EmpMapper:
<mapper namespace="com.acoffee.mapper.EmpMapper">
<resultMap id="EmpMap" type="Emp">
<id column="id" property="id"></id>
<id column="lastname" property="lastname"></id>
<id column="email" property="email"></id>
<id column="gender" property="gender"></id>
<association property="dept" javaType="com.acoffee.pojo.Dept">
<result property="id" column="id"></result>
<result property="dname" column="dname"></result>
</association>
</resultMap>
<select id="queryEmpList" resultMap="EmpMap">
select emp.*,dept.* from emp_dept.employees emp,emp_dept.department dept where emp.did = dept.id
</select>
</mapper>
上述执行结果如下
我们发现是员工id,我们现在将数据库中部门表的id改为pid
修改配置文件以及实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dept {
private int pid;
private String dname;
}
<mapper namespace="com.acoffee.mapper.EmpMapper">
<resultMap id="EmpMap" type="Emp">
<id column="id" property="id"></id>
<id column="lastname" property="lastname"></id>
<id column="email" property="email"></id>
<id column="gender" property="gender"></id>
<association property="dept" javaType="com.acoffee.pojo.Dept">
<result property="pid" column="pid"></result>
<result property="dname" column="dname"></result>
</association>
</resultMap>
<select id="queryEmpList" resultMap="EmpMap">
select emp.*,dept.* from emp_dept.employees emp,emp_dept.department dept where emp.did = dept.pid
</select>
</mapper>
执行结果:
我们就发现查询的就是部门的id了
但是针对上面这个问题我们将部门的id字段名改为did (did在员工表中也存在) 此时我们再去查询又发现还是可以把部门id查询出来
这个问题的原因就是mybatis的字段冲突了,我们写id的时候本来它就会根据前者去查,但是当我们写did的时候,本来查的就是一个东西所以没问题,而我们将字段改成pid就是将字段名区分开了,所以也没问题。
但是我们一般处理这个问题(字段名一样),我们都是在通过sql语句去查询的时候取别名来解决的。