MyBatis學習之路(關聯查詢)

2021-09-02 13:24:17 字數 898 閱讀 5801

1.一對一關聯

mybatis中使用association標籤來解決一對一的關聯查詢,association標籤可用的屬性如下:

方法1:使用巢狀結果對映來處理,封裝聯表查詢的結果

select * from class c, teacher t where c.teacher_id=t.t_id and c.c_id=#

方法2:巢狀查詢,通過執行另乙個sql對映語句來返回預期的複雜型別

select * from class where c_id=#

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

2.一對多關聯

mybatis中使用collection標籤來解決一對多的關聯查詢,oftype屬性指定集合中元素的物件型別

方法1:巢狀結果,使用巢狀結果對映來處理重複的聯合結果的子集

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

方法2:

巢狀查詢:通過執行另外乙個sql對映語句來返回預期的複雜型別

select * from class 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=#

mybatis關聯查詢

備註 1 type是實體類 2 id是唯一標識,是resulmap指定的標識 4 collection是集合對映,用於多個物件 association是用於單個物件 5 如果裡面有collection,又有association,應該把association放前面,不然會報錯 6 無論是associ...

mybatis級聯 關聯 查詢

級聯 關聯 查詢,mybatis已經有了很好的支援,配置也相當簡單,示例 一種是一對一的,一種是一結多的,association用於前者,collection用於後者。下面都有相應配置。當然一對一的,可以直接配置在一起,就不用兩次查詢了。select from school where id sel...

Mybatis入門 關聯查詢 八

maybatis中在查詢進行select對映的時候,返回型別可以用resulttype,也可以用resultmap resulttype是直接表示返回型別的 對應著我們的model物件中的實體 resultmap則是對外部resultmap的引用 提前定義了db和model之間的對映key valu...