MyBatis多表操作查询功能

一对一查询

用户表和订单表的关系为,一个用户多个订单,一个订单只从属一个用户
一对一查询的需求:查询一个订单,与此同时查询出该订单所属的用户

MyBatis多表操作查询功能_第1张图片

在只查询order表的时候,也要查询user表,所以需要将所有数据全部查出进行封装SELECT *,o.id oid FROM orders o,USER u WHERE o.uid=u.id

MyBatis多表操作查询功能_第2张图片

创建Order和User实体

order

public class Order {
    private int id;
    private Date ordertime;
    private double total;
    //表示当前订单属于哪一个用户
    private User user;

user

public class User {
    private int id;
    private String username;
    private String password;
    private Date birthday;

创建OrderMapper接口

public interface UserMapper {
//查询全部的方法
    public List findAll();
}

配置OrderMapper.xml






    
        
        
        
        
        
        
        
        
        

    

    
    




sqlMapConfig.xml





    
    
    
    
        
        
    

    
    
        
            
            
                
                
                
                
            
        
    
    
    
        
        
    



MyBatis多表操作查询功能_第3张图片

在一对一配置的时候,在order实体中创建了一个user,所以property属性都使用user.** 的方式进行编写,但是这里还可以使用association






    
        
        
        
        
        
        
        
        
        
        
            
            
            
            
        
        

    

    
    




一对多查询的模型

用户表和订单表的关系为,一个用户有多个订单,一个当但只从属一个用户
一对多查询需求:查询一个用户,与此同时查询出该用户具有的订单

MyBatis多表操作查询功能_第4张图片

package com.zg.domain;

import java.util.Date;
import java.util.List;

public class User {
    private int id;
    private String username;
    private String password;
    private Date birthday;

    //描述当前用户存在哪些订单
    private List  orderList;

    public List getOrderList() {
        return orderList;
    }

    public void setOrderList(List orderList) {
        this.orderList = orderList;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", birthday=" + birthday +
                ", orderList=" + orderList +
                '}';
    }

    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}

MyBatis多表操作查询功能_第5张图片

修改User实体

MyBatis多表操作查询功能_第6张图片

package com.zg.domain;

import java.util.Date;
import java.util.List;

public class User {
    private int id;
    private String username;
    private String password;
    private Date birthday;

    //描述当前用户存在哪些订单
    private List  orderList;

    public List getOrderList() {
        return orderList;
    }

    public void setOrderList(List orderList) {
        this.orderList = orderList;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", birthday=" + birthday +
                ", orderList=" + orderList +
                '}';
    }

    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}

创建UserMapper接口

package com.zg.mapper;

import com.zg.domain.User;

import java.util.List;

public interface UserMapper {
    public List findAll();

}

配置UserMapper.xml






    
        
        
        
        
        
        
        
            
            
            
            
        
    

    



测试

 @Test//测试一对多
    public void test2() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List userList = mapper.findAll();
        for (User user : userList) {
            System.out.println(user);
        }



        sqlSession.close();
    }

MyBatis多表操作查询功能_第7张图片

多对多查询

用户表和角色表的关系为,一个用户有多个角色,一个角色被多个用户使用
多对多查询的需求:查询用户同时查询该用户的所有角色

MyBatis多表操作查询功能_第8张图片

select * from user u,sys_user_role ur ,sys_role r where u.id=ur.userId and ur.roleId=r.id

MyBatis多表操作查询功能_第9张图片

创建Role实体,修改User实体

MyBatis多表操作查询功能_第10张图片

添加UserMapper接口

package com.zg.mapper;

import com.zg.domain.User;

import java.util.List;

public interface UserMapper {
    public List findAll();

    public List findUserAndRoleAll();

}

配置UserMapper.xml


        
        
        
        
        

        
        
            
            
            
        

    
    

测试代码

 @Test//测试多对多
    public void test3() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List userAndRoleAll = mapper.findUserAndRoleAll();
        for (User user : userAndRoleAll) {
            System.out.println(user);
        }


        sqlSession.close();
    }

MyBatis多表操作查询功能_第11张图片

到此这篇关于MyBatis多表操作的文章就介绍到这了,更多相关MyBatis多表操作内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(MyBatis多表操作查询功能)