赞
踩
一对多的理解:
数据库可参考多对一。
下面的例子是在SpringBoot环境下搭建的。这里有两种方式来进行多对一的查询。
按照查询进行嵌套处理就像SQL中的子查询
按照结果进行嵌套处理就像SQL中的联表查询
只举一种来说,按照结果进行嵌套处理就像SQL中的联表查询。比较好理解。
1. pom.xml 依赖
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter</artifactId>
- </dependency>
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>2.1.1</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <scope>test</scope>
- </dependency>

2. application.properties
- mybatis.mapper-locations=classpath:mapper/*.xml
- mybatis.type-aliases-package=com.cc.pojo
- mybatis.config-location=classpath:mybatis-config.xml
-
- spring.datasource.username=root
- spring.datasource.password=123456
- spring.datasource.url=jdbc:mysql://localhost:3306/mis?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8
3.日志配置(这里采用mybatis标准的日志。STDOUT_LOGGING。)
mybatis-config.xml
- <?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>
- <settings>
- <setting name="logImpl" value="STDOUT_LOGGING" />
- </settings>
- </configuration>
4 主要代码
1) pojo包下的Student类,Teacher类。
package com.cc.pojo;
- package com.cc.pojo;
-
- import lombok.Data;
-
- public class Student {
- private int id;
- private String name;
- private int tid;
- //GET,SET,ToString,有参,无参构造
- }
- package com.cc.pojo;
-
- import java.util.List;
-
- public class Teacher {
- private int id;
- private String name;
- //一个老师多个学生
- private List<Student> students;
- }
2)mapper下的接口。
package com.cc.mapper;
- package com.cc.mapper;
-
- import org.apache.ibatis.annotations.Mapper;
-
- @Mapper
- public interface StudentMapper {
-
- }
- package com.cc.mapper;
-
- import com.cc.pojo.Teacher;
- import org.apache.ibatis.annotations.Mapper;
-
- @Mapper
- public interface TeacherMapper {
- //获取指定老师,及老师下的所有学生
- public Teacher getTeacher(int id);
- }
3)编写Mapper接口对应的 mapper.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="com.cc.mapper.StudentMapper">
-
- </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="com.cc.mapper.TeacherMapper">
- <!--
- 思路:
- 1. 从学生表和老师表中查出学生id,学生姓名,老师姓名
- 2. 对查询出来的操作做结果集映射
- 1. 集合的话,使用collection!
- JavaType和ofType都是用来指定对象类型的
- JavaType是用来指定pojo中属性的类型
- ofType指定的是映射到list集合属性中pojo的类型。
- -->
- <select id="getTeacher" resultMap="TeacherStudent">
- select s.id sid, s.name sname , t.name tname, t.id tid
- from student s,teacher t
- where s.tid = t.id and t.id=#{tid}
- </select>
-
- <resultMap id="TeacherStudent" type="Teacher">
- <result property="id" column="tid"/>
- <result property="name" column="tname"/>
- <collection property="students" ofType="Student">
- <result property="id" column="sid" />
- <result property="name" column="sname" />
- <result property="tid" column="tid" />
- </collection>
- </resultMap>
- </mapper>

4)测试
- package com.cc;
-
- import com.cc.mapper.TeacherMapper;
- import com.cc.pojo.Teacher;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.test.context.junit4.SpringRunner;
-
- @RunWith(SpringRunner.class)
- @SpringBootTest
- public class TeacherTest {
-
- @Autowired
- private TeacherMapper teacherMapper;
-
- @Test
- public void testTeacher() {
- Teacher teacher = teacherMapper.getTeacher(2);
- System.out.println(teacher);
- }
- }

5)运行结果
Teacher{id=2, name='大老师', students=[Student(id=1, name=小明, tid=2), Student(id=2, name=小红, tid=2), Student(id=3, name=小张, tid=2), Student(id=4, name=小李, tid=2), Student(id=5, name=小王, tid=2)]}
注意:resultMap映射的时候属性不能少,不然输不出来。
对于多对一和一对多的小结:
- 关联-association
- 集合-collection
- 所以association是用于一对一和多对一,而collection是用于一对多的关系
JavaType和ofType都是用来指定对象类型的
- JavaType是用来指定pojo中属性的类型
- ofType指定的是映射到list集合属性中pojo的类型。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。