最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MyBatis一對多關系使用@Many注解的實現(xiàn)

 更新時間:2025年11月28日 09:57:47   作者:Sugarian  
本文介紹了在MyBatis中實現(xiàn)一對多查詢的方法,包括數(shù)據(jù)表和數(shù)據(jù)類的設計,以及使用@Many注解進行查詢,具有一定的參考價值,感興趣的可以了解一下

一對多關系

在討論使用 MyBatis 實現(xiàn)一對多查找之前,有必要在數(shù)據(jù)表和數(shù)據(jù)類設計層次上明確一對多關系。
假設一個用戶有多個賬戶。在最簡單的情況下,用戶表僅包含 user_id 和 user_name 列,而賬戶表則包含 account_id、account_name 和 user_id 外鍵,其中 *_id 列為各個表的主鍵。
現(xiàn)在來看數(shù)據(jù)類的設計。一個用戶擁有多個賬號,自然想到 User 類中包含一個 Account 列表。但是,Account 類中能包含 User 對象嗎?
如果 Account 中包含 User 對象,這意味著用戶的信息有泄露的可能(如果用戶表還包含其他隱私信息)。比如,當我們只需要知道用戶的某個賬號時,卻附贈了用戶的一系列其他信息。另外這種互相包含的關系也容易導致寫出無限遞歸的查詢語句。Account 要為 User 賦值,而 User 中包含 Account 列表,Account 列表中的元素要為 User 賦值……
這里給出感覺合理的數(shù)據(jù)表和數(shù)據(jù)類模型。

# users table
CREATE TABLE `users` (
  `user_id` int NOT NULL,
  `user_name` varchar(45) NOT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
// user 類,忽略 setter 和 getter 
public class User {
    private Integer userId;
    private String userName;
    private List<Account> accounts;
}
# accounts table
CREATE TABLE `accounts` (
  `account_id` int NOT NULL,
  `account_name` varchar(45) NOT NULL,
  `user_id` int NOT NULL,
  PRIMARY KEY (`account_id`),
  KEY `user_id_idx` (`user_id`),
  CONSTRAINT `user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
// account 類,忽略 setter 和 getter 
public class Account {
    private Integer accountId;
    private String accountName;
    private Integer userId;
}

MyBatis 一對多查詢

查詢的主要難度在查詢 User 上,其關鍵是理解 @Many 注解相關的參數(shù)。
先把簡單的 AccountDao 搞了。

@Mapper
public interface AccountDao {
    @Results(id = "accountResultMap", value = {
            @Result(property = "accountId", column = "account_id", id = true),
            @Result(property = "accountName", column = "account_name"),
            @Result(property = "userId", column = "user_id")
    })
    @Select(value = "SELECT * FROM accounts WHERE account_id = #{id}")
    public Account findAccountById(@Param("id") Integer id);

    @ResultMap(value = "accountResultMap")
    @Select(value = "SELECT * FROM accounts WHERE user_id=#{id}")
    public List<Account> findAccountByUserId(@Param("id") Integer id);
}

注意:這里我們定義了一個 findAccountByUserId 查詢函數(shù)。因為 User 類在填充 Account 列表時,是用 user_id 進行查詢的。
再看 UserDao。

@Mapper
public interface UserDao {
    @Results(id = "userResultMap", value = {
            @Result(property = "userId", column = "user_id", id = true),
            @Result(property = "userName", column = "user_name"),
            @Result(property = "accounts", column = "user_id", javaType = List.class,
                    many = @Many(select = "com.sugarian.dao.AccountDao.findAccountByUserId"))
    })
    @Select(value = "SELECT * FROM users WHERE user_id=#{id}")
    public User findUserById(@Param("id") Integer id);
}

注意下面抽取的部分

@Result(property = "userName", column = "user_name"),
            @Result(property = "accounts", column = "user_id", javaType = List.class,
                    many = @Many(select = "com.sugarian.dao.AccountDao.findAccountByUserId"))
    })

User 中的 accounts 是存放 Account 的列表,其在表結構中是沒有對應的列,所以它對應的這條 @Result 實際上是再次執(zhí)行了一次 sql 索引,然后將值填充到 accounts 中。另外一點,請看 column 是 user_id,這實際上就是以 user_id 作為參數(shù),findAccountByUserId 用進行索引。
我們可以看看調試工具給出的信息:

2022-11-25 23:39:09,552 [DEBUG] com.sugarian.dao.UserDao.findUserById - ==>  Preparing: SELECT * FROM users WHERE user_id=?
2022-11-25 23:39:09,552 [DEBUG] com.sugarian.dao.UserDao.findUserById - ==> Parameters: 1(Integer)
2022-11-25 23:39:09,553 [DEBUG] com.sugarian.dao.AccountDao.findAccountByUserId - ====>  Preparing: SELECT * FROM accounts WHERE user_id=?
2022-11-25 23:39:09,553 [DEBUG] com.sugarian.dao.AccountDao.findAccountByUserId - ====> Parameters: 1(Integer)
2022-11-25 23:39:09,556 [DEBUG] com.sugarian.dao.AccountDao.findAccountByUserId - <====      Total: 3
2022-11-25 23:39:09,556 [DEBUG] com.sugarian.dao.UserDao.findUserById - <==      Total: 1

明顯執(zhí)行了兩次 sql 查詢。對于第二個查詢,它從第一個查詢結果中取出其中的 user_id 列,作為查詢的參數(shù)。
其他參數(shù)如 javaType 指定返回類型。@Many 中的參數(shù) select 指定第二次查詢用的函數(shù)。

到此這篇關于MyBatis一對多關系使用@Many注解的實現(xiàn)的文章就介紹到這了,更多相關MyBatis一對多@Many 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

岱山县| 肇庆市| 楚雄市| 庄浪县| 广南县| 舞阳县| 肇州县| 古丈县| 西和县| 安丘市| 临高县| 稻城县| 图木舒克市| 上栗县| 来宾市| 沈丘县| 奈曼旗| 英德市| 沙洋县| 青阳县| 昌宁县| 百色市| 东平县| 博野县| 光泽县| 台北市| 汶川县| 武邑县| 遂平县| 尉犁县| 江口县| 克东县| 无为县| 宁陕县| 泸溪县| 射洪县| 上犹县| 红河县| 宜兰县| 乃东县| 九江县|