MyBatis

本文介绍了MyBatis中如何使用ResultMap进行一对一、一对多及多对多的关系映射,包括具体的配置方法和示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MyBatis

ResultMap

  • 属性:type:指定为哪个实体类配置表字段(或sql语句查询的别名)和实体类的属性的映射,id:为唯一标识
  • assoation:配置实体类属性为对象的关系映射标签 :
    属性:property:被包含对象的对象名称
    javaType:被包含对象的数据类型

SQL语句

关系模型映射:
resultMap中映射的是查询的字段(可以起别名)和实体对象的属性的关系映射
起别名时候是因为两个表的属性都是id导致错误,故起别名解决
select p.id id,c.id cid from person p,idcard c;

多表操作

一对一操作

通过外键(任意一方建立外键)来关联起来

<resultMap>:配置字段和对象属性的映射关系标签。
       id 属性:唯一标识
       type 属性:实体对象类型
   <id>:配置主键映射关系标签。
   <result>:配置非主键映射关系标签。
       column 属性:表中字段名称
       property 属性: 实体对象变量名称
   <association>:配置被包含对象的映射关系标签。
       property 属性:被包含对象的变量名
       javaType 属性:被包含对象的数据类型

eg:
一个人对象对应一个身份证对象
sql:

 CREATE TABLE person(
     	id INT PRIMARY KEY AUTO_INCREMENT,
     	NAME VARCHAR(20),
     	age INT
     );
     INSERT INTO person VALUES (NULL,'张三',23);
     INSERT INTO person VALUES (NULL,'李四',24);
     INSERT INTO person VALUES (NULL,'王五',25);
     
     CREATE TABLE card(
     	id INT PRIMARY KEY AUTO_INCREMENT,
     	number VARCHAR(30),
     	pid INT,
     	CONSTRAINT cp_fk FOREIGN KEY (pid) REFERENCES person(id)
     );
     INSERT INTO card VALUES (NULL,'12345',1);
     INSERT INTO card VALUES (NULL,'23456',2);
     INSERT INTO card VALUES (NULL,'34567',3);

pojo:

public class Pseron{
	int id;
	String name;
	int age;
}
public class Card{
	int id;
	String number;
	Person p;
}

mapper:

     <mapper namespace="com.itheima.table01.OneToOneMapper">
         <!--配置字段和实体对象属性的映射关系-->
         <resultMap id="oneToOne" type="card">
             <id column="cid" property="id" />
             <result column="number" property="number" />
             <!--
                 association:配置被包含对象的映射关系
                 property:被包含对象的变量名
                 javaType:被包含对象的数据类型
             -->
             <association property="p" javaType="person">
                 <id column="pid" property="id" />
                 <result column="name" property="name" />
                 <result column="age" property="age" />
             </association>
         </resultMap>
     
         <select id="selectAll" resultMap="oneToOne">
             SELECT c.id cid,number,pid,NAME,age FROM card c,person p WHERE c.pid=p.id
         </select>
     </mapper>
一对多操作

通过外键(在多的一方建立外键)或者中间表来关联起来

<resultMap>:配置字段和对象属性的映射关系标签。
       id 属性:唯一标识
       type 属性:实体对象类型
   <id>:配置主键映射关系标签。
   <result>:配置非主键映射关系标签。
       column 属性:表中字段名称
       property 属性: 实体对象变量名称
   <collection>:配置被包含集合对象的映射关系标签。
       property 属性:被包含集合对象的变量名
       ofType 属性:集合中保存的对象数据类型

eg:
一个班级有多个学生
sql中两个主键都是id,以及name都相同故难以辨别,故起别名
sql:

CREATE TABLE classes(
     	id INT PRIMARY KEY AUTO_INCREMENT,
     	NAME VARCHAR(20)
     );
     INSERT INTO classes VALUES (NULL,'黑马一班');
     INSERT INTO classes VALUES (NULL,'黑马二班');
     
     
     CREATE TABLE student(
     	id INT PRIMARY KEY AUTO_INCREMENT,
     	NAME VARCHAR(30),
     	age INT,
     	cid INT,
     	CONSTRAINT cs_fk FOREIGN KEY (cid) REFERENCES classes(id)
     );
     INSERT INTO student VALUES (NULL,'张三',23,1);
     INSERT INTO student VALUES (NULL,'李四',24,1);
     INSERT INTO student VALUES (NULL,'王五',25,2);
     INSERT INTO student VALUES (NULL,'赵六',26,2);

pojo:

public class Classes{
	int id;
	String name;
	List<Student> students;//存放多个学生
}
public class Student{
	int id;
	String name;
	int age;
}

mapper:

<mapper namespace="com.itheima.table02.OneToManyMapper">
         <resultMap id="oneToMany" type="classes">
             <id column="cid" property="id"/>
             <result column="cname" property="name"/>
     
             <!--
                 collection:配置被包含的集合对象映射关系
                 property:被包含对象的变量名
                 ofType:被包含对象的实际数据类型
             -->
             <collection property="students" ofType="Student">
                 <id column="sid" property="id"/>
                 <result column="sname" property="name"/>
                 <result column="sage" property="age"/>
             </collection>
         </resultMap>
         <select id="selectAll" resultMap="oneToMany">
             SELECT c.id cid,c.name cname,s.id sid,s.name sname,s.age sage FROM classes c,student s WHERE c.id=s.cid
         </select>
     </mapper>
多对多操作

通过建立中间表(主键为两个表的主键的组合)来完成关联

eg:
一个学生选择多门课程,一门课程被多个学生选择
sql:

CREATE TABLE course(
     	id INT PRIMARY KEY AUTO_INCREMENT,
     	NAME VARCHAR(20)
     );
     INSERT INTO course VALUES (NULL,'语文');
     INSERT INTO course VALUES (NULL,'数学');
     
     
     CREATE TABLE stu_cr(
     	id INT PRIMARY KEY AUTO_INCREMENT,
     	sid INT,
     	cid INT,
     	CONSTRAINT sc_fk1 FOREIGN KEY (sid) REFERENCES student(id),
     	CONSTRAINT sc_fk2 FOREIGN KEY (cid) REFERENCES course(id)
     );
     INSERT INTO stu_cr VALUES (NULL,1,1);
     INSERT INTO stu_cr VALUES (NULL,1,2);
     INSERT INTO stu_cr VALUES (NULL,2,1);
     INSERT INTO stu_cr VALUES (NULL,2,2);

pojo:

public class Student{
	int id;
	String name;
	int age;
	List<Course> courses;
}
public class course{
	int id;
	String name;
}

StudentMapper:

<mapper namespace="com.itheima.table03.ManyToManyMapper">
         <resultMap id="manyToMany" type="student">
             <id column="sid" property="id"/>
             <result column="sname" property="name"/>
             <result column="sage" property="age"/>
     
             <collection property="courses" ofType="course">
                 <id column="cid" property="id"/>
                 <result column="cname" property="name"/>
             </collection>
         </resultMap>
         <select id="selectAll" resultMap="manyToMany">
             SELECT sc.sid,s.name sname,s.age sage,sc.cid,c.name cname FROM student s,course c,stu_cr sc WHERE sc.sid=s.id AND sc.cid=c.id
         </select>
     </mapper>

同理,需要在课程中查询选课的学生时只需要在course实体类中添加List< Student >属性后在courseMapper中完成ResultMap的配置即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值