一、多对一处理
多对一:
多个学生对应一个老师
对于学生而言,关联一个老师 【多对一】
对于老师而言,集合,一个老师,和很多学生【一对多】
SQL:数据自己插一些
CREATE table `teacher` (
`id` int(10) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (`id`)
)ENGINE=INNODB DEFAULT CHARSET=utf8
CREATE TABLE `student`(
`id` int(10) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
`tid` INT(10) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fktid` (`tid`),
CONSTRAINT `fktid` FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`)
)ENGINE=INNODB DEFAULT CHARSET=utf8
测试环境搭建
结构:
1.导入lombok
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
2.新建实体类
Teachar类
package com.shy.pojo;
import lombok.Data;
@Data
public class Teacher {
private int id;
private String name;
}
Student类
package com.shy.pojo;
import lombok.Data;
@Data
public class Student {
private int id;
private String name;
//学生需要关联一个老师
private Teacher teacher;
}
3.编写Mapper接口
TeacherMapper
package com.shy.dao;
import com.shy.pojo.Teacher;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
public interface TeacherMapper {
@Select("select * from teacher where id = #{tid}")
Teacher getTeacher(@Param("tid") int id);
}
StudentMapper
package com.shy.dao;
import com.shy.pojo.Student;
import java.util.List;
public interface StudentMapper {
//查询所有信息
List<Student> getStu();
List<Student> getStu2();
}
4.编写Mapper.xml文件,**无论有没有需求,都应该写上,以备后来之需!**这里需要都写上,因为TeacherMapper.xml之后会用到
StudentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--configuration核心配置文件-->
<mapper namespace="com.shy.dao.StudentMapper">
<!--方式一:按照结果嵌套处理-->
<select id="getStu2" resultMap="StuTea2">
select s.id sid,s.name sname,t.name tname
from student s,teacher t
where s.tid = t.id
</select>
<resultMap id="StuTea2" type="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="Teacher">
<result property="name" column="tname"/>
</association>
</resultMap>
<!--方式二-->
<!--
思路:
1.查询所有的学生信息
2.根据查询出来的学生的tid,寻找对应的老师
-->
<select id="getStu" resultMap="StudentTeacher">
select * from student
</select>
<resultMap id="StudentTeacher" type="Student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<!--复杂的属性需要单独处理-->
<!--对象 association
集合 collection
-->
<!--association关联属性 property属性名 javaType属性类型
column在多的一方的表中的列名,这里的tid是学生表中的tid-->
<!--
这里传递过来的id,只有一个属性的时候,下面可以写任何值
association中column多参数配置:
column="{key=value,key=value}"
其实就是键值对的形式,key是传给下个sql的取值名称,value是片段一中sql查询的字段名。
-->
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="Teacher">
select * from teacher where id = #{id}
</select>
</mapper>
TeacherMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--configuration核心配置文件-->
<mapper namespace="com.shy.dao.TeacherMapper">
</mapper>
5.在核心配置中绑定注册Mapper接口或者文件
<!--可以给实体类取别名-->
<typeAliases>
<package name="com.shy.pojo"/>
</typeAliases>
<mappers>
<mapper class="com.shy.dao.TeacherMapper"/>
<mapper class="com.shy.dao.StudentMapper"/>
</mappers>
6.测试
import com.shy.dao.StudentMapper;
import com.shy.dao.TeacherMapper;
import com.shy.pojo.Student;
import com.shy.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class MyTest {
@Test
public void getTea(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
mapper.getTeacher(1)
sqlSession.close();
}
@Test
public void getStu(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> stu = mapper.getStu();
for (Student student : stu) {
System.out.println(student);
}
sqlSession.close();
}
@Test
public void getStu2(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> stu = mapper.getStu2();
for (Student student : stu) {
System.out.println(student);
}
sqlSession.close();
}
}
多对一查询方式
按照查询进行嵌套处理就像SQL中的子查询
按照结果进行嵌套处理就像SQL中的联表查询
二、一对多处理
一个老师有多个学生
1.环境搭建
有重复代码直接复制就行
结构:
Lombok
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
</dependencies>
TeacherMapper接口
package com.shy.dao;
import com.shy.pojo.Teacher;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface TeacherMapper {
//获取指定老师下的学生信息及老师信息:方式一
Teacher getTea(@Param("tid") int id);
//获取指定老师下的学生信息及老师信息:方式二
Teacher getTea2(@Param("tid") int id);
}
实体类:
@Data
public class Student {
private int id;
private String name;
//学生需要关联一个老师
private int tid;
}
@Data
public class Teacher {
private int id;
private String name;
//一个老师有多个学生
private List<Student> students;
}
按照结果嵌套处理
<!--按照结果嵌套查询-->
<select id="getTea" resultMap="TeaStu">
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="TeaStu" type="Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<!--
集合的话,使用collection
javaType=""指定属性的类型
集合中的泛型信息使用 ofType获取
-->
<collection property="students" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
按照查询嵌套处理
<select id="getTea2" resultMap="TeaStu2">
select * from teacher where id = #{tid}
</select>
<resultMap id="TeaStu2" type="Teacher">
<collection property="students" javaType="ArrayList" ofType="Student" select="getStuByTeaId" column="id"/>
</resultMap>
<select id="getStuByTeaId" resultType="Student">
select * from student where tid = #{tid}
</select>
测试:
import com.shy.dao.TeacherMapper;
import com.shy.pojo.Teacher;
import com.shy.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
public class MyTest {
@Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher tea = mapper.getTea(1);
System.out.println(tea);
sqlSession.close();
}
@Test
public void test2(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher tea = mapper.getTea2(1);
System.out.println(tea);
sqlSession.close();
}
}
小结:
1.关联–association 【多对一】
2.集合–collection 【一对多】
3.javaType:用来指定实体类中属性的类型
ofType:用来指定映射到List或者集合中的pojo类型,泛型指定约束类型
注意:
- 保证sql的可读性,通俗易懂
- 注意一对多和多对一中,属性名和字段的问题
- 如果问题不好排查错误,可以使用日志。建议使用log4j
Lombok不能用的,去下Lombok插件,然后开启注解支持~