Mybatis一對(duì)多關(guān)聯(lián)關(guān)系映射實(shí)現(xiàn)過程解析
這篇文章主要介紹了Mybatis一對(duì)多關(guān)聯(lián)關(guān)系映射實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
一對(duì)多關(guān)聯(lián)關(guān)系只需要在多的一方引入少的一方的主鍵作為外鍵即可。在實(shí)體類中就是反過來,在少的一方添加多的一方,聲明一個(gè)List<另一方> 屬性名 作為少的一方的屬性。
用戶和訂單就是一對(duì)多的關(guān)系,從用戶角度看就是一對(duì)多關(guān)系,從訂單的角度來看就是多對(duì)一的關(guān)系。
/** * 訂單持久化類 */ 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;//用戶關(guān)聯(lián)的訂單 setter/getter方法 }
用戶mapper接口
/** * 用戶Mapper接口 */ public interface UserMapper { //用戶和訂單為一對(duì)多關(guān)系,那么此時(shí)應(yīng)該返回一個(gè)用戶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"/> <!--一對(duì)多關(guān)系映射 ofType表示屬性集合中的元素的類型,List<Orders>屬性即Orders類 --> <collection property="ordersList" ofType="Orders"> <id property="id" column="orders_id"/> <result property="number" column="number"/> </collection> </resultMap> <!--關(guān)聯(lián)查詢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>
用戶業(yè)務(wù)層接口
/** * 用戶業(yè)務(wù)層接口 */ public interface UserWithOrdersInfo { List<User> selectUserOrdersInfo(int id); }
用戶業(yè)務(wù)層實(shí)現(xiàn)類
@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>訂單號(hào)</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}">訂單號(hào)</td> <td th:text="${userordersinfo.ordersList.get(1).id}">訂單id</td> <td th:text="${userordersinfo.ordersList.get(1).number}">訂單號(hào)</td> </tr> </table> </body> </html>
運(yùn)行結(jié)果
一對(duì)多關(guān)聯(lián)關(guān)系總結(jié):
一對(duì)多關(guān)系從不同的角度看,關(guān)系是不一樣的,但是最終都是一樣的,在編寫mapper映射文件的時(shí)候使用的是<collection>元素。也是使用嵌套查詢更加方便,需要解決的問題是如何在頁面展示查詢出來的一對(duì)多關(guān)聯(lián)關(guān)系的結(jié)果。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot實(shí)現(xiàn)用戶名查找用戶功能
本文主要介紹了springboot實(shí)現(xiàn)用戶名查找用戶功能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04