前期準備資料庫關係
可以看到employee是參照department表的,是多對一的關係
實體類com.maple.pojo.department
com.maple.pojo.employee@data
public class department
com.maple.entity.department@data
public class employee
com.maple.entity.employee@data
public class department extends com.maple.pojo.department
}
核心配置檔案@data
public class employee extends com.maple.pojo.employee
}
listqueryemplist();<?xml version="1.0" encoding="utf-8" ?>
employee queryempbyid(integer empid);
}測試類
@test
public void test2()
@test
public void test3()
使用resulttype返回型別查詢
執行結果select * from mybatis.employee,mybatis.department
where employee.deptid=department.deptid
# select * from mybatis.employee
可以看到department並沒有對映到結果
解決方法按照結果巢狀處理(使用resultmap返回型別,通過子查詢獲取department)
執行結果<?xml version="1.0" encoding="utf-8" ?>
select *
from mybatis.employee
select *
from mybatis.department
where deptid = #
按照結果巢狀處理(使用連表查詢)
執行結果select empid, last_name, email, gender, employee.deptid, birth
, department_name
from mybatis.department ,mybatis.employee
where employee.deptid=department.deptid and empid=#
介面
listquerydeplist();
listquerydepbyid(@param("deptid") int deptid);
}
測試類<?xml version="1.0" encoding="utf-8" ?>
select d.department_name,e.* from mybatis.department d,mybatis.employee e where e.deptid=d.deptid
select d.department_name,e.* from mybatis.department d,mybatis.employee e where e.deptid=d.deptid and d.deptid=#
執行結果@test
public void test3()
@test
public void test4()

MyBatis 中一對一和一對多的對映關係
1 一對一對映 比如每位學生有乙個位址。public class address public class student 我們根據學生 id 選擇學生資訊 方法一 使用句點符號表示巢狀物件的引用,student 的 address 屬性使用了圓點記法被賦上了 address 對應列的值。view ...
mybatis 一對多查詢
查詢訂單及訂單明細的資訊。這裡怎麼體現了一對多 這裡orders的id出現重複的記錄,無法對映到orders類中 collection 對關聯查詢到多條記錄對映到集合物件中 4 查詢訂單 關聯使用者 及訂單明細 public listfindordersandorderdetailresultmap...
mybatis 一對多查詢
與phoenix不同,在mysql中查詢的結果不會按照id預設排序。所以如果頁面有隱含的順序要求 兩次呼叫,列表順序不變 此時千萬不要使用set,而應該使用list。接下來進入正題 直接上 public class userpublic class order 根據id查詢使用者,並且查詢出該使用者...