当前位置: 首页 > news >正文

MyBatis框架—xml映射

目录

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

二.关联查询

1.使用resultMap进行映射

2.使用Connection进行映射


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

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

二.关联查询

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

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对象来接收部门信息

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;}@Overridepublic String toString() {return "Employee{" +"id=" + id +", name='" + name + '\'' +", gender='" + gender + '\'' +", department=" + department +'}';}
}

Employee接口定义:

public interface EmployeeDao {Employee findEmployeeById(int id);ArrayList<EmployeeDao> findEmployee();
}

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="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 didfrom employee e inner join department d on e.dep_id=d.idwhere e.id=#{id}</select><select id="findEmployee" resultMap="EmployeeMap">select e.name,e.gender,d.name dnamefrom employee e inner join department d on e.dep_id=d.id</select></mapper>

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

//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对应的实体类:

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;}@Overridepublic String toString() {return "Department{" +"id=" + id +", name='" + name + '\'' +", arrayList=" + employee +'}';}
}

Department对应的接口:

public interface DepartmentDao {Department findDepartmentById(int id);
}

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="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.idfrom employee e inner join department d on e.dep_id=d.idwhere d.id = #{id}</select>
</mapper>

http://www.xdnf.cn/news/57997.html

相关文章:

  • 34、Spark实现读取XLS文件
  • iOS中使用AWS上传zip文件到Minio上的oss平台上
  • nvidia physx
  • C# 封装教程
  • MCP实践第一步--磕磕碰碰搭环境
  • TensorFlow中使用Keras
  • Spring如何通过XML注册Bean
  • C++23 让 Lambda 表达式中的 () 更可选:P1102R2 提案深度解析
  • Apache RocketMQ 荣获 2024 开源创新榜单“年度开源项目
  • 【网络安全】OWASP 十大漏洞
  • 大数据组件学习之--Kafka 安装搭建
  • 机器人进阶---视觉算法(五)仿射变换和投影变换有什么区别
  • 国产AI新突破!全球首款无限时长电影生成模型SkyReels-V2开源:AI视频进入长镜头时代!
  • LangChain + 文档处理:构建智能文档问答系统 RAG 的实战指南
  • 微服务划分的思考
  • 量子计算在金融领域的应用与展望
  • Unity接入安卓SDK(3)厘清Gradle的版本
  • AI助理iOS开发:Copilot for Xcode 下载与安装全指南
  • Java 自动装箱与拆箱:基本数据类型与包装类的转换
  • Ansys electronics安装多版本simulink打开s-function冲突解决方法
  • 用Mac M4构建多架构Docker镜像指南
  • CSS 中实现 div 居中有以下几种常用方法
  • 解决Chrome浏览器访问https提示“您的连接不是私密连接”的问题
  • Android 15强制edge-to-edge全面屏体验
  • (7)NodeJS的使用与NPM包管理器
  • 1.2软考系统架构设计师:系统架构的定义与作用 - 练习题附答案及超详细解析
  • 23种设计模式-结构型模式之外观模式(Java版本)
  • Spark和Hadoop的区别和联系
  • 深入理解 DML 和 DQL:SQL 数据操作与查询全解析
  • Java BIO、NIO、AIO、Netty面试题(已整理全套PDF版本)