Many-to-many relational query

In fact, MyBatis does not implement many-to-many cascades, because many-to-many cascades can be replaced by two one-to-many cascades.

For example, an order can have multiple products, and one product can correspond to multiple orders. Orders and products are in a many-to-many cascade relationship. Using an intermediate table (order record table) can convert the many-to-many cascade into Two to many relationship.

The following takes orders and products (implementing the function of "query all orders and the product information corresponding to each order") as an example to explain the many-to-many cascade query.

1) Create a data table

The order table has been created before, here you need to create the product table product and the order record table orders_detail, the creation code is as follows:

CREATE TABLE 'product'(
    'id' tinyint(2) NOT NULL,
    'name' varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
    'price' double DEFAULT NULL,
    PRIMARY KEY ('id')
);
CREATE TABLE 'orders_detail'(
    'id' tinyint(2) NOT NULL AUTO_INCREMENT,
    'orders_id' tinyint(2) DEFAULT NULL,
    'product_id' tinyint(2) DEFAULT NULL,
    PRIMARY KEY ('id'),
    KEY 'orders_id' ('orders_id'),
    KEY 'product_id' ('product_id'),
    CONSTRAINT 'orders_id' FOREIGN KEY ('orders_id') REFERENCES 'orders' ('id'),
    CONSTRAINT 'product_id' FOREIGN KEY ('product_id') REFERENCES 'product' ('id')
);

2) Create a persistent class

Create the persistent class Product corresponding to the data table product in the com.po package of the myBatisDemo02 application, and the intermediate table orders_detail does not need a persistent class, but needs to add associated attributes to the persistent class Orders corresponding to the orders table orders.

The code for Product is as follows:

package com.po; 

import java.util.List; 

public class Product { 
    private Integer id; 
    private String name; 
    private Double price; 
    // one-to-many in many-to-many 
    private List<Orders> orders; 

    // setter omitted And getter method 
    @Override 
    public String toString() { // In order to view the results conveniently, the toString method is rewritten 
        return "Product[id=" + id + ",name=" + name + ",price=" + price + " ]"; 
    } 
}

The code for Orders is as follows:

package com.po; 
import java.util.List; 
public class Orders { 
    private Integer id; 
    private String ordersn; 
    // Another one-to-many in many-to-many 
    private List<Product> products; 

    // Setter and getter methods omitted 
    @Override 
    public String toString() { 
        return "Orders[id=" + id + ",ordersn=" + ordersn + ",products=" 
                + products + "]"; 
    } 
}

3) Create a mapping file

This example only needs to add the following configuration to the OrdersMapper.xml file of com.mybatis to realize the many-to-many cascading query.

4) Create POJO class

This instance does not need to create a POJO class.

5) Add data operation interface method

Add the following interface method to the Orders interface:

public List<Orders> selectallOrdersAndProducts();

6) Call interface method and test

Create a MoreToMoreController class in the com.controller package of the myBatisDemo02 application, call the interface method in step 5 in this class, and create a test class TestMoreToMore at the same time.

The code for MoreToMoreController is as follows:

package com.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.dao.OrdersDao;
import com.po.Orders;

@Controller("moreToMoreController")
public class MoreToMoreController {
    @Autowired
    private OrdersDao ordersDao;

    public void test() {
        List<Orders> os = ordersDao.selectallOrdersAndProducts();
        for (Orders orders : os) {
            System.out.println(orders);
        }
    }
}

The code for TestMoreToMore is as follows:

package com.controller;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestMoreToMore {
    public static void main(String[] args) {
        ApplicationContext appcon = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        MoreToMoreController otm = (MoreToMoreController) appcon
                .getBean("moreToMoreController");
        otm.test();
    }
}

The running results of the above test classes are shown in Figure 1.
 

 

Many-to-many cascading query results


Figure 1 Many-to-many cascade query results

Guess you like

Origin blog.csdn.net/unbelievevc/article/details/132401044