微前端架构如何改变企业的开发模式与效率提升
647
2022-11-23
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
}
配置OrderMapper.xml
SELECT *,o.id oid FROM orders o,USER u WHERE o.uid=u.id
sqlMapConfig.xml
<environments default="developement">
在一对一配置的时候,在order实体中创建了一个user,所以property属性都使用user.** 的方式进行编写,但是这里还可以使用association
SELECT *,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 List
public List
return orderList;
}
public void setOrderList(List
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
public List
return orderList;
}
public void setOrderList(List
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
}
配置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 http://SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List
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
public List
}
配置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);
List
for (User user : userAndRoleAll) {
System.out.println(user);
}
sqlSession.close();
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~