平時(shí)在開(kāi)發(fā)過(guò)程中dao、bean和XML文件都是自動(dòng)生成的,很少寫(xiě)XML的配置關(guān)系,今天記錄一下mybatis的關(guān)聯(lián)查詢中的多對(duì)一和一對(duì)多的情況。
- 首先是有兩張表(學(xué)生表Student和老師Teacher表),為了更易懂,這里只設(shè)置了最簡(jiǎn)單的幾個(gè)必要字段。表結(jié)構(gòu)如下圖
Student表:

Teacher表:

Teacher.java:
* @version 創(chuàng)建時(shí)間:2017年12月21日 上午9:02:45 private String className; private List<Student> students; public List<Student> getStudents() { public void setStudents(List<Student> students) { this.students = students; public void setId(Integer id) { public String getName() { public void setName(String name) { public String getClassName() { public void setClassName(String className) { this.className = className;
Sfudent.java
* @author 作者 E-mail:2332999366@qq.com * @version 創(chuàng)建時(shí)間:2017年12月21日 上午9:01:17 private Integer teacherId; private String className; public Teacher getTeacher() { public void setTeacher(Teacher teacher) { public void setId(Integer id) { public String getName() { public void setName(String name) { public Integer getTeacherId() { public void setTeacherId(Integer teacherId) { this.teacherId = teacherId; public String getClassName() { public void setClassName(String className) { this.className = className; public String toString() { return "{id:"+this.id+",name:"+this.name+",className:"+this.className+",teacherId:"+this.teacherId+"}";
- 下面重點(diǎn)來(lái)了:配置Mapper.xml文件:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "http:///dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.tz.mybatis.dao.studentDao"> <!-- /////////////////////////////////一對(duì)多的第一種寫(xiě)法,一般考慮到性能問(wèn)題,不會(huì)這么實(shí)現(xiàn)//////////////////////// --> <resultMap type="Teacher" id="teacherMap"> <id column="id" property="id"/> <result column="name" property="name"/> <collection property="students" ofType="Student" column="id"> <id column="sid" property="id"/><!-- 這里的column對(duì)應(yīng)的是下面查詢的別名,而不是表字段名 --> <result column="sname" property="name"/><!-- property對(duì)應(yīng)JavaBean中的屬性名 --> <result column="className" property="className"/> <!-- 查詢所有的老師級(jí)各自的所有學(xué)生 --> <select id="getTeachers" parameterType="Teacher" resultMap="teacherMap"> s.class_name as className LEFT JOIN student s ON t.id = s.teacher_id
import java.io.IOException; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.tz.mybatis.bean.Student; import com.tz.mybatis.bean.Teacher; public class TeacherTest { private SqlSessionFactory sqlSessionFactory; public void init() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); public void getTeachers() { SqlSession session = sqlSessionFactory.openSession(); List<Teacher> list = session.selectList("com.tz.mybatis.dao.studentDao.getTeachers"); System.out.println(list);
下面給出第二種寫(xiě)法:
<!-- //////////////////////////////////////////////一對(duì)多的第二種寫(xiě)法///////////////////////////////////////////////////// --> <resultMap type="Teacher" id="teacherMaps"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="class_name" property="className"/> <collection property="students" ofType="Student" select="getStudents" column="id"> <!-- 查詢所有的老師級(jí)各自的所有學(xué)生 --> <select id="getAllTeacher" parameterType="Teacher" resultMap="teacherMaps"> <select id="getStudents" parameterType="int" resultType="Student"> s.class_name as className
測(cè)試類:
public void getTeachers2() { SqlSession session = sqlSessionFactory.openSession(); List<Teacher> list = session.selectList("com.tz.mybatis.dao.studentDao.getAllTeacher"); System.out.println(list);
查詢學(xué)生信息(多對(duì)一):
首先還是配置文件:
<resultMap type="Student" id="studentMap"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="class_name" property="className"/> <result column="teacher_id" property="teacherId"/> <association property="teacher" select="getTeacher" column="teacher_id" javaType="Teacher"> <!-- 這里要注意的是column對(duì)應(yīng)的是student中的外鍵,而且需是表字段名 --> <select id="getStudent" resultMap="studentMap"> <select id="getTeacher" resultType="Teacher" parameterType="int"> t.class_name as className
測(cè)試類:
public void getStudents() { SqlSession session = sqlSessionFactory.openSession(); List<Student> list = session.selectList("com.tz.mybatis.dao.studentDao.getStudent"); System.out.println(list);
最后:當(dāng)然如果不想配置這么麻煩的信息,可以直接寫(xiě)一個(gè)關(guān)聯(lián)查詢的SQL語(yǔ)句,返回結(jié)果直接由Map接受即可。不過(guò)這樣就不太符合面向?qū)ο蟮睦砟盍恕?br>
|