本篇內容主要講解“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一對多關聯關系映射實現過程介紹”有了更深的了解,不妨來實際操作一番吧!這里是創新互聯網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
分享文章:Mybatis一對多關聯關系映射實現過程介紹
網頁地址:http://m.kartarina.com/article38/jeddsp.html
成都網站建設公司_創新互聯,為您提供定制開發、品牌網站制作、網站營銷、微信公眾號、建站公司、動態網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯