CREATE TABLE teacher( t_id INT PRIMARY KEY AUTO_INCREMENT, t_name VARCHAR(20) ); CREATE TABLE class( c_id INT PRIMARY KEY AUTO_INCREMENT, c_name VARCHAR(20), teacher_id INT ); ALTER TABLE class ADD CONSTRAINT fk_teacher_id FOREIGN KEY (teacher_id) REFERENCES teacher(t_id); INSERT INTO teacher(t_name) VALUES('teacher1'); INSERT INTO teacher(t_name) VALUES('teacher2'); INSERT INTO class(c_name, teacher_id) VALUES('class_a', 1); INSERT INTO class(c_name, teacher_id) VALUES('class_b', 2);
package com.test.model; //Teacher實體類 public class TeacherEntity { private int tid; private String tname; private ClassEntity classEntity; public void setTid(Integer tid) { this.tid = tid; } public Integer getTid() { return tid; } public void setTname(String tname) { this.tname = tname; } public String getTname() { return tname; } public void setClassEntity(ClassEntity classEntity) { this.classEntity = classEntity; } public ClassEntity getClassEntity() { return classEntity; } }
package com.test.model; //Class實體類 public class ClassEntity { private int cid; private String cname; private int teacherid; public void setCid(Integer cid) { this.cid = cid; } public Integer getCid() { return cid; } public void setCname(String cname) { this.cname = cname; } public String getCname() { return cname; } public void setTeacherid(Integer teacherid) { this.teacherid = teacherid; } public Integer getTeacherid() { return teacherid; } }
這裏須要注意的是,在TeacherEntity類裏申明ClassEntity實體java
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.test.dao.TeacherMapper" > <resultMap type="com.test.model.TeacherEntity" id="resultTeacherList">
<!-- 用id屬性來映射主鍵字段 -->
<id property="tid" column="t_id"/>
<!-- 用result屬性來映射非主鍵字段 -->
<result property="tname" column="t_name"/>
<association property="classEntity" javaType="com.test.model.ClassEntity"> <id property="cid" column="c_id"/> <id property="cname" column="c_name"/> <id property="teacherid" column="teacher_id"/> </association> </resultMap> <select id="getList" resultType="com.test.model.TeacherEntity" resultMap="resultTeacherList"> select t.t_id,t.t_name,c.c_id,c.c_name,c.teacher_id from teacher t,class c where t.t_id=c.teacher_id </select> </mapper>
MyBatis中使用association標籤來解決一對一的關聯查詢,association標籤可用的屬性以下:spring
一、resultMap > type 是TeacherEntity實體類sql
二、resultMap > id屬性,要和下面select > resultMap對應數據庫
三、resultMap 下的id標籤對應sql語句查詢teacther表的字段json
四、association > property的值是TeacherEntity裏申明的ClassEntity的屬性mybatis
五、association > javaType的值是ClassEntity實體類app
六、association 下的 id標籤是對應sql語句查詢class表的字段單元測試
補充說明:測試
package com.test.dao; import com.test.model.TeacherEntity; import java.util.List; public interface TeacherMapper { List<TeacherEntity> getList(); }
package com.test.controller; import com.test.dao.TeacherMapper; import com.test.model.TeacherEntity; import net.sf.json.JSONArray; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:applicationContext.xml", "classpath:dispatcher-servlet.xml"}) public class test { @Autowired TeacherMapper teacherMapper; @Test public void demo(){ List<TeacherEntity> teacherList =teacherMapper.getList(); System.out.println(JSONArray.fromObject(teacherList)); } }
[{ "tname": "teacher1", "classEntity": { "teacherid": 1, "cname": "class_a", "cid": 1 }, "tid": 1 }, { "tname": "teacher2", "classEntity": { "teacherid": 2, "cname": "class_b", "cid": 2 }, "tid": 2 }]