溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Mybatis一對多關聯關系映射實現過程介紹

發布時間:2021-08-31 10:50:52 來源:億速云 閱讀:172 作者:chen 欄目:編程語言

本篇內容主要講解“Mybatis一對多關聯關系映射實現過程介紹”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Mybatis一對多關聯關系映射實現過程介紹”吧!

一對多關聯關系只需要在多的一方引入少的一方的主鍵作為外鍵即可。在實體類中就是反過來,在少的一方添加多的一方,聲明一個List<另一方> 屬性名 作為少的一方的屬性。

用戶和訂單就是一對多的關系,從用戶角度看就是一對多關系,從訂單的角度來看就是多對一的關系。

/** * 訂單持久化類 */public class Orders { private Integer id; private String number; setter/getter方法}

/***用戶持久化類*/public class User { private Integer id; private String username; private String address; private List<Orders> ordersList;//用戶關聯的訂單 setter/getter方法}

用戶mapper接口

/** * 用戶Mapper接口 */public interface UserMapper { //用戶和訂單為一對多關系,那么此時應該返回一個用戶list里面包含了訂單信息 List<User> userOrdersinfo(int id);//通過用戶id返回它的訂單信息}

用戶的mapper映射文件

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" ><mapper namespace="cn.jason.bootmybatis.mapper.UserMapper"> <resultMap id="UserWithOrdersInfo" type="User">  <id property="id" column="id"/>  <result property="username" column="username"/>  <result property="address" column="address"/>  <!--一對多關系映射   ofType表示屬性集合中的元素的類型,List<Orders>屬性即Orders類  -->  <collection property="ordersList" ofType="Orders">   <id property="id" column="orders_id"/>   <result property="number" column="number"/>  </collection> </resultMap> <!--關聯查詢sql--> <select id="userOrdersinfo" parameterType="Integer" resultMap="UserWithOrdersInfo">  select u.*,o.id as orders_id,o.number  from tb_user u,tb_orders o  where u.id=o.user_id and u.id=#{id} </select></mapper>

用戶業務層接口

/** * 用戶業務層接口 */public interface UserWithOrdersInfo { List<User> selectUserOrdersInfo(int id);}

用戶業務層實現類

@Servicepublic class UserWithOrdersInfoImpl implements UserWithOrdersInfo { @Autowired private UserMapper userMapper; @Override public List<User> selectUserOrdersInfo(int id) {  return userMapper.userOrdersinfo(id); }}

控制器類

@Controllerpublic class UserOrdersController { @Autowired private UserWithOrdersInfo userWithOrdersInfo; @RequestMapping("/userordersinfo/{id}") public String getUserOrdersInfo(@PathVariable int id, Model model){  model.addAttribute("userordersinfo",userWithOrdersInfo.selectUserOrdersInfo(id));  return "userordersinfo"; }}

頁面

<!DOCTYPE html><html lang="en" xmlns:th="http://www.thymeleaf.org"  xmlns="http://www.w3.org/1999/xhtml"><head> <meta charset="UTF-8"> <title>person信息頁面</title></head><body><table> <thead> <tr>  <th>用戶id</th>  <th>姓名</th>  <th>用戶地址</th>  <th>訂單id</th>  <th>訂單號</th> </tr> </thead> <tr th:each="userordersinfo:${userordersinfo}">  <td th:text="${userordersinfo.id}">用戶id</td>  <td th:text="${userordersinfo.username}">用戶姓名</td>  <td th:text="${userordersinfo.address}">用戶地址</td>  <td th:text="${userordersinfo.ordersList.get(0).id}">訂單id</td>  <td th:text="${userordersinfo.ordersList.get(0).number}">訂單號</td>  <td th:text="${userordersinfo.ordersList.get(1).id}">訂單id</td>  <td th:text="${userordersinfo.ordersList.get(1).number}">訂單號</td> </tr></table></body></html>

運行結果

一對多關聯關系總結:

一對多關系從不同的角度看,關系是不一樣的,但是最終都是一樣的,在編寫mapper映射文件的時候使用的是<collection>元素。也是使用嵌套查詢更加方便,需要解決的問題是如何在頁面展示查詢出來的一對多關聯關系的結果。

到此,相信大家對“Mybatis一對多關聯關系映射實現過程介紹”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女