Spring Boot开发之Mybatis多对多查询


今天接着上次的项目来讲解Mybatis一对多、多对多查询

一、一对多查询

1、打开MySQL Workbench,在上篇数据库下新建orders表,并插入一些数据;其中userid对应class表的id

在这里插入图片描述
在这里插入图片描述

2、打开IDEA,打开上次项目,在pojo文件夹下新建Order实体类

在这里插入图片描述

package com.example.springboot2.pojo;

import lombok.Data;

import java.util.List;

@Data
public class Order {
    
    
    private int id;
    private int totalprice;
    private String orderinfo;
    private int userid;
}

3、修改Person实体类

在这里插入图片描述

private List<Order> orderList;

4、在dao文件夹下新建OrderMapper类

在这里插入图片描述

package com.example.springboot.dao;

import com.example.springboot.pojo.Order;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface OrderMapper {
    
    
    public List<Order> findOrderByUserId(Integer userid);
}

5、在resources文件夹的mappers文件夹下新建OrderMapper.xml

在这里插入图片描述

<?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="com.example.springboot2.dao.OrderMapper">
<select id="findOrderByUserId"
        resultType="com.example.springboot2.pojo.Order"
        parameterType="Integer">
select * from orders where userid=#{userid}
</select>
</mapper>

6、修改PersonMapper类

在这里插入图片描述

  public List<Person> findAllPersonOrders();

7、修改PersonMapper.xml

在这里插入图片描述

    <select id="findAllPersonOrders" resultMap="personOrder">
        select * from class
    </select>
    <resultMap id="personOrder" type="com.example.springboot2.pojo.Person">
        <id column="id" property="id"></id>
        <result column="age" property="age"></result>
        <result column="name" property="name"></result>
        <!--        select查询方法 column将class表查询出来的idcardid字段传到findIdcardById方法中-->
        <association property="idcardid" javaType="com.example.springboot2.pojo.Idcard" column="idcardid"
                     select="com.example.springboot2.dao.IdcardMapper.findIdcardById">
        </association>
        <!--            private List<Order> orderlist;-->
        <!--        collection映射到集合对象  column class表查询出来的将返回的id字段传到findOrderByUserId方法中-->
        <collection property="orderList"
                    ofType="com.example.springboot2.pojo.Order"
                    column="id"
                    select="com.example.springboot2.dao.OrderMapper.findOrderByUserId">
        </collection>
    </resultMap>

8、修改service层的PersonServiceImpl实现类

在这里插入图片描述

    @Override
    public List<Person> findAllPerson(){
    
    
        return personMapper.findAllPersonOrders();
//        return personMapper.findAllPersonIdcard2();
    }

9、修改personlist.html

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>用户列表</h1>
<a th:href="@{/addperson}">增加页面</a>
<table border="1">
    <tr>
        <td>学生id</td>
        <td>学生年龄</td>
        <td>学生姓名</td>
        <td>学生学号</td>
        <td>学生班级</td>
        <td>订单信息</td>
        <td>删除</td>
        <td>更新</td>
    </tr>
    <tr th:each="person:${personlist}">
        <td th:text="${person.id}">学生id</td>
        <td th:text="${person.age}">学生年龄</td>
        <td th:text="${person.name}">学生姓名</td>
        <td th:text="${person.idcardid.stuid}">学生学号</td>
        <td th:text="${person.idcardid.classname}">学生班级</td>
        <td>
            <ul  th:each="order:${person.orderList}">
                <li th:text="${order.totalprice}"></li>
                <li th:text="${order.orderinfo}"></li>
            </ul>
        </td>

        <th>
            <a th:href="@{deletepersonbyid(id=${person.id},idcardid=${person.idcardid.id})}">删除学生</a>
            <!--            <a th:href="@{'deletepersonbyid?id='+${person.id}+'&idcardid='+${person.idcardid.id}}">删除学生</a>-->
        </th>
        <th>
            <a th:href="@{/updateperson(id=${person.id},age=${person.age},name=${person.name})}">更新学生</a>
        </th>

    </tr>
</table>
</body>
</html>

10、加上非空判断(这里可写可不写)

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>用户列表</h1>
<a th:href="@{/addperson}">增加页面</a>
<table border="1">
    <tr>
        <td>学生id</td>
        <td>学生年龄</td>
        <td>学生姓名</td>
        <td>学生学号</td>
        <td>学生班级</td>
        <td>订单信息</td>
        <td>删除</td>
        <td>更新</td>
    </tr>
    <tr th:each="person:${personlist}">
        <td th:text="${person.id}">学生id</td>
        <td th:text="${person.age}">学生年龄</td>
        <td th:text="${person.name}">学生姓名</td>
        <td th:if="${person.idcardid.stuid} ne null" th:text="${person.idcardid.stuid}">学生学号</td>
        <td th:if="${person.idcardid.classname} ne null" th:text="${person.idcardid.classname}">学生班级</td>
        <td>
            <ul th:if="${person.orderList} ne null" th:each="order:${person.orderList}">
                <li th:text="${order.totalprice}"></li>
                <li th:text="${order.orderinfo}"></li>
            </ul>
        </td>

        <th>
            <a th:href="@{deletepersonbyid(id=${person.id},idcardid=${person.idcardid.id})}">删除学生</a>
            <!--            <a th:href="@{'deletepersonbyid?id='+${person.id}+'&idcardid='+${person.idcardid.id}}">删除学生</a>-->
        </th>
        <th>
            <a th:href="@{/updateperson(id=${person.id},age=${person.age},name=${person.name})}">更新学生</a>
        </th>

    </tr>
</table>
</body>
</html>

11、点击运行,1对多查询成功!

在这里插入图片描述

二、多对多查询

1、在数据库中新建product表,并插入一些数据

在这里插入图片描述

2、在数据库中新建orders_product表,其中orderid为orders表中id,productid为product表中id

在这里插入图片描述

3、在项目pojo文件夹下新建Product实体类

在这里插入图片描述

package com.example.springboot2.pojo;

import lombok.Data;

@Data
public class Product {
    
    
    private int id;
    private String name;
    private int price;
}

4、修改Order实体类

在这里插入图片描述

    private List<Product> productList;

5、在dao文件夹下新建ProductMapper类

在这里插入图片描述

package com.example.springboot2.dao;


import com.example.springboot2.pojo.Product;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface ProductMapper {
    
    
    public List<Product> findProductById(Integer orderid);
}

6、在resources文件夹的mappers文件夹下新建ProductMapper.xml

在这里插入图片描述

<?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="com.example.springboot2.dao.ProductMapper">
<!--      第二步:将订单主键传到中间order_product表中查询商品主键-->
<!--      第三步:将商品主键传到商品表中查询商品信息-->
  <select id="findProductById" resultType="com.example.springboot2.pojo.Product" parameterType="Integer">
    select *
    from product
    where id in (select productid
                 from orders_product
                 where orderid = #{orderid})
  </select>

</mapper>

7、修改OrderMapper.xml

在这里插入图片描述

<?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="com.example.springboot2.dao.OrderMapper">
<select id="findOrderByUserId"
        resultMap="orderProduct"
        parameterType="Integer">
select * from orders where userid=#{userid}
</select>
    <resultMap id="orderProduct"
               type="com.example.springboot2.pojo.Order">
        <id column="id" property="id"></id>
        <result column="totalprice" property="totalprice"></result>
        <result column="orderinfo" property="orderinfo"></result>

        <collection property="productList"
                    ofType="com.example.springboot2.pojo.Product"
                    column="id"
                    select="com.example.springboot2.dao.ProductMapper.findProductById">

        </collection>
    </resultMap>
</mapper>

8、修改personlist.html

在这里插入图片描述

                <div th:each="product:${order.productList}">
                    <div th:text="${product.name}"></div>
                </div>

9、点击运行,多对多查询成功!

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_61963074/article/details/127579319