1、多对一问题【association】
-
对于学生这边而言,多个学生,关联一个老师【多对一】 关联
-
对于老师而言,一个老师有很多学生【一对多】 集合
1.1、首先需要搭建一个数据库的环境
CREATE TABLE `teacher` (
`id` INT(10) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
INSERT INTO teacher(`id`, `name`) VALUES (1,"秦老师");
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
INSERT INTO `student` (`id`, `name`, `tid`) VALUES (1,"小明", 1);
INSERT INTO `student` (`id`, `name`, `tid`) VALUES (2,"小红", 1);
INSERT INTO `student` (`id`, `name`, `tid`) VALUES (3,"小张", 1);
INSERT INTO `student` (`id`, `name`, `tid`) VALUES (4,"小李", 1);
INSERT INTO `student` (`id`, `name`, `tid`) VALUES (5,"小王", 1);
1.2、导入lombok
<dependencies>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<!--在build中配置resources,来防止我们资源导出失败的问题-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
1.3、新建实体类Teacher Student
@Data
public class Teacher {
private int id;
private String name;
}
@Data
public class Student {
private int id;
private String name;
private Teacher teacher;
}
1.4、建立Mapper接口
public interface TeacherMapper {
@Select("select * from teacher where id = #{tid}")
Teacher getTeacher(@Param("tid") int id);
}
public interface StudentMapper {
//查询所有的学生信息,以及对应老师的信息
public List<Student> getStudent();
public List<Student> getStudent2();
}
1.5、建立Mapper.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.jl.dao.TeacherMapper">
</mapper>
<?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.jl.dao.StudentMapper">
</mapper>
1.6、在核心配置文件中绑定注册我们的Mapper接口或文件!
<!--绑定接口-->
<mappers>
<mapper class="com.jl.dao.TeacherMapper"></mapper>
<mapper class="com.jl.dao.StudentMapper"></mapper>
</mappers>
1.7、测试能否成功!
public class Test {
public static void main(String[] args) {
SqlSession sqlSession = MybatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher = mapper.getTeacher(1);
System.out.println(teacher);
sqlSession.close();
}
}
出现过的问题:
如果出现以上问题,只需要把jdk9换成jdk8即可,我在上面配置的pom.xml文件中已经换成jdk8了。
按照查询嵌套处理
<!--
思路:1查询所有的学生信息
2.根据查询出来的学生tid,寻找对应的老师 子查询 -->
<select id="getStudent" resultMap="Student">
select * from student
</select>
<resultMap id="Student" type="com.jl.pojo.Student">
<!--复杂的属性我们需要单独处理 对象:association 集合:collection-->
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="com.jl.pojo.Teacher">
select * from teacher where id=#{tid}
</select>
按照结果嵌套查询
<!--按照结果嵌套处理-->
<select id="getStudent2" resultMap="StudentMap">
select s.id sid,s.name sname,t.name tname,t.id
from student s,teacher t
where s.tid=t.id;
</select>
<resultMap id="StudentMap" type="com.jl.pojo.Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="Teacher" >
<result property="name" column="tname"/>
<result property="id" column="id"/>
</association>
</resultMap>
自己的出现的问题:
在resultMap中用com.jl.pojo.Student会报错
必须在mybatis核心配置文件中起别名
<typeAliases>
<typeAlias type="com.jl.pojo.Teacher" alias="Teacher"/>
<typeAlias type="com.jl.pojo.Student" alias="Student"/>
</typeAliases>
2、一对多的处理【collection】
2.1、实体类
@Data
public class Teacher {
private int id;
private String name;
//一个老师拥有多个学生
private List<Student> students;
}
@Data
public class Student {
private int id;
private String name;
private int tid;
}
2.2、接口类
public interface TeacherMapper {
//获取老师
List<Teacher> teacher();
//获取指定老师下的所有学生及老师的信息
Teacher getTeacher(@Param("tid") int id);
}
2.3、按照查询嵌套处理:
Mapper.xml配置文件
<select id="getTeacher2" resultMap="Teacher">
select * from mybetis.teacher where id=#{tid};
</select>
<resultMap id="Teacher" type="Teacher">
<collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId" column="id"/>
</resultMap>
<select id="getStudentByTeacherId" resultType="Student">
select * from mybatis.student where tid=#{tid};
</select>
2.4、按照结果嵌套查询:
Mapper.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.jl.dao.TeacherMapper">
<!--按结果嵌套查询-->
<select id="getTeacher" resultMap="Teacher">
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>
<!--select的resultMap的值与下面resultMap id的值相同-->
<resultMap id="Teacher" type="Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<!--复杂的属性我们需要单独处理 对象:association 集合: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>
</mapper>
2.5、测试类
public class MyTest {
@Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
List<Teacher> teacherList = mapper.teacher();
for (Teacher teacher : teacherList) {
System.out.println(teacher);
}
sqlSession.close();
}
@Test
public void getTeacher(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher = mapper.getTeacher(1);
System.out.println(teacher);
sqlSession.close();
}
}
小结:
1.关联:association 【多对一】
2.集合:collection 【一对多】
3.javaType & ofType
- JavaType 用来指定实体类中属性的类型
- ofType 用来指定映射到List或者集合中的pojo类型,泛型中的约束类型