千家信息网

MyBatis多表操作查询功能怎么用

发表于:2024-11-30 作者:千家信息网编辑
千家信息网最后更新 2024年11月30日,小编给大家分享一下MyBatis多表操作查询功能怎么用,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!一对一查询用户表和订单表的关系为,一个用户多个订单,一个订单只从属一个用户一对一查
千家信息网最后更新 2024年11月30日MyBatis多表操作查询功能怎么用

小编给大家分享一下MyBatis多表操作查询功能怎么用,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

一对一查询

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

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

创建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

                                                                                                                                                                                                

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

                                                                                                                                                                

一对多查询的模型

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

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;    }}

修改User实体

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();    }

多对多查询

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

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

创建Role实体,修改User实体

添加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多表操作查询功能怎么用"有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!

0