MyBatis一對一以及一對多關聯表查詢

2021-08-02 00:19:49 字數 820 閱讀 7255

當前資料庫中存在三個表clazz和teacher以及student,資料字典如下:

clazz(c_id,c_name,teacher_id)

teacher(t_id,t_name)

student(s_id,s_name,class_id)

三個表對應的實體類如下:

public class clazz
public class teacher
public class student
配置檔案如下,需要解釋的地方都在**中做了相應的解釋:

<?xml version="1.0" encoding="utf-8"?>

select * from clazz c,teacher t,student s where c.teacher_id = t.t_id and s.class_id = c.c_id and c.c_id = #

select * from clazz where c_id = #;

select t_id id,t_name name from teacher where t_id = #

select s_id id,s_name name from student where class_id = #

測試類如下:

public class clazzcrud 

@test

public void getclazz2()

}

mybatis之一對一,一對多

resulttype 直接表示返回型別 基本資料型別 引用資料型別 resultmap 對外部resultmap 資料庫字段資訊和物件屬性不一樣 複雜的聯合查詢,自由控制對映結果 注 它們兩個不能同時存在 association select from class c,teacher t where...

Mybatis註解實現 一對一對映 一對多對映

results id usermap value one 相當於 xml 檔案中的 association 標籤 column 代表資料庫中的字段 property 代表 pojo 類中的屬性 和上面的column 形成一一對應的關係 results 註解對應的 id值 可以使用 resultmap...

JavaWeb mybatis一對一 一對多查詢

mybatis查詢基本返回資料基本用到的都是resulttype與resultmap,resulttype必須po類與sql欄位一致,而resultmap可以不一致通過配置對映。本篇文章一是要講解resulttype與resultmap如何使用 二是重點講解一對一與一對多查詢resultmap的使用...