MyBatis學習總結—實現關聯表查詢

一、數據庫表結構

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);

二、定義實體

  2.1 TeacherEntity類,TeacherEntity類是teacher表對應的實體類。

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;
    }
}

  2.2 ClassEntity類,ClassEntity類是class表對應的實體類。

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

三、定義sql映射文件TeacthMapper.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.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表的字段單元測試

  補充說明:測試

  • property:對象屬性的名稱
  • javaType:對象屬性的類型
  • column:所對應的外鍵字段名稱
  • select:使用另外一個查詢封裝的結果

四、定義TeacthMapper.java查詢實現接口

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
}]
相關文章
相關標籤/搜索