目录
在MoreMapper里面写入方法,通过员工ID来查询员工的信息以及部门的信息,员工表代表多的一方。
在文件中,namespace要写接口的唯一标识,即在包下的位置,然后将方法名复制过来,对应查询的id即可。
在实际开发中,根据业务需要,数据表之间往往会存在某种关联关系,例如:一对一,一对多,多对一,多对多等,当程序操作数据库时,如果被操作的表与其他表相关联,那么处理这些表中的数据时必须要考虑它们之间的关联关系。
为此,MyBatis提供了映射表之间关联关系的功能,如此一来,使用MyBatis能够以简洁的方式操作多张表。
在一对一关系中,一方数据表中的一条记录最多可以和另一方数据表中的一条记录相关。例如:人的角度:一个人只能有一张身份证,身份证角度:一张身份证只能属于一个人。
在一对多关系中,主键数据表中的一条记录可以和另外一张数据表的多条记录相关。例如:班级角度:一个班级可以有多名学生。学生角度:一个学生只能属于一个班级。
在多对一关系中,外键数据表的每一个数据可以和另外一张表的一条数据相对应。例如:
球员角度:多名球员属于一个球队。球队角度:一个球队里面的球员是唯一的(与一对多相反)
在多对多关系中,两个数据表里的每一条记录都可以和另外一张表里的每一条记录相关。例如:学生角度:一名学生由多名老师授课,老师角度:一名老师为多个学生授课。
现有emp员工表和dept部门表如下:
- package com.mybatis.mapper;
-
- import com.mybatis.pojo.Emp;
- import org.apache.ibatis.annotations.Param;
-
- import java.util.List;
-
- public interface MoreMapper {
-
- //通过员工Id来查询员工信息和部门信息
- List
selectEmpAndDeptByEmpId(@Param("empId") Integer empId); - }
- "1.0" encoding="UTF-8" ?>
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
"com.mybatis.mapper.MoreMapper"> -
-
-
-
3、多对一查询实现的三种方法
方法一、用表名.属性名
- "1.0" encoding="UTF-8" ?>
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.mybatis.mapper.MoreMapper">
- <resultMap id="selectOne" type="Emp">
- <id column="emp_id" property="empId">id>
- <result column="emp_name" property="empName">result>
- <result column="age" property="age">result>
- <result column="gender" property="gender">result>
- <result column="dept_id" property="dept.deptId">result>
- <result column="dept_name" property="dept.deptName">result>
- resultMap>
-
- <select id="selectEmpAndDeptByEmpId" resultMap="selectOne">
- select *from emp join dept on emp.emp_id = dept.dept_id
- where emp_id = #{empId}
- select>
-
- mapper>
运用resultMap来查询多个表,id标签代表查询的主键 ,
id里面的column写数据库的字段名,property写实体类对象的属性名。
方法一使用数据库里面的字段外键,属性名用点表示实体类里面的名字
方法一测试类MoreMapperTest
- import com.mybatis.SqlSessionUtil;
- import com.mybatis.mapper.MoreMapper;
- import com.mybatis.pojo.Emp;
- import org.apache.ibatis.session.SqlSession;
- import org.junit.Test;
-
- import java.util.List;
-
- public class MoreMapperText {
- @Test
- public void textSelectEmpAndDeptByEmpId(){
- SqlSession sqlSession = SqlSessionUtil.getSqlSession();
- MoreMapper mapper = sqlSession.getMapper(MoreMapper.class);
- List
emps = mapper.selectEmpAndDeptByEmpId(1); - System.out.println(emps);
- }
- }
测试运行:
方法一测试成功!!!
方法二、用association标签
- "1.0" encoding="UTF-8" ?>
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.mybatis.mapper.MoreMapper">
- <resultMap id="selectOne" type="Emp">
- <id column="emp_id" property="empId">id>
- <result column="emp_name" property="empName">result>
- <result column="age" property="age">result>
- <result column="gender" property="gender">result>
- <result column="dept_id" property="dept.deptId">result>
- <result column="dept_name" property="dept.deptName">result>
- resultMap>
-
- <resultMap id="selectTwo" type="Emp">
- <id column="emp_id" property="empId">id>
- <result column="emp_name" property="empName">result>
- <result column="age" property="age">result>
- <result column="gender" property="gender">result>
- <association property="dept" javaType="Dept">
- <id column="dept_id" property="deptId">id>
- <result column="dept_name" property="deptName">result>
- association>
- resultMap>
-
- <select id="selectEmpAndDeptByEmpId" resultMap="selectTwo">
- select *from emp join dept on emp.emp_id = dept.dept_id
- where emp_id = #{empId}
- select>
-
-
- mapper>
测试运行:
方法二测试成功!!!
方法三、用分步查询
分步查询第一步:
只查询Emp表里面的全部内容
在MoreMapper里面写入分步查询第一步方法
- package com.mybatis.mapper;
-
- import com.mybatis.pojo.Emp;
- import org.apache.ibatis.annotations.Param;
-
- import java.util.List;
-
- public interface MoreMapper {
-
- //通过员工Id来查询员工信息和部门信息
- List
selectEmpAndDeptByEmpId(@Param("empId") Integer empId); -
- //分步查询第一步
- List
selectEmpAndDeptByStepOne(@Param("empId") Integer empId); - }
在MoreMapper.xml里面写入第一步查询语句
- "1.0" encoding="UTF-8" ?>
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.mybatis.mapper.MoreMapper">
- <resultMap id="selectOne" type="Emp">
- <id column="emp_id" property="empId">id>
- <result column="emp_name" property="empName">result>
- <result column="age" property="age">result>
- <result column="gender" property="gender">result>
- <result column="dept_id" property="dept.deptId">result>
- <result column="dept_name" property="dept.deptName">result>
- resultMap>
-
- <resultMap id="selectTwo" type="Emp">
- <id column="emp_id" property="empId">id>
- <result column="emp_name" property="empName">result>
- <result column="age" property="age">result>
- <result column="gender" property="gender">result>
- <association property="dept" javaType="Dept">
- <id column="dept_id" property="deptId">id>
- <result column="dept_name" property="deptName">result>
- association>
- resultMap>
-
- <select id="selectEmpAndDeptByEmpId" resultMap="selectTwo">
- select *from emp join dept on emp.emp_id = dept.dept_id
- where emp_id = #{empId}
- select>
-
- <resultMap id="MoreMapperOne" type="Emp">
- <id column="emp_id" property="empId">id>
- <result column="emp_name" property="empName">result>
- <result column="age" property="age">result>
- <result column="gender" property="gender">result>
- <association property="dept"
- select=""
- column="">association>
- resultMap>
- <select id="selectEmpAndDeptByStepOne" resultMap="">
- select *from emp where emp_id = #{empId}
- select>
-
-
-
- mapper>
resultMap里面的关键字select表示第二步查询方法的唯一标识,column为查询的条件
开始准备分步查询第二步,创建LessMapper接口写入方法名
- package com.mybatis.mapper;
-
- import com.mybatis.pojo.Dept;
- import org.apache.ibatis.annotations.Param;
-
- import java.util.List;
-
- public interface LessMapper {
- //多对一分步查询第二步
- List
selectEmpAndDeptByStepTwo(@Param("deptId") Integer deptId); -
- }
在LessMapper.xml文件中写入第二步查询方法
- "1.0" encoding="UTF-8" ?>
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.mybatis.mapper.LessMapper">
- <select id="selectEmpAndDeptByStepTwo" resultType="Dept">
- select *from dept where dept_id = #{dept}
- select>
- mapper>
完善MoreMapper里面的select和column
- "1.0" encoding="UTF-8" ?>
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.mybatis.mapper.MoreMapper">
- <resultMap id="selectOne" type="Emp">
- <id column="emp_id" property="empId">id>
- <result column="emp_name" property="empName">result>
- <result column="age" property="age">result>
- <result column="gender" property="gender">result>
- <result column="dept_id" property="dept.deptId">result>
- <result column="dept_name" property="dept.deptName">result>
- resultMap>
-
- <resultMap id="selectTwo" type="Emp">
- <id column="emp_id" property="empId">id>
- <result column="emp_name" property="empName">result>
- <result column="age" property="age">result>
- <result column="gender" property="gender">result>
- <association property="dept" javaType="Dept">
- <id column="dept_id" property="deptId">id>
- <result column="dept_name" property="deptName">result>
- association>
- resultMap>
-
- <select id="selectEmpAndDeptByEmpId" resultMap="selectTwo">
- select *from emp join dept on emp.emp_id = dept.dept_id
- where emp_id = #{empId}
- select>
-
- <resultMap id="MoreMapperOne" type="Emp">
- <id column="emp_id" property="empId">id>
- <result column="emp_name" property="empName">result>
- <result column="age" property="age">result>
- <result column="gender" property="gender">result>
- <association property="dept"
- select="com.mybatis.mapper.LessMapper.selectEmpAndDeptByStepTwo"
- column="dept_id">association>
- resultMap>
- <select id="selectEmpAndDeptByStepOne" resultMap="MoreMapperOne">
- select *from emp where emp_id = #{empId}
- select>
-
-
-
- mapper>
最后开始测试:
分步查询成功!!!
三、一对多的使用
现在我们把Emp表当成多,Dept表当成一,在LessMapper里面写则可以实现一对多
1.创建接口LessMapper
- package com.mybatis.mapper;
-
- import com.mybatis.pojo.Dept;
- import org.apache.ibatis.annotations.Param;
-
- import java.util.List;
-
- public interface LessMapper {
- //多对一分步查询第二步
- List
selectEmpAndDeptByStepTwo(@Param("deptId") Integer deptId); -
- //一对多查询
- List
selectEmpAndDeptByDeptId(@Param("deptId") Integer deptId); -
- }
2、多对一查询实现的两种方法
方法一、用collection标签
- "1.0" encoding="UTF-8" ?>
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.mybatis.mapper.LessMapper">
- <select id="selectEmpAndDeptByStepTwo" resultType="Dept">
- select *from dept where dept_id = #{dept}
- select>
-
- <resultMap id="selectOne" type="Dept">
- <id column="dept_id" property="deptId">id>
- <result column="dept_name" property="deptName">result>
- <collection property="emps" ofType="Emp">
- <id column="emp_id" property="empId">id>
- <result column="emp_name" property="empName">result>
- <result column="age" property="age">result>
- <result column="gender" property="gender">result>
- collection>
-
- resultMap>
- <select id="selectEmpAndDeptByDeptId" resultMap="selectOne">
- select *from dept join emp on dept.dept_id = emp.dept_id where dept.dept_id = #{deptId}
- select>
- mapper>
测试结果如下:
方法一测试成功!!!
方法二、用分步查询
分步查询第一步:
只查询Dept表里面的全部内容
在LessMapper里面写入分步查询第一步方法
- package com.mybatis.mapper;
-
- import com.mybatis.pojo.Dept;
- import org.apache.ibatis.annotations.Param;
-
- import java.util.List;
-
- public interface LessMapper {
- //多对一分步查询第二步
- List
selectEmpAndDeptByStepTwo(@Param("deptId") Integer deptId); -
- //一对多查询
- List
selectEmpAndDeptByDeptId(@Param("deptId") Integer deptId); -
- //一对多分步查询第一步
- List
selectDeptAndEmpByStepOne(@Param("deptId") Integer deptId); -
- }
在LessMapper.xml里面写入第一步查询语句
- "1.0" encoding="UTF-8" ?>
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.mybatis.mapper.LessMapper">
- <select id="selectEmpAndDeptByStepTwo" resultType="Dept">
- select *from dept where dept_id = #{dept}
- select>
-
- <resultMap id="selectOne" type="Dept">
- <id column="dept_id" property="deptId">id>
- <result column="dept_name" property="deptName">result>
- <collection property="emps" ofType="Emp">
- <id column="emp_id" property="empId">id>
- <result column="emp_name" property="empName">result>
- <result column="age" property="age">result>
- <result column="gender" property="gender">result>
- collection>
-
- resultMap>
- <select id="selectEmpAndDeptByDeptId" resultMap="selectOne">
- select *from dept join emp on dept.dept_id = emp.dept_id where dept.dept_id = #{deptId}
- select>
-
- <resultMap id="LessMapperOne" type="Dept">
- <id column="dept_id" property="deptId">id>
- <result column="dept_name" property="deptName">result>
- <collection property="emps"
- select=""
- column="">collection>
- resultMap>
- <select id="selectDeptAndEmpByStepOne" resultMap="LessMapperOne">
- select *from dept where dept_id = #{deptId}
- select>
- mapper>
resultMap里面的关键字select表示第二步查询方法的唯一标识,column为查询的条件
开始准备分步查询第二步,创建MoreMapper接口写入方法名
- package com.mybatis.mapper;
-
- import com.mybatis.pojo.Emp;
- import org.apache.ibatis.annotations.Param;
-
- import java.util.List;
-
- public interface MoreMapper {
-
- //通过员工Id来查询员工信息和部门信息
- List
selectEmpAndDeptByEmpId(@Param("empId") Integer empId); -
- //多对一分步查询第一步
- List
selectEmpAndDeptByStepOne(@Param("empId") Integer empId); -
- //一对多分步查询第二步
- List
selectDeptAndEmpByStepTwo(@Param("deptId") Integer deptId); - }
在LessMapper.xml文件中写入第二步查询方法
- "1.0" encoding="UTF-8" ?>
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.mybatis.mapper.MoreMapper">
- <resultMap id="selectOne" type="Emp">
- <id column="emp_id" property="empId">id>
- <result column="emp_name" property="empName">result>
- <result column="age" property="age">result>
- <result column="gender" property="gender">result>
- <result column="dept_id" property="dept.deptId">result>
- <result column="dept_name" property="dept.deptName">result>
- resultMap>
-
- <resultMap id="selectTwo" type="Emp">
- <id column="emp_id" property="empId">id>
- <result column="emp_name" property="empName">result>
- <result column="age" property="age">result>
- <result column="gender" property="gender">result>
- <association property="dept" javaType="Dept">
- <id column="dept_id" property="deptId">id>
- <result column="dept_name" property="deptName">result>
- association>
- resultMap>
-
- <select id="selectEmpAndDeptByEmpId" resultMap="selectTwo">
- select *from emp join dept on emp.emp_id = dept.dept_id
- where emp_id = #{empId}
- select>
-
- <resultMap id="MoreMapperOne" type="Emp">
- <id column="emp_id" property="empId">id>
- <result column="emp_name" property="empName">result>
- <result column="age" property="age">result>
- <result column="gender" property="gender">result>
- <association property="dept"
- select="com.mybatis.mapper.LessMapper.selectEmpAndDeptByStepTwo"
- column="dept_id">association>
- resultMap>
- <select id="selectEmpAndDeptByStepOne" resultMap="MoreMapperOne">
- select *from emp where emp_id = #{empId}
- select>
-
- <select id="selectDeptAndEmpByStepTwo" resultType="Emp">
- select * from emp where dept_id = #{deptId}
- select>
-
- mapper>
完善LessMapper里面的select和column
- "1.0" encoding="UTF-8" ?>
- mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.mybatis.mapper.LessMapper">
- <select id="selectEmpAndDeptByStepTwo" resultType="Dept">
- select *from dept where dept_id = #{dept}
- select>
-
- <resultMap id="selectOne" type="Dept">
- <id column="dept_id" property="deptId">id>
- <result column="dept_name" property="deptName">result>
- <collection property="emps" ofType="Emp">
- <id column="emp_id" property="empId">id>
- <result column="emp_name" property="empName">result>
- <result column="age" property="age">result>
- <result column="gender" property="gender">result>
- collection>
-
- resultMap>
- <select id="selectEmpAndDeptByDeptId" resultMap="selectOne">
- select *from dept join emp on dept.dept_id = emp.dept_id where dept.dept_id = #{deptId}
- select>
-
- <resultMap id="LessMapperOne" type="Dept">
- <id column="dept_id" property="deptId">id>
- <result column="dept_name" property="deptName">result>
- <collection property="emps"
- select="com.mybatis.mapper.MoreMapper.selectDeptAndEmpByStepTwo"
- column="dept_id">collection>
- resultMap>
- <select id="selectDeptAndEmpByStepOne" resultMap="LessMapperOne">
- select *from dept where dept_id = #{deptId}
- select>
- mapper>
开始测试:
分步查询成功!!!
四、总结
创作不易,请大家多多点赞多多支持,努力创造出更好的作品!!!