mybatis的学习7___一对多的实现
(本案列为maven项目下进行:)
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');
需求:通过老师的编号,查找到,该老师下面的所有学生信息(id,name):
2.老师pojo类的编写:
由于一个老师对应着多个学生,所以老师类中存在属性:List<Student> studentList
public class Teacher { private int id; private String name; private List<Student> studentList; public Teacher() { } public Teacher(int id, String name, List<Student> studentList) { this.id = id; this.name = name; this.studentList = studentList; } public List<Student> getStudentList() { return studentList; } public void setStudentList(List<Student> studentList) { this.studentList = studentList; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Teacher{" + "id=" + id + ", name='" + name + '\'' + ", studentList=" + studentList + '}'; } }
3.Dao层TeacherMapper接口的编写:
public interface TeacherDao { //获取老师和老师下学生信息 Teacher getInfo(int id); //获取老师和老师下学生信息 Teacher getTeacherTwo(int id); }
4.接口的xml配置文件的编写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.xbf.dao.TeacherDao"> <!--面向对象的处理方式--> <!--使用 collection标签--> <select id="getInfo" resultMap="T"> select s.id as sid,s.name as sname,t.name as tname from student s,teacher t where t.id=s.tid and t.id=#{id} </select> <resultMap id="T" type="Teacher"> <result column="tname" property="name"/> <collection property="studentList" ofType="Student"> <result column="sid" property="id"/> <result column="sname" property="name"/> </collection> </resultMap> <!--数据库的方式--> <select id="getTeacherTwo" resultMap="TeacherStudent2"> select * from mybatis.teacher where id = #{id} </select> <resultMap id="TeacherStudent2" type="Teacher"> <collection property="studentList" javaType="ArrayList" ofType="Student" column="id" select="T2"/> </resultMap> <select id="T2" resultType="Student"> select * from mybatis.student where tid = #{id} </select> </mapper>
5.mybatis-config.xml的映射器:
<mappers> <mapper resource="com/xbf/dao/teacherMapper.xml"></mapper> </mappers>
6.测试类的编写:
public class StudentDaoTest { @Test public void getInfoTest(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); TeacherDao mapper = sqlSession.getMapper(TeacherDao.class); Teacher teacher = mapper.getInfo(1); System.out.println(teacher); } @Test public void getTeacherTwo(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); TeacherDao mapper = sqlSession.getMapper(TeacherDao.class); Teacher teacher = mapper.getTeacherTwo(1); System.out.println(teacher.getName()); System.out.println(teacher.getStudentList()); } }
总结:
一对多使用: collection
项目第一次跑的时候报错:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.xbf.dao.Teacher
后来仔细检查发现,在maven导出的target目录中找不到 teacherMapper.xml,因为src/java中的.xml或.properties资源配置文件没有导入。
在maven的pom.xml中添加如下代码后,在target文件中找到了 teacherMapper.xml,成功运行!
<build> <!--希望maven在导出项目的时候(target),能够将我们的配置及资源导出--> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>