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 ListfindAll(); }
配置OrderMapper.xml
SELECT *,o.id oid FROM orders o,USER u WHERE o.uid=u.id
sqlMapConfig.xml
文章图片
在一对一配置的时候,在order实体中创建了一个user,所以property属性都使用
user.**
的方式进行编写,但是这里还可以使用associationSELECT *,o.id oid FROM orders o,USER u WHERE o.uid=u.id
一对多查询的模型 用户表和订单表的关系为,一个用户有多个订单,一个当但只从属一个用户
一对多查询需求:查询一个用户,与此同时查询出该用户具有的订单
文章图片
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 ListorderList; public List getOrderList() {return orderList; }public void setOrderList(List orderList) {this.orderList = orderList; }@Overridepublic 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 ListorderList; public List getOrderList() {return orderList; }public void setOrderList(List orderList) {this.orderList = orderList; }@Overridepublic 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 ListfindAll(); }
配置UserMapper.xml
select *,o.id oid from user u,orders o where u.id=o.uid
测试
@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); ListuserList = 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 ListfindAll(); public List findUserAndRoleAll(); }
配置UserMapper.xml
select * from user u,sys_user_role ur ,sys_role r where u.id=ur.userId and ur.roleId=r.id
测试代码
@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); ListuserAndRoleAll = mapper.findUserAndRoleAll(); for (User user : userAndRoleAll) {System.out.println(user); }sqlSession.close(); }
文章图片
【MyBatis多表操作查询功能】到此这篇关于MyBatis多表操作的文章就介绍到这了,更多相关MyBatis多表操作内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- 2.6|2.6 Photoshop操作步骤的撤消和重做 [Ps教程]
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- mybatisplus如何在xml的连表查询中使用queryWrapper
- mybatisplus|mybatisplus where QueryWrapper加括号嵌套查询方式
- MybatisPlus|MybatisPlus LambdaQueryWrapper使用int默认值的坑及解决
- MybatisPlus使用queryWrapper如何实现复杂查询
- MongoDB,Wondows下免安装版|MongoDB,Wondows下免安装版 (简化版操作)
- 在线版的迅捷思维导图怎么操作()
- 操作系统|[译]从内部了解现代浏览器(1)
- 数据库总结语句