1. 程式人生 > 程式設計 >Mybatis一對多關聯關係對映實現過程解析

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);
}

使用者業務層實現類

@Service
public class UserWithOrdersInfoImpl implements UserWithOrdersInfo {
 @Autowired
 private UserMapper userMapper;

 @Override
 public List<User> selectUserOrdersInfo(int id) {
  return userMapper.userOrdersinfo(id);
 }
}

控制器類

@Controller
public 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>

執行結果

Mybatis一對多關聯關係對映實現過程解析

一對多關聯關係總結:

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。