MyBatis框架—xml映射

目录

一.为什么需要进行手动映射?

二.关联查询

1.使用resultMap进行映射

2.使用Connection进行映射


一.为什么需要进行手动映射?

当我们设计多表查询或关联查询时,表中含有相同的字段名或要进行关联查询时,MyBatis无法智能识别如何处理映射结果,就需要我们进行手动映射

二.关联查询

用员工表单和部门表单进行演示,对应的表单信息如下

sql 复制代码
create table employee(
	id int primary key auto_increment,
	name varchar(20),
	gender char(1),
	dep_id int,
	constraint employee_department foreign key(dep_id) references department(id)
)
create table department(
	id int primary key auto_increment,
	name varchar(10)
)

1.使用resultMap进行映射

我们需要用员工的id来查询他所在的部门

Employee实体类定义:

用一个Department对象来接收部门信息

java 复制代码
public class Employee {
    private int id;

    private String name;

    private String gender;

    private Department department;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Department getDepartment() {
        return department;
    }

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

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", department=" + department +
                '}';
    }
}

Employee接口定义:

java 复制代码
public interface EmployeeDao {
    Employee findEmployeeById(int id);

    ArrayList<EmployeeDao> findEmployee();
}

Mapper定义:

java 复制代码
<?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="Demo.dao.EmployeeDao">
    <resultMap id="EmployeeMap" type="Employee">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="gender" column="gender"/>
<!--    在关联表时,会自动创建映射对象    -->
        <association property="department" javaType="Department">
            <id property="id" column="did"/>
            <result property="name" column="dname"/>
        </association>
    </resultMap>

    <select id="findEmployeeById" parameterType="int" resultMap="EmployeeMap">
        select e.name,e.gender,d.name dname,d.id did
            from employee e inner join department d on e.dep_id=d.id
            where e.id=#{id}
    </select>

    <select id="findEmployee" resultMap="EmployeeMap">
        select e.name,e.gender,d.name dname
        from employee e inner join department d on e.dep_id=d.id
    </select>

</mapper>

映射部分单独拿出来解释:

java 复制代码
//id就是resultMap的名字,type就是映射对象的数据类型(对应的实体类)
    <resultMap id="EmployeeMap" type="Employee">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="gender" column="gender"/>
<!--    在关联表时,会自动创建映射对象    -->
//property接收映射结果对象的名字,javaType就是在java中对应的数据类型
        <association property="department" javaType="Department">
            <id property="id" column="did"/>
            <result property="name" column="dname"/>
        </association>
    </resultMap>

2.使用Connection进行映射

我们需要查找某个部门的所有员工

Department对应的实体类:

java 复制代码
public class Department {
    private int id;
    private String name;
    private ArrayList<Employee> employee;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ArrayList<Employee> getArrayList() {
        return employee;
    }

    public void setArrayList(ArrayList<Employee> arrayList) {
        this.employee = arrayList;
    }

    @Override
    public String toString() {
        return "Department{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", arrayList=" + employee +
                '}';
    }
}

Department对应的接口:

java 复制代码
public interface DepartmentDao {
    Department findDepartmentById(int id);
}

Mapper:

java 复制代码
<?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="Demo.dao.DepartmentDao">
    <resultMap id="departmentMap" type="Department">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
<!--   这里的property必须和对应实现类的集合名相同,javatype指的是java中的数据类型,ofType指的是集合的泛型    -->
        <collection property="employee" javaType="arraylist" ofType="Employee">
            <id column="did" property="id"/>
            <result column="ename" property="name"/>
            <result column="egender" property="gender"/>
        </collection>
    </resultMap>
<!--  查询结果必须全部写在映射里面  -->
    <select id="findDepartmentById" parameterType="int" resultMap="departmentMap">
            select e.name ename,e.gender egender,e.id eid,d.name,d.id
            from employee e inner join department d on e.dep_id=d.id
            where d.id = #{id}
    </select>
</mapper>
相关推荐
lanfufu6 分钟前
记一次诡异的线上异常赋值排查:代码没错,结果不对
java·jvm·后端
yuren_xia6 分钟前
Spring Boot + MyBatis 集成支付宝支付流程
spring boot·tomcat·mybatis
枣伊吕波16 分钟前
第十三节:第四部分:集合框架:HashMap、LinkedHashMap、TreeMap
java·哈希算法
weixin_4723394625 分钟前
使用Python提取PDF元数据的完整指南
java·python·pdf
PascalMing28 分钟前
Ruoyi多主键表的增删改查
java·若依ruoyi·多主键修改删除
橘子青衫34 分钟前
Java并发编程利器:CyclicBarrier与CountDownLatch解析
java·后端·性能优化
天天摸鱼的java工程师1 小时前
高考放榜夜,系统别崩!聊聊查分系统怎么设计,三张表足以?
java·后端·mysql
天天摸鱼的java工程师1 小时前
深入理解 Spring 核心:IOC 与 AOP 的原理与实践
java·后端
漫步者TZ1 小时前
【Netty系列】解决TCP粘包和拆包:LengthFieldBasedFrameDecoder
java·网络协议·tcp/ip·netty
愿你是阳光06071 小时前
Java-redis实现限时在线秒杀功能
java·redis·bootstrap