SpringBoot對數據庫操作有多種方式,下面這篇文章主要給大家介紹了關於springboot整合mybatis實現多表查詢的相關資料,文中通過示例代碼以及圖文介紹的非常詳細,需要的朋友可以參考下
什麼是mybatis
(1)mybatis 是一個半 orm(對象關係映射)框架,它內部封裝了 jdbc,開發時只需要關注 sql 語句本身,不需要花費精力去處理加載驅動、創建連接、創建statement 等繁雜的過程。程序員直接編寫原生態 sql,可以嚴格控制 sql 執行性能,靈活度高。
(2)mybatis 可以使用 xml 或註解來配置和映射原生信息,將 pojo 映射成數據庫中的記錄,避免了幾乎所有的 jdbc 代碼和手動設置參數以及獲取結果集。 @insert @repository
(3)通過 xml 文件或註解的方式將要執行的各種 statement 配置起來,並通過java 對象和 statement 中 sql 的動態參數進行映射生成最終執行的 sql 語句,最後由 mybatis 框架執行 sql 並將結果映射爲 java 對象並返回。(從執行 sql 到返回 result 的過程)。
mybaits 的優點:
(1)基 於 sql 語句編程,相當靈活,不會對應用程序或者數據庫的現有設計造成任何影響,sql 寫在 xml 裏,解除 sql 與程序代碼的耦合,便於統一管理;提供 xml標籤,支持編寫動態 sql 語句,並可重用。
(2)與 jdbc 相比,減少了 50%以上的代碼量,消除了 jdbc 大量冗餘的代碼,不需要手動開關連接;
(3)很好的與各種數據庫兼容(因爲 mybatis 使用 jdbc 來連接數據庫,所以只要jdbc 支持的數據庫 mybatis 都支持)。
(4)能夠與 spring 很好的集成;
mybatis是如何進行分頁的?分頁插件的原理是什麼?
mybatis使用rowbounds對象進行分頁,它是針對resultset結果集執行的內存分頁,而非物理分頁。可以在sql內直接書寫帶有物理分頁的參數來完成物理分頁功能,也可以使用分頁插件來完成物理分頁。
下面將詳細springboot整合mybatis多表查詢的方法,一起來看看吧
1、一對一查詢(例一個用戶一個賬戶)
1.1、實體類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
//用戶實體 @data public class userinfo { private int u_id; private string name; private account account; } //賬戶實體 @data public class account { private int a_id; private string aname; private double money; } |
1.2、數據庫表
用戶表
賬戶表
1.3、持久層接口
1
2
3
4
5
6
7
8
|
@select ( "select * from userinfo where name=#{name} " ) @results ({ //@result(property = "a_id",column = "a_id"), @result (property = "account" ,column = "a_id" ,javatype = account. class , one = @one (select= "com.bbz.dao.accountdao.findbyid" ,fetchtype = fetchtype.lazy)) }) public userinfo finduserlnfo(string name); |
1
2
|
@select ( "select * from account where a_id=#{a_id}" ) public account findbyid ( int a_id); |
2、一對多查詢(例一個用戶對應多個賬戶)
2.1、實體類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
//用戶實體 @data public class userinfo { private int u_id; private string name; private list<account>accountlist; } //賬戶實體 @data public class account { private int id; private int a_id; private string aname; private double money; } |
2.2、數據庫表
用戶表
賬戶表
2.3、持久層接口
1
2
3
4
5
6
7
8
9
|
@select ( "select * from userinfo where name=#{name}" ) @results ({ @result (property = "accountlist" ,column = "a_id" ,javatype = list. class , many = @many (select = "com.bbz.dao.accountdao.findbyid" ,fetchtype = fetchtype.lazy) ) }) public userinfo finduser(string name); //fetchtype = fetchtype.lazy):提取方式爲延遲加載,默認是立即加載 |
1
2
3
|
@select ( "select * from account where a_id=#{a_id}" ) public account findbyid ( int a_id); |
3、總結
共同點:
無論是一對一還是一對多,都是通過附屬查詢來實現的,我們需要定義這個附屬查詢方法。
在主查詢方法中通過@one、@many指定附屬查詢方法的全路徑。
都通過column來傳遞參數給附屬方法。
不同點:
一對一,那麼附屬方法返回的是一個單獨的對象
一對多,那麼附屬方法返回的是一個對象集合
4、多對多的查詢(例一個用戶多個角色)
4.1、實體類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
//用戶實體 @data public class userinfo { private int u_id; private string name; private list<role>rolelist; } //角色實體 @data public class role { private int r_id; private string name; } |
4.2、數據庫表
用戶表
角色表
中間表
4.3、持久層接口
1
2
3
4
5
6
7
8
9
10
|
@select ( "select * from userinfo where u_id=#{u_id}" ) @results ({ @result (property = "u_id" ,column = "u_id" ), @result (property = "rolelist" ,column = "u_id" ,javatype = list. class , many = @many (select = "com.bbz.dao.roledao.findbyid" ,fetchtype = fetchtype.lazy) ) }) public userinfo finduser( int u_id); |
1
2
|
@select ( "select * from role r,user_role ur where r.r_id=ur.r_id and ur.u_id=#{u_id}" ) public list<role> findbyid( int u_id); |
5、多對一(一個用戶對應多個老師)
5.1 實體類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//用戶實體 @data public class userinfo { private int u_id; private string name; private teacher teacher; } //老師實體 public class teacher { public int t_id; public string name; } |
5.2、數據庫表
用戶表
老師表
5.3、持久層接口
1
2
3
4
5
6
|
@select ( "select * from userinfo where u_id=#{u_id}" ) @results ({ @result (property = "teacher" ,column = "t_id" ,javatype = teacher. class , one= @one (select = "com.bbz.dao.teacherdao.findbyid" ,fetchtype = fetchtype.lazy) ) }) |
1
2
|
@select ( "select * from teacher where t_id=#{t_id}" ) public teacher findbyid( int t_id); |
總結
到此這篇關於springboot整和mybatis實現多表查詢的文章就介紹到這了,更多相關springboot整和mybatis多表查詢內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支持服務器之家!