标签:turn ltm utf-8 close vat eve property color span
package com.springmvc.domain; public class Student { private int id; private String studentname; private int tid; private int classid; public int getTid() { return tid; } public void setTid(int tid) { this.tid = tid; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getStudentname() { return studentname; } public void setStudentname(String studentname) { this.studentname = studentname; } public int getClassid() { return classid; } public void setClassid(int classid) { this.classid = classid; } @Override public String toString() { return "Student{" + "id=" + id + ", studentname=‘" + studentname + ‘\‘‘ + ", tid=" + tid + ", classid=" + classid + ‘}‘; } }
Teacher类
package com.springmvc.domain; import java.util.List; public class Teacher { private int id; private String name; private List<Student> list; 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; } public List<Student> getList() { return list; } public void setList(List<Student> list) { this.list = list; } @Override public String toString() { return "Teacher{" + "id=" + id + ", name=‘" + name + ‘\‘‘ + ", list=" + list + ‘}‘; } }
StudentDao.interface
package com.springmvc.dao; import com.springmvc.domain.Student; import java.util.List; public interface StudentMapper { /*List<Student> selectAll();*/ List<Student> selectByTid(int id); /*List<Student> selectByCid(int cid);*/ }
TeacherDao.interface
package com.springmvc.dao; import com.springmvc.domain.Teacher; import java.util.List; public interface TeacherMapper { /*Teacher selectAll(int id);*/ List<Teacher> selAll(); List<Teacher> selAll1(); }
Mapper:
TeacherMapper:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="com.springmvc.dao.TeacherMapper"> 6 <!--<select id="selectAll" resultType="com.springmvc.domain.Teacher"> 7 select * from teacher where id=#{0} 8 </select>--> 9 10 <select id="selAll" resultMap="teachermap"> 11 select * from teacher 12 </select> 13 //方式一 14 <resultMap id="teachermap" type="Teacher"> 15 <id property="id" column="id"/> 16 <result property="name" column="name"/> 17 <collection property="list" ofType="Student" select="com.springmvc.dao.StudentMapper.selectByTid" column="id"/> 18 </resultMap> 19 20 //方式二:使用左连接查询 21 <select id="selAll1" resultMap="teacher1"> 22 select t.*,s.* 23 from teacher t left join student s on t.id = s.tid 24 </select> 25 26 <resultMap id="teacher1" type="com.springmvc.domain.Teacher"> 27 <id property="id" column="id"/> 28 <result property="name" column="name"/> 29 <collection property="list" ofType="com.springmvc.domain.Student"> 30 <id property="id" column="id"/> 31 <result property="studentname" column="studentname"/> 32 <result property="tid" column="tid"/> 33 <result column="classid" property="classid"/> 34 </collection> 35 </resultMap> 36 37 38 39 </mapper>
StudentMapper:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="com.springmvc.dao.StudentMapper" > 6 <!-- <select id="selectAll" resultMap="student"> 7 select * from student 8 </select>--> 9 <select id="selectByTid" resultType="Student"> 10 select * from student where tid=#{0}; 11 </select> 12 13 14 <!-- <resultMap id="student" type="com.springmvc.domain.Student"> 15 <id property="id" column="_id"></id> 16 <result property="studentName" column="student_name"></result> 17 <result property="tid" column="t_id"/> 18 <association property="teacher" select="com.springmvc.dao.TeacherMapper.selectAll" column="t_id"></association> 19 </resultMap>--> 20 21 22 23 24 </mapper>
标签:turn ltm utf-8 close vat eve property color span
原文地址:https://www.cnblogs.com/liuruijie/p/12001144.html