标签:student res 使用 engine name 需要 string efault table
准备sql
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‘);
做法: select s.id,s.name,t.name from student s,teacher t where s.tid=t.id;
用mybatis实现,类似子查询 ,法一:
<mapper namespace="com.kuang.dao.StudentMapper"> <!--思路:1.查询所有学生的信息.2.根据查询出来的学生的tid,寻找对应的老师--> <select id="getStudent" resultMap="StudentTeacher"> select * from student </select> 《其中id表示他的标识,type表示那种类作为其映射的类》 <resultMap id="StudentTeacher" type="Student"> <result property="name" column="name"/> <!--复杂的属性,我们需要单独处理对像:association 集合:collection--> <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/> </resultMap> <select id="getTeacher" resultType="com.kuang.pojo.Teacher"> select * from teacher where id=#{id} </select> ? </mapper>
法二:
<select id="getStudent2" resultMap="StudentTeacher2"> select s.id sid,s.name sname,t.name tname from student s,teacher t where s.tid=t.id; </select> <resultMap id="StudentTeacher2" type="com.kuang.pojo.Student"> <result property="id" column="sid"/> <result property="name" column="sname"/> <association property="teacher" javaType="com.kuang.pojo.Teacher"> <result property="name" column="tname"/> </association> </resultMap>
注意 : <!--复杂的属性,我们需要单独处理对像:association 集合:collection-->
实体类
public class Student { private int id; private String name; private int tid; public class Teacher { private int id; private String name; private List<Student> students;
<mapper namespace="com.kuang.dao.TeacherMapper"> <select id="getTeacher" resultMap="TeacherStudent"> select s.id sid,s.name sname,t.name tname,t.id tid from teacher t,student s where s.id=t.id and t.id=#{tid} </select> <resultMap id="TeacherStudent" type="com.kuang.pojo.Teacher"> <result property="id" column="tid"/> <result property="name" column="tname"/> <!--复杂的属性,我们需要单独处理 对象:association 集合:collection javaType=“”指定属性的类型 集合中的泛型信息,我们使用ofType获取--> <collection property="students" ofType="com.kuang.pojo.Student"> <result property="id" column="sid"/> <result property="name" column="sname"/> <result property="tid" column="tid"/> </collection> </resultMap> </mapper>
小结:
1.关联-association【多对一】
2.集合-collection【一对多】
JavaType & ofType
JavaType 用来指定实体类中属性的类型
ofType用来指定映射到List或者集合中pojo类型,反型中的约束类型
标签:student res 使用 engine name 需要 string efault table
原文地址:https://www.cnblogs.com/Liguangyang/p/12740513.html