Mapper.xml详解 一对一 多对一 多对多

发布时间 2023-07-31 18:33:23作者: sunny123456

Mapper.xml详解 Mapper.xml详解 一对一 多对一 多对多
原文链接:https://blog.csdn.net/qq_36850813/article/details/80037363

 我们知道,每一款框架产品在实际开发中,都是通过XML文件来培训框架的相关流程的,MyBatis也不例外,主要有两个配置文件:config.xml和Mapper.xml,当然,这两种配置文件可以自定义文件名。
config.xml是全局配置文件,主要配置MyBatis的数据源(DataSource),事务管理(TransactionManager),以及打印SQL语句,开启二级缓存,设置实体类别名等功能。
Mapper.xml的作用是什么?我们之前介绍过,MyBatis是"半自动"的ORM框架,即SQL语句需要开发者自定义,MyBatis的关注点在POJO与SQL之间的映射关系。那么SQL语句在哪里配置自定义呢?就在Mapper.xml中配置。

首先来介绍Mapper.xml常用属性:
parameterType:参数数据类型

(1)基本数据类型,通过id查询User。

UserDao:

  1. //通过id查找User
  2. public User getById(int id);

UserDao.xml:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper>
  4. <select id="getById" parameterType="int" resultType="com.xf.pojo.User">
  5. SELECT * FROM t_user WHERE id=#{id}
  6. </select>
  7. </mapper>

(2)String类型,通过name查User

UserDao:

  1. //通过name查询User
  2. public User getByName(String name);

UserDao.xml:

  1. <select id="get2" parameterType="java.lang.String" resultType="com.xf.pojo.User">
  2.    select * from t_user where name = #{name}
  3. </select>

(3)包装类,通过id查询User。

UserDao:

  1. //通过id查询User
  2. public User getById(Integer id);

UserDao.xml:

  1. <select id="getById" parameterType="java.lang.Integer" resultType="com.xf.pojo.User">
  2.    select * from t_user where id=#{id}
  3. </select>

(4)多个参数,通过name和age查询User。两个参数分别是String类型和int类型,类型不一致,所以此时parameterType可以省略,通过参数下标取出参数值

UserDao:

  1. //通过name和age查询User
  2. public User getByNameAge(int id,String name);

UserDao.xml:

  1. <select id="getByNameAge" resultType="com.xf.pojo.User">
  2.    select * from t_user where name = #{0} and age = #{1}
  3. </select>

(5)POJO,很显然,当有多个参数时,一个一个写太麻烦了,这时候我们可以将参数列表进行封装,将封装对象作为parameterType的值。

UserDao:

  1. //根据Usesr封装对象查询User
  2. public User getByUser(User user);

UserDao.xml:

  1. <select id="getByUser" parameterType="com.xf.pojo.User" resultType="com.xf.pojo.User">
  2.    select * from t_user where name = #{name} and age = #{age}
  3. </select>

resultType:结果类型

(1)基本数据类型,统计User总数。

UserDao:

  1. //通过User总数量
  2. public int getCount();

UserDao.xml:

  1. <select id="getCount" resultType="int">
  2.    select count(*) from t_user
  3. </select>

(2)包装类,统计User总数。

UserDao:

  1. //通过User总数量
  2. public Integer getCount();

UserDao.xml:

  1. <select id="getCount" resultType="java.lang.Integer">
  2.    select count(*) from t_user
  3. </select>

(3)String类型,根据id查询User的name值。

UserDao:

  1. //根据id查询User的name
  2. public String getNameById(int id);

UserDao.xml:

  1. <select id="getNameById" parameterType="int" resultType="java.lang.String">
  2.    select name from user where id = #{name}
  3. </select>

(4)POJO,如通过id查询User,上面已经介绍过了,这里就不再重复了。

级联查询
 

一对多

我们现在查询的User是单表查询,如果是多表关联查询,比如查询Student同时级联对应的Classes,如何处理呢?

使用resultType无法完成,我们以通过id查询Student来举例。

SQL:

select * from student as s,classes as c where s.cid = c.c_id and s.id = 1; 

查询结果:

实体类Student:

  1. package com.xf.pojo;
  2. public class Student {
  3. private int id;
  4. private String name;
  5. private String address;
  6. private String tel;
  7. private int score;
  8. private Classes classes;
  9. public int getId() {
  10. return id;
  11. }
  12. public void setId(int id) {
  13. this.id = id;
  14. }
  15. public String getName() {
  16. return name;
  17. }
  18. public void setName(String name) {
  19. this.name = name;
  20. }
  21. public String getAddress() {
  22. return address;
  23. }
  24. public void setAddress(String address) {
  25. this.address = address;
  26. }
  27. public String getTel() {
  28. return tel;
  29. }
  30. public void setTel(String tel) {
  31. this.tel = tel;
  32. }
  33. public int getScore() {
  34. return score;
  35. }
  36. public void setScore(int score) {
  37. this.score = score;
  38. }
  39. public Classes getClasses() {
  40. return classes;
  41. }
  42. public void setClasses(Classes classes) {
  43. this.classes = classes;
  44. }
  45. }

Classes:

  1. package com.xf.pojo;
  2. import java.util.List;
  3. public class Classes {
  4. private int id;
  5. private String name;
  6. private List<Student> students;
  7. public int getId() {
  8. return id;
  9. }
  10. public void setId(int id) {
  11. this.id = id;
  12. }
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. public List<Student> getStudents() {
  20. return students;
  21. }
  22. public void setStudents(List<Student> students) {
  23. this.students = students;
  24. }
  25. }

MyBatis会自动将结果与实体类进行映射,将字段的值赋给对应的属性,若字段名与属性名一致,完成赋值,那么问题来了。

如图,id,name,address,tel,score属性可以对应字段,classes属性没有对应的字段,准确的讲,classes属性需要对应的对象为c_id,c_name封装起来的对象。

此时,需要使用resultMap来完成映射。

StudentDao:

  1. //通过id查询Student
  2. public Student getById(int id);

StudentDao.xml,使用association标签配置classes级联,因为一个Student只能对应一个Classes。

  1. <resultMap type="student" id="studentMap">
  2.    <id property="id" column="id"/>
  3.    <result property="name" column="name"/>
  4.    <result property="address" column="address"/>
  5.    <result property="tel" column="tel"/>
  6.    <result property="score" column="score"/>
  7.    <!-- 映射classes属性 -->
  8.    <association property="classes" javaType="com.xf.pojo.Classes">
  9.        <id property="id" column="c_id"/>
  10.        <result property="name" column="c_name"/>
  11.    </association>
  12. </resultMap>
  13. <select id="getById" parameterType="int" resultMap="studentMap">
  14.    select * from student as s,classes as c where s.cid = c.c_id and s.id = #{id};
  15. </select>

同理,反过来查询Classes,将级联的所有Student一并查询。

ClassesDao:

  1. //根据id查询Classes
  2. public Classes getById(int id);

ClassesDao.xml,使用collection标签配置students级联,因为一个Classes可以对应多个Student。

  1. <resultMap type="classes" id="classesMap">
  2.    <id property="id" column="c_id"/>
  3.    <result property="name" column="c_name"/>
  4.    <!-- 映射students属性 -->
  5.    <collection property="students" ofType="student">
  6.        <id property="id" column="id"/>
  7.        <result property="name" column="name"/>
  8.        <result property="address" column="address"/>
  9.        <result property="tel" column="tel"/>
  10.        <result property="score" column="score"/>
  11.    </collection>
  12. </resultMap>
  13. <select id="getById" parameterType="int" resultMap="classesMap">
  14.    select * from classes as c,student as s where c.c_id = s.cid and c.c_id = #{id};
  15. </select>

需要注意的是:association标签,通过设置javaType属性,映射实体类,

collection标签,通过设置ofType属性映射实体类。

 

多对多

多对多其实是双向的一对多关系,我们用Customer和Goods来举例,

一个Customer可以对应多个Goods,一个Goods也可以对应多个Customer,所以双方都是用collection标签设置级联。

Customer:

  1. package com.xf.pojo;
  2. import java.util.List;
  3. public class Customer {
  4. private int id;
  5. private String name;
  6. private List<Goods> goods;
  7. public int getId() {
  8. return id;
  9. }
  10. public void setId(int id) {
  11. this.id = id;
  12. }
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. public List<Goods> getGoods() {
  20. return goods;
  21. }
  22. public void setGoods(List<Goods> goods) {
  23. this.goods = goods;
  24. }
  25. }

Goods:

  1. package com.xf.pojo;
  2. import java.util.List;
  3. public class Goods {
  4. private int id;
  5. private String name;
  6. private List<Customer> customers;
  7. public int getId() {
  8. return id;
  9. }
  10. public void setId(int id) {
  11. this.id = id;
  12. }
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. public List<Customer> getCustomers() {
  20. return customers;
  21. }
  22. public void setCustomers(List<Customer> customers) {
  23. this.customers = customers;
  24. }
  25. }

CustomerDao:

  1. //根据id查询Customer
  2. public Customer getById(int id);

CustomerDao.xml:

  1. <resultMap type="customer" id="customerMap">
  2.    <id property="id" column="c_id"/>
  3.    <result property="name" column="c_name"/>
  4.    <!-- 映射goods属性 -->
  5.    <collection property="goods" ofType="goods">
  6.        <id property="id" column="g_id"/>
  7.        <result property="name" column="g_name"/>
  8.    </collection>
  9. </resultMap>
  10. <select id="getById" parameterType="int" resultMap="customerMap">
  11.    select * from customer as c,goods as g,
  12.    customer_goods as cg where c.c_id = cg.c_id
  13.    and g.g_id =c g.g_id and c.c_id = #{id};
  14. </select>

GoodsDao:

  1. //根据id查询Goods
  2. public Goods getById(int id);

GoodsDao.xml:

  1. <resultMap type="goods" id="goodsMap">
  2.    <id property="id" column="g_id"/>
  3.    <result property="name" column="g_name"/>
  4.    <!-- 映射customers属性 -->
  5.    <collection property="customers" ofType="customer">
  6.        <id property="id" column="c_id"/>
  7.        <result property="name" column="c_name"/>
  8.    </collection>
  9. </resultMap>
  10. <select id="getById" parameterType="int" resultMap="goodsMap">
  11.    select * from customer as c,
  12.    goods as g,customer_goods as cg
  13.    where c.c_id = cg.c_id and g.g_id
  14.    = cg.g_id and g.g_id = #{id};
  15. </select>