Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Association query in Mybatis_day05

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

本篇文章给大家分享的是有关Mybatis_day05中怎么实现关联查询,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

关联查询

一对一查询

案例:查询所有订单信息,关联查询下单用户信息。

注意:因为一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查询。如果从用户信息出发查询用户下的订单信息则为一对多查询,因为一个用户可以下多个订单。

方法一:

使用resultType,定义订单信息po类,此po类中包括了订单信息和用户信息:

Sql语句:

SELECT orders.*, user.username, userss.address FROM orders, user WHERE orders.user_id = user.id

定义po类

Po类中应该包括上边sql查询出来的所有字段,如下:

public class OrdersCustom extends Orders { private String username;// 用户名称 private String address;// 用户地址 get/set。。。。 OrdersCustom类继承Orders类后OrdersCustom类包括了Orders类的所有字段,只需要定义用户的信息字段即可。

Mapper.xml

SELECT orders.*, user.username, user.address FROM orders, user WHERE orders.user_id = user.id

Mapper接口:

public List findOrdersList() throws Exception; 测试: Public void testfindOrdersList()throws Exception{ //获取session SqlSession session = sqlSessionFactory.openSession(); //获限mapper接口实例 UserMapper userMapper = session.getMapper(UserMapper.class); //查询订单信息 List list = userMapper.findOrdersList(); System.out.println(list); //关闭session session.close(); }

小结:

定义专门的po类作为输出类型,其中定义了sql查询结果集所有的字段。此方法较为简单,企业中使用普遍。

方法二:

使用resultMap,定义专门的resultMap用于映射一对一查询结果。

Sql语句:

SELECT orders.*, user.username, user.address FROM orders, user WHERE orders.user_id = user.id

定义po类

在Orders类中加入User属性,user属性中用于存储关联查询的用户信息,因为订单关联查询用户是一对一关系,所以这里使用单个User对象存储关联查询的用户信息。

public class Orders { private Integer id; private Integer userId; private String number; private Date createtime; private String note; private User user; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public Date getCreatetime() { return createtime; } public void setCreatetime(Date createtime) { this.createtime = createtime; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public String toString() { return "Orders{" + "id=" + id + ", userId=" + userId + ", number='" + number + '\'' + ", createtime=" + createtime + ", note='" + note + '\'' + ", user=" + user + '}'; } }

Mapper.xml

SELECT o.id, o.user_id, o.number, o.createtime, o.note, u.username, u.address FROM orders o JOIN `user` u ON u.id = o.user_id 这里resultMap指定orderUserResultMap。 association:表示进行关联查询单条记录 property:表示关联查询的结果存储在com.pp.mybatis.po.Orders的user属性中 javaType:表示关联查询的结果类型 :查询结果的user_id列对应关联对象的id属性,这里是表示user_id是关联查询对象的唯一标识。 :查询结果的username列对应关联对象的username属性。

Mapper接口:

public List findOrdersListResultMap() throws Exception; 测试: Public void testfindOrdersListResultMap()throws Exception{ //获取session SqlSession session = sqlSessionFactory.openSession(); //获限mapper接口实例 UserMapper userMapper = session.getMapper(UserMapper.class); //查询订单信息 List list = userMapper.findOrdersList2(); System.out.println(list); //关闭session session.close(); }

小结:

使用association完成关联查询,将关联查询信息映射到pojo对象中。

一对多查询

案例:查询所有用户信息及用户关联的订单信息。

用户信息和订单信息为一对多关系。

使用resultMap实现如下:

Sql语句:

SELECT u.*, o.id oid, o.number, o.createtime, o.note FROM `user` u LEFT JOIN orders o ON u.id = o.user_id

定义po类

在User类中加入Listorders属性

public class User { private int id; private String username; private String sex; private Date birthday; private String address; private List ordersList; @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", sex='" + sex + '\'' + ", birthday=" + birthday + ", address='" + address + '\'' + ", ordersList=" + ordersList + '}'; } public List getOrdersList() { return ordersList; } public void setOrdersList(List ordersList) { this.ordersList = ordersList; } 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 getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }

Mapper.xml

SELECT u.*, o.id oid, o.number, o.createtime, o.note FROM `user` u LEFT JOIN orders o ON u.id = o.user_id

collection部分定义了用户关联的订单信息。表示关联查询结果集

property="orders":关联查询的结果集存储在User对象的上哪个属性。

ofType="orders":指定关联查询的结果集中的对象类型即List中的对象类型。此处可以使用别名,也可以使用全限定名。

及的意义同一对一查询。

Mapper接口:

List getUserOrderList();

测试

@Test public void getUserOrderList() { SqlSession session = sqlSessionFactory.openSession(); UserMapper userMapper = session.getMapper(UserMapper.class); List result = userMapper.getUserOrderList(); for (User user : result) { System.out.println(user); } session.close(); }以上就是Mybatis_day05中怎么实现关联查询,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report