首页 > 代码库 > Mybatis配置文件双向关联出现的问题
Mybatis配置文件双向关联出现的问题
今天在配置mybatis的双向关联的时候,遇到了一些问题,现在在这里总结一下:
现在有学生和老师两个实体:
student(N): teacher (1)
数据库的关联:
Supervisor_id表示的是老师的id;是外键
Sql语句:
/*
Navicat MySQL Data Transfer
Source Server : test
Source Server Version : 50528
Source Host : localhost:3306
Source Database : courseman
Target Server Type : MYSQL
Target Server Version : 50528
File Encoding : 65001
Date: 2014-10-30 10:58:45
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT ,
`name` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`gender` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`major` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`grade` char(4) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`supervisor_id` int(11) NULL DEFAULT NULL ,
PRIMARY KEY (`id`),
FOREIGN KEY (`supervisor_id`) REFERENCES `teacher` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
INDEX `s_t` (`supervisor_id`) USING BTREE
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=22
;
-- ----------------------------
-- Records of student
-- ----------------------------
BEGIN;
INSERT INTO `student` VALUES (‘1‘, ‘陈三勇1‘, ‘男‘, ‘计算机科学技术‘, ‘100‘, ‘2‘), (‘2‘, ‘老白2‘, ‘男‘, ‘计算‘, ‘100‘, ‘2‘), (‘21‘, ‘陈三勇3‘, ‘男‘, ‘科学技术‘, ‘11‘, ‘2‘);
COMMIT;
-- ----------------------------
-- Table structure for teacher
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
`id` int(11) NOT NULL AUTO_INCREMENT ,
`name` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`gender` varchar(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`research_area` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`title` varchar(6) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=4
;
-- ----------------------------
-- Records of teacher
-- ----------------------------
BEGIN;
INSERT INTO `teacher` VALUES (‘1‘, ‘老师1‘, ‘男‘, ‘计算器‘, ‘科学‘), (‘2‘, ‘老师2‘, ‘男‘, ‘科学‘, ‘科学‘), (‘3‘, ‘老师3‘, ‘男‘, ‘数学‘, ‘数学‘);
COMMIT;
-- ----------------------------
-- Auto increment value for student
-- ----------------------------
ALTER TABLE `student` AUTO_INCREMENT=22;
-- ----------------------------
-- Auto increment value for teacher
-- ----------------------------
ALTER TABLE `teacher` AUTO_INCREMENT=4;
配置文件:
TeacherMapper.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.abc.mapper.TeacherMapper">
<resultMap type="Teacher" id="supervisorResultMap">
<id property="id" column="t_oid"/>
<result property="name" column="t_name"/>
<result property="gender" column="t_gender"/>
<result property="researchArea" column="t_research_area"/>
<result property="title" column="t_title"/>
<collection property="supStudent" ofType="com.abc.domian.Student" column="t_oid" select="com.abc.mapper.StudentMapper.getById" resultMap="com.abc.mapper.StudentMapper.StudentResultMap"></collection>
</resultMap>
<select id="findTeacherByPage" resultMap="supervisorResultMap">
select id as t_oid,name as t_name,gender as t_gender,research_area as t_research_area,title as t_title from teacher order by ${sort} ${dir} limit ${start},${limit}
</select>
</mapper>
StudentMapper.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.abc.mapper.StudentMapper">
<resultMap type="com.abc.domian.Student" id="StudentResultMap">
<id property="id" column="s_id"/>
<result property="name" column="s_name"/>
<result property="gender" column="s_gender"/>
<result property="major" column="s_major"/>
<result property="grade" column="s_grade"/>
<association property="supervisor" resultMap="com.abc.mapper.TeacherMapper.supervisorResultMap">
</association>
</resultMap>
<select id="getById" parameterType="int" resultMap="StudentResultMap">
select id as s_id,name as s_name,gender as s_gender,major as s_major,grade as s_grade from student where supervisor_id = #{id}
</select>
</mapper>
Student.java
public class Student {
private Integer id;
private String name;
private String gender;
private String major;
private String grade;
//添加教师
private Teacher supervisor;
setter/getter
}
Teacher:
public class Teacher {
private int id;
private String name;
private String gender;
private String researchArea;
private String title;
private List<Student> supStudent;
setter/getter
}
查询方法:该方法主要通过教师查找到学生
@RequestMapping(value="/queryTeacherByPage")
public ModelAndView queryTeacherByPage(){
TeacherMapper teacherMapper = (TeacherMapper) SpringContextUtil.getBean("teacherMapper");
List<Teacher> teachers = teacherMapper.findTeacherByPage("name", "asc", 0, 2);
for(int i = 0 ;i < teachers.size();i++){
Teacher teacher = teachers.get(i);
System.out.println("教师姓名"+" "+teacher.getName());
System.out.println("教师职称"+" "+teacher.getTitle());
System.out.println("指定学生信息"+" "+teacher.getName());
System.out.println("__________________________________");
for(Student s :teacher.getSupStudent()){
System.out.println(
s.getName() + " " + s.getGender() + " " + s.getGrade() + " "+ s.getMajor()
);
}
System.out.println("-----------------------------------------------------");
}
Student s = new Student();
s.setName("123");
return new ModelAndView("/show","student",s);
}
进行debug我们会发现,查出的学生的数据并不是我们想要得到的数据:
查询老师的数据时正确,当我看进行查看学生下面的老师的时候需要主要学生的个数;
当查看老师下面的学生个数的时候,发现,老师1没有学生,但是返回的size却不为0,为什么呢?
在student的配置文件中,我们配置了老师的关联即<association property="supervisor" resultMap="com.abc.mapper.TeacherMapper.supervisorResultMap"></association> 那么Mybatis会自动的去查询老师,并将查询的值复制给supervisor所以查询到的内容,size不为0;
如果我们去掉了上述的配置内容,观察查询出的内容:
这个查询的结果是老师教的学生数据;这样查询的结果才是我们想要的数据;
我们还需要注意一个问题,在关联查询的时候,如果配置文件中的Mapper.xml column配置的内容一样;即
(备注:如果查询到的teacher为空,可能是应该sql语句中起了别名,和resultMap的column不匹配导致的)
根据debug发现老师也被当成了学生,这样会导致查询出现错误,所以在书写Mapper的配置文件的时候,需要注意这个问题;
下载地址:http://pan.baidu.com/s/1gd1lOv5
Mybatis配置文件双向关联出现的问题