首页 > 其他分享 >03 MyBatis自定义映射

03 MyBatis自定义映射

时间:2022-09-25 10:56:32浏览次数:52  
标签:03 star 自定义 light dept emp MyBatis import public

一、MyBatis环境搭建

1.1、数据的准备

CREATE DATABASE IF NOT EXISTS db_test;
USE db_test;

CREATE TABLE IF NOT EXISTS t_emp(
	emp_id int PRIMARY KEY auto_increment,
	emp_name VARCHAR(30),
	age INT,
	sex CHAR,
	dept_id INT
);

CREATE TABLE IF NOT EXISTS t_dept(
	dept_id INT PRIMARY KEY auto_increment,
	dept_name VARCHAR(30)
);


INSERT INTO  t_dept(dept_name)
VALUES('魔法部'),
		  ('超能力部'),
			('灵异部');


INSERT INTO t_emp(emp_name,age,sex,dept_id)
VALUES('Sakura',10,'女',1),
			('Mikoto',14,'女',2),
			('Shana',15,'女',1),
			('Kikyō',18,'女',3),
			('Kagome',15,'女',3);

1.2、导入jar包

  通过 maven 的方式导入 jar包,打包方式设置为 jar 包即可。

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.7</version>
</dependency>

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.29</version>
</dependency>

<!-- junit 测试程序 -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

1.3、创建核心配置文件

  创建 MyBatis 的核心配置文件,习惯上命名为 mybatis-config.xml,这个文件名仅仅只是建议,并非强制要求。配置 MyBatis 的核心配置文件中的标签必须按照固定的顺序:

properties --> settings --> typeAliases --> typeHandlers --> objectFactory --> objectWrapperFactory --> reflectorFactory --> plugins --> environments --> databaseIdProvider --> mappers

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!-- 引入数据库连接信息的配置文件 -->
    <properties resource="jdbc.properties"/>

    <!-- 设置类型别名 -->
    <typeAliases>
        <!-- 以包为单位,将包下所有的类型设置默认的类型别名,即类名且不区分大小写 -->
        <package name="star.light.pojo"/>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <!-- 设置连接数据库的驱动 -->
                <property name="driver" value="${jdbc.driver}"/>
                <!-- 设置连接数据库的地址 -->
                <property name="url" value="${jdbc.url}"/>
                <!-- 设置连接数据库的用户名 -->
                <property name="username" value="${jdbc.username}"/>
                <!-- 设置连接数据库的密码 -->
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>

    <!--引入映射文件-->
    <mappers>
        <!--
            以包为单位引入映射文件,要求:
                1、mapper接口所在的包与映射文件所在的包一致
                2、mapper接口要和映射文件一致
        -->
        <package name="star.light.mapper"/>
    </mappers>
</configuration>

  数据库连接信息的配置文件:jdbc.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_test
jdbc.username=root
jdbc.password=abc123

1.4、创建实体类

package star.light.pojo;

public class Employee {
    private Integer empId;
    private String empName;
    private Integer age;
    private String sex;
    private Department department;

    public Employee() {
    }

    public Employee(Integer empId, String empName, Integer age, String sex) {
        this.empId = empId;
        this.empName = empName;
        this.age = age;
        this.sex = sex;
    }

    public Employee(Integer empId, String empName, Integer age, String sex, Department department) {
        this.empId = empId;
        this.empName = empName;
        this.age = age;
        this.sex = sex;
        this.department = department;
    }

    public Integer getEmpId() {
        return empId;
    }

    public void setEmpId(Integer empId) {
        this.empId = empId;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "empId=" + empId +
                ", empName='" + empName + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", department=" + department +
                '}';
    }
}
package star.light.pojo;

import java.util.List;

public class Department {
    private Integer deptId;
    private String deptName;
    private List<Employee> employees;

    public Department() {
    }

    public Department(Integer deptId, String deptName) {
        this.deptId = deptId;
        this.deptName = deptName;
    }

    public Department(Integer deptId, String deptName, List<Employee> employees) {
        this.deptId = deptId;
        this.deptName = deptName;
        this.employees = employees;
    }

    public Integer getDeptId() {
        return deptId;
    }

    public void setDeptId(Integer deptId) {
        this.deptId = deptId;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public List<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }

    @Override
    public String toString() {
        return "Department{" +
                "deptId=" + deptId +
                ", deptName='" + deptName + '\'' +
                ", employees=" + employees +
                '}';
    }
}

1.5、创建mapper接口

  MyBatis 中的 mapper 接口相当于以前的 dao。但是区别在于,mapper 仅仅是接口,我们不需要提供实现类。

package star.light.mapper;

public interface EmpMapper {

    /**
     * MyBatis 面向接口编程的两个一致:
     *  1、映射文件的 namespace 要和 mapper 接口的全类名保持一致
     *  2、映射文件中 SQL 语句的 id 要和 mapper 接口中方法名一致
     */
}
package star.light.mapper;

public interface DeptMapper{

    /**
     * MyBatis 面向接口编程的两个一致:
     *  1、映射文件的 namespace 要和 mapper 接口的全类名保持一致
     *  2、映射文件中 SQL 语句的 id 要和 mapper 接口中方法名一致
     */
}

1.6、创建MyBatis的映射文件

  映射文件的命名应该为 表所对应的实体类的类名+Mapper.xml。一个映射文件对应一个实体类,对应一张表的操作。MyBatis映射文件用于编写SQL,访问以及操作表中的数据。

<?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="star.light.mapper.EmpMapper">

</mapper>
<?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="star.light.mapper.DeptMapper">

</mapper>

1.7、封装SqlSessionUtil工具类

package star.light.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SqlSessionUtil {
  
    public static SqlSession getSqlSession(){
        SqlSession sqlSession = null;
  
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            sqlSession = sqlSessionFactory.openSession(true);
        } catch (IOException e) {
            e.printStackTrace();
        }
  
        return sqlSession;
    }
}

二、字段名与属性名不一致问题

2.1、查询的字段设置别名

  测试程序:

package star.light.test;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import star.light.mapper.EmpMapper;
import star.light.pojo.Employee;
import star.light.util.SqlSessionUtil;

public class ResultMapTest {

    @Test
    public void testGetEmployeeByEmpId(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Employee employee = mapper.getEmployeeByEmpId(1);
        System.out.println(employee);
    }
}

  重写 EmpMapper 接口:

package star.light.mapper;

import org.apache.ibatis.annotations.Param;
import star.light.pojo.Employee;

public interface EmpMapper {
    Employee getEmployeeByEmpId(@Param("empId") Integer empid);         // 根据id查询员工的信息
}

  重写 EmpMapper.xml 配置文件:

<?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="star.light.mapper.EmpMapper">
    <!-- Employee getEmployeeByEmpId(@Param("empId") Integer empid); -->
    <select id="getEmployeeByEmpId" resultType="Employee">
        <!--
            字段名和属性名不一致的情况,如何处理映射关系:
                为查询的字段设置别名,和属性名保持一致
        -->
        select emp_id empId,emp_name empName,age,sex from t_emp where emp_id = #{empId}
    </select>
</mapper>

2.2、核心配置文件中设置全局配置

  在 mybatis-config.xml 配置文件中 <configuration> 中的 <properties> 后添加如下信息:

<settings>
    <!-- 将下划线映射为驼峰 -->
    <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>

  重写 EmpMapper.xml 配置文件:

<?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="star.light.mapper.EmpMapper">
    <!-- Employee getEmployeeByEmpId(@Param("empId") Integer empid); -->
    <select id="getEmployeeByEmpId" resultType="Employee">
        <!--
            字段名和属性名不一致的情况,如何处理映射关系:
                当字段符合MySQL的要求使用_命令法,而属性名符合Java的要求使用驼峰命名法
                此时可以在 MyBatis的核心配置文件中设置一个全局配置,可以自动将下划线映射为驼峰
        -->
        select emp_id,emp_name,age,sex from t_emp where emp_id = #{empId}
    </select>
</mapper>

  其中,ReusltMapTest 测试程序,EmpMapper 接口内容同上;

2.3、自定义映射处理

  重写 EmpMapper.xml 配置文件:

<?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="star.light.mapper.EmpMapper">

    <!--
        resultMap:设置自定义映射
        属性:
            id:表示自定义映射的唯一标识
            type:查询的数据要映射的实体类的类型
        子标签:
            id:设置主键的映射关系
            result:设置普通字段的映射关系
            association:设置多对一的映射关系
            collection:设置一对多的映射关系
            属性:
                property:设置映射关系中实体类中的属性名
                column:设置映射关系中表中的字段名
     -->
    <resultMap id="empAndDeptResultMap" type="Emp">
        <id column="emp_id" property="empId"></id>
        <result column="emp_name" property="empName"></result>
        <result column="age" property="age"></result>
        <result column="sex" property="sex"></result>
    </resultMap>

    <!-- Employee getEmployeeByEmpId(@Param("empId") Integer empid); -->
    <select id="getEmployeeByEmpId" resultMap="empResultMap">
        select emp_id,emp_name,age,sex from t_emp where emp_id = #{empId}
    </select>
</mapper>

  其中,ReusltMapTest 测试程序,EmpMapper 接口内容同上;

三、多对一映射关系

3.1、级联方式处理映射关系

  测试程序:

package star.light.test;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import star.light.mapper.EmpMapper;
import star.light.pojo.Employee;
import star.light.util.SqlSessionUtil;

public class ResultMapTest {

    @Test
    public void testGetEmpAndDeptByEmpId(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Employee employee = mapper.getEmpAndDeptByEmpId(1);
        System.out.println(employee);
    }
}

  EmpMapper 接口:

package star.light.mapper;

import org.apache.ibatis.annotations.Param;
import star.light.pojo.Employee;

public interface EmpMapper {
    Employee getEmpAndDeptByEmpId(@Param("empId") Integer empid);       // 获取员工以及所对应的部门信息
}

  EmpMapper.xml 配置文件:

<?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="star.light.mapper.EmpMapper">

    <resultMap id="empAndDeptResultMap" type="Employee">
        <id column="emp_id" property="empId"></id>
        <result column="emp_name" property="empName"></result>
  
        <!-- 使用级联方式处理多对一映射关系 -->
        <result column="dept_id" property="department.deptId"></result>
        <result column="dept_name" property="department.deptName"></result>
    </resultMap>

    <!-- Employee getEmpAndDeptByEmpId(@Param("empId") Integer empid); -->
    <select id="getEmpAndDeptByEmpId" resultMap="empAndDeptResultMap">
        select emp_id,emp_name,age,sex,t_dept.dept_id,dept_name
        from t_emp
        left join t_dept on t_emp.dept_id = t_dept.dept_id
        where emp_id = #{empId}
    </select>
</mapper>

3.2、association标签处理映射关系

  EmpMapper.xml 配置文件:

<?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="star.light.mapper.EmpMapper">

    <resultMap id="empAndDeptResultMap" type="Employee">
        <id column="emp_id" property="empId"></id>
        <result column="emp_name" property="empName"></result>

        <!-- 
            使用association标签处理多对一映射关系(处理实体类类型属性) 
            property:设置需要处理映射关系的属性的属性名
            javaType:设置要处理的出行的类型
        -->
        <association property="department" javaType="Department">
            <id column="dept_id" property="deptId"></id>
            <result column="dept_name" property="deptName"></result>
        </association>
    </resultMap>

    <!-- Employee getEmpAndDeptByEmpId(@Param("empId") Integer empid); -->
    <select id="getEmpAndDeptByEmpId" resultMap="empAndDeptResultMap">
        select emp_id,emp_name,age,sex,t_dept.dept_id,dept_name
        from t_emp
        left join t_dept on t_emp.dept_id = t_dept.dept_id
        where emp_id = #{empId}
    </select>
</mapper>

  其中,ReusltMapTest 测试程序,EmpMapper 接口内容同上;

3.3、分步查询

  测试程序:

package star.light.test;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import star.light.mapper.EmpMapper;
import star.light.pojo.Employee;
import star.light.util.SqlSessionUtil;

public class ResultMapTest {

    @Test
    public void testGetEmpAndDeptByEmpId(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Employee employee = mapper.getEmpAndDeptByStepOne(1);
        System.out.println(employee);
    }
}

  EmpMapper 接口:

package star.light.mapper;

import org.apache.ibatis.annotations.Param;
import star.light.pojo.Employee;

public interface EmpMapper {
    Employee getEmpAndDeptByStepOne(@Param("empId") Integer empId);      //通过分步查询员工以及所对应的部门信息的第一步
}

  EmpMapper.xml 配置文件

<?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="star.light.mapper.EmpMapper">

    <resultMap id="empAndDeptByStepResultMapOne" type="Employee">
        <id column="emp_id" property="empId"></id>
        <result column="emp_name" property="empName"></result>

        <!--
            使用association标签处理多对一映射关系(处理实体类类型属性)
            property:设置需要处理映射关系的属性的属性名
            select:设置分步查询的sql的唯一标识
            column:将查询出的某个字段作为分步查询的sql的条件
        -->
        <association property="department"
                     select="star.light.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                     column="dept_id">
        </association>
    </resultMap>

    <!-- Employee getEmpAndDeptByStepOne(@Param("empId") Integer empId); -->
    <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMapOne">
        select emp_id,emp_name,age,sex,dept_id from t_emp where emp_id = #{empId}
    </select>
</mapper>

  DeptMapper 接口:

package star.light.mapper;

import org.apache.ibatis.annotations.Param;
import star.light.pojo.Department;

public interface DeptMapper {
    Department getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);        // 通过分步查询查询员工以及所对应的部门信息的第二部
}

  DeptMapper.xml 配置文件:

<?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="star.light.mapper.DeptMapper">

        <resultMap id="empAndDeptByStepResultMapTwo" type="Department">
                <id column="dept_id" property="deptId"></id>
                <result column="dept_name" property="deptName"></result>
        </resultMap>

        <!-- Department getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId); -->
        <select id="getEmpAndDeptByStepTwo" resultMap="empAndDeptByStepResultMapTwo">
                select dept_id,dept_name from t_dept where dept_id = #{deptId}
        </select>
</mapper>

  分步查询可以实现延迟加载,但是必须在核心配置文件中设置全局配置信息:

lazyLoadingEnabled:延迟加载的全局开关。当开启时,所有关联对象都会延迟加载

aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。否则,每个属性会按需加载。此时就可以实现按需加载,获取的数据是什么,就只会执行相应的 sql。

<settings>
    <!-- 开启延迟加载 -->
    <setting name="lazyLoadingEnabled" value="true"/>
    <!-- 按需加载 -->
    <setting name="aggressiveLazyLoading" value="false"/>
</settings>

  设置全局设置后,对所有的分步查询都有效果,如果某个分步查询不想实现延迟加载,此时可以在相对应的 MyBatis 的映射文件中通过 <association><collection> 中的 fetchType 属性设置当前的分步查询是否使用延迟加载,** fetchType="lazy (延迟加载)** | eager (立即加载)"

四、一对多映射关系

4.1、collection标签处理映射关系

  测试程序:

package star.light.test;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import star.light.mapper.DeptMapper;
import star.light.pojo.Department;
import star.light.util.SqlSessionUtil;

public class ResultMapTest {

    @Test
    public void getDeptAndEmpByDeptId(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
        Department department = mapper.getDeptAndEmpByDeptId(1);
        System.out.println(department);
    }
}

  DeptMapper 接口:

package star.light.mapper;

import org.apache.ibatis.annotations.Param;
import star.light.pojo.Department;

public interface DeptMapper {
    Department getDeptAndEmpByDeptId(@Param("deptId") Integer deptId);          // 查询部门以及部门中的员工信息
}

  DeptMapper.xml 配置文件:

<?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="star.light.mapper.DeptMapper">

        <resultMap id="deptAndEmpResultMap" type="Department">
                <id column="dept_id" property="deptId"></id>
                <result column="dept_name" property="deptName"></result>

                <!--
                        collection处理一对多映射关系(处理集合类型的属性)
                        property:设置需要处理映射关系的属性的属性名
                        ofType:设置集合类型的属性中存储的数据的类型
                 -->
                <collection property="employees" ofType="Employee">
                        <id column="emp_id" property="empId"></id>
                        <result column="emp_name" property="empName"></result>
                        <result column="age" property="age"></result>
                        <result column="sex" property="sex"></result>
                </collection>
        </resultMap>

        <!-- Department getDeptAndEmpByDeptId(@Param("deptId") Integer deptId); -->
        <select id="getDeptAndEmpByDeptId" resultMap="deptAndEmpResultMap">
                SELECT t_dept.dept_id,dept_name,emp_id,emp_name,age,sex
                FROM t_emp
                RIGHT JOIN t_dept ON t_emp.dept_id = t_dept.dept_id
                WHERE t_dept.dept_id = #{deptId}
        </select>
</mapper>

4.2、分步查询

  测试程序:

package star.light.test;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import star.light.mapper.DeptMapper;
import star.light.pojo.Department;
import star.light.util.SqlSessionUtil;

public class ResultMapTest {

    @Test
    public void getDeptAndEmpByStep(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
        Department department = mapper.getDeptAndEmpByStepOne(1);
        System.out.println(department);
    }
}

  DeptMapper 接口:

package star.light.mapper;

import org.apache.ibatis.annotations.Param;
import star.light.pojo.Department;

public interface DeptMapper {
    Department getDeptAndEmpByStepOne(@Param("deptId") Integer deptId);     //通过分步查询查询部门以及部门中的员工信息的第一步
}

  DeptMapper.xml 配置文件:

<?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="star.light.mapper.DeptMapper">

        <resultMap id="deptAndEmpResultMapOne" type="Department">
                <id column="dept_id" property="deptId"></id>
                <result column="dept_name" property="deptName"></result>

                <!--
                        collection处理一对多映射关系(处理集合类型的属性)
                        property:设置需要处理映射关系的属性的属性名
                        select:设置分步查询的sql的唯一标识
                        column:将查询出的某个字段作为分步查询的sql的条件
                 -->
                <collection property="employees"
                            select="star.light.mapper.EmpMapper.getDeptAndEmpByStepTwo"
                            column="dept_id">
                </collection>
        </resultMap>

        <!-- Department getDeptAndEmpByStepOne(@Param("deptId") Integer deptId); -->
        <select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpResultMapOne">
                SELECT dept_id,dept_name FROM t_dept WHERE dept_id = #{deptId}
        </select>
</mapper>

  EmpMapper 接口:

package star.light.mapper;

import org.apache.ibatis.annotations.Param;
import star.light.pojo.Employee;

import java.util.List;

public interface EmpMapper {
    List<Employee> getDeptAndEmpByStepTwo(@Param("deptId") Integer deptId);         // 通过分步查询查询部门以及部门中的员工信息的第二步
}

  EmpMapper.xml 配置文件:

<?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="star.light.mapper.EmpMapper">

    <resultMap id="deptAndEmpResultMapTwo" type="Employee">
        <id column="emp_id" property="empId"></id>
        <result column="emp_name" property="empName"></result>
    </resultMap>

    <!-- List<Employee> getDeptAndEmpByStepTwo(@Param("deptId") Integer deptId); -->
    <select id="getDeptAndEmpByStepTwo" resultMap="deptAndEmpResultMapTwo">
        select emp_id,emp_name,age,sex,dept_id from t_emp where dept_id = #{deptId}
    </select>
</mapper>

标签:03,star,自定义,light,dept,emp,MyBatis,import,public
From: https://www.cnblogs.com/nanoha/p/16727406.html

相关文章