Mybatis多表查询

一、简介

MyBatis 是一款优秀的 ORM 框架,支持多表查询操作。在实际开发中,经常需要使用多表查询来获取业务数据。

表与表之间存在的三种关系:一对一,一对多,多对多

 二、一对一
1.pojo

        student(学生类)

import java.util.List;

public class Student {
    private int id;
    private String studentName;
    private String gender;
    private String address;
    private String email;
    private String remark;

    public Student() {
    }

    public Student(String studentName, String gender, String address,String remark) {
        this.studentName = studentName;
        this.gender = gender;
        this.address = address;
        this.remark = remark;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }
}

        order(订单类)

import java.util.List;

public class Order {
    private int id;
    private String ordername;
    private int ordercount;
    private Student student;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getOrdername() {
        return ordername;
    }

    public void setOrdername(String ordername) {
        this.ordername = ordername;
    }

    public int getOrdercount() {
        return ordercount;
    }

    public void setOrdercount(int ordercount) {
        this.ordercount = ordercount;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }
}
2.queryOrder接口类
public interface QueryOrderMapper {
    public List findOrderStudentAll();
}
3.OrderMapper.xml



    
    
        
        
        
            
            
            
        
    
    
4.test测试
 @Test
    public void testMapper6(){
        SqlSession sqlSession = ssf.openSession();
        QueryOrderMapper mapper = sqlSession.getMapper(QueryOrderMapper.class);
        List orders = mapper.findOrderStudentAll();
        QueryOrderStudent student = new QueryOrderStudent();
        for (Order order : orders) {
            System.out.println(
                    order.getOrdername()+","+order.getOrdercount()+","+
                    student.getId()+","+student.getStudentName()
                    +","+student.getAddress()
            );
        }
    }
三、一对多
1.pojo

        student.java

package com.batis.domain;

        import java.util.List;

public class Student {
    private int id;
    private String studentName;
    private String gender;
    private String address;
    private String email;
    private String remark;
    private List listOrder;

    public Student() {
    }

    public Student(String studentName, String gender, String address,String remark) {
        this.studentName = studentName;
        this.gender = gender;
        this.address = address;
        this.remark = remark;
    }

    public List getListOrder() {
        return listOrder;
    }

    public void setListOrder(List listOrder) {
        this.listOrder = listOrder;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }
}

        order.java

import java.util.List;

public class Order {
    private int id;
    private String ordername;
    private int ordercount;
    private Student student;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getOrdername() {
        return ordername;
    }

    public void setOrdername(String ordername) {
        this.ordername = ordername;
    }

    public int getOrdercount() {
        return ordercount;
    }

    public void setOrdercount(int ordercount) {
        this.ordercount = ordercount;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }
}
2.StudentMapper接口类
public interface StudentMapper {
    public List findStudentOrderAll();
}
3.StudentMapper.xml



    
        
        
        
        
            
            
        
    

    



4.test测试
@Test
    public void testMapper8(){
        SqlSession sqlSession = ssf.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List students = mapper.findStudentOrderAll();
        for (Student student : students) {
            System.out.println(student.getId()+","+student.getStudentName()+","+student.getAddress());
            List listOrder = student.getListOrder();
            for (Order order : listOrder) {
                System.out.println(order.getOrdername()+","+order.getOrdercount());
            }

        }
    }
四、多对多
1.pojo

        items(物品类)

public class Items {
    private int id;
    private String items_name;
    private int items_price;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getItems_name() {
        return items_name;
    }

    public void setItems_name(String items_name) {
        this.items_name = items_name;
    }

    public int getItems_price() {
        return items_price;
    }

    public void setItems_price(int items_price) {
        this.items_price = items_price;
    }
}

        order

import java.util.List;

public class Order {
    private int id;
    private String ordername;
    private int ordercount;
    private Student student;
    private List orderDetailList;

    public List getOrderDetailList() {
        return orderDetailList;
    }

    public void setOrderDetailList(List orderDetailList) {
        this.orderDetailList = orderDetailList;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getOrdername() {
        return ordername;
    }

    public void setOrdername(String ordername) {
        this.ordername = ordername;
    }

    public int getOrdercount() {
        return ordercount;
    }

    public void setOrdercount(int ordercount) {
        this.ordercount = ordercount;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }
}

        orderdetail(订单详细)

import java.util.List;

public class OrderDetail {
    private int id;
    private int item_count;
    private int item_all_price;
    private Order orderL;
    private List itemsList;

    public Order getOrderL() {
        return orderL;
    }

    public void setOrderL(Order orderL) {
        this.orderL = orderL;
    }

    public List getItemsList() {
        return itemsList;
    }

    public void setItemsList(List itemsList) {
        this.itemsList = itemsList;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getItem_count() {
        return item_count;
    }

    public void setItem_count(int item_count) {
        this.item_count = item_count;
    }

    public int getItem_all_price() {
        return item_all_price;
    }

    public void setItem_all_price(int item_all_price) {
        this.item_all_price = item_all_price;
    }
}
2.queryOrder接口类
import com.batis.domain.Order;

import java.util.List;

public interface QueryOrderMapper {
    public List findOrderStudentItemsOrderDetail();
}
3.OrderMapper.xml




    
        
        
        
            
            
            
        
        
            
            
            
        
        
            
            
        
    
    

4.test测试
@Test
    public void testMapper9(){
        SqlSession sqlSession = ssf.openSession();
        QueryOrderMapper mapper = sqlSession.getMapper(QueryOrderMapper.class);
        List orders = mapper.findOrderStudentItemsOrderDetail();
        for (Order order : orders) {
            System.out.println(order.getId()+","+order.getOrdername()+","+order.getStudent().getStudentName()+","+order.getStudent().getGender());
            List orderDetailList = order.getOrderDetailList();
            for (OrderDetail orderDetail : orderDetailList) {
                System.out.println(orderDetail.getId()+","+orderDetail.getItem_count()+","+orderDetail.getItem_all_price());
                List itemsList = orderDetail.getItemsList();
                for (Items items : itemsList) {
                    System.out.println(items.getItems_name()+","+items.getItems_price());

                }
            }

        }
    }

你可能感兴趣的:(数据库,java,mybatis,idea)