需求:
一、一对一查询
查询订单信息,关联查询创建订单的用户信息;
package com.cy.po; import java.util.Date;
import java.util.List; public class Orders {
private Integer id; private Integer userId; private String number; private Date createtime; private String note; 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 == null ? null : number.trim();
} 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 == null ? null : note.trim();
}
}
Orders
pojo-- User:
package com.cy.po; import java.util.Date; public class User {
//属性名和数据库表的字段对应
private int id;
private String username;// 用户姓名
private String sex;// 性别
private Date birthday;// 生日
private String address;// 地址
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;
}
@Override
public String toString() {
return "------->> User [id=" + id + ", username=" + username + ", sex=" + sex
+ ", birthday=" + birthday + ", address=" + address + "]";
}
}
User
pojo -- Order扩展类 OrdersCustom:
package com.cy.po; /**
*
* <p>Description: 订单的扩展类</p>
*/
//通过此类映射订单和用户查询的结果,让此类继承包括 字段较多的pojo类
public class OrdersCustom extends Orders{ //添加用户属性
/*USER.username,
USER.sex,
USER.address */
private String username;
private String sex;
private String address;
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
} }
OrdersCustom
Mapper.xml:
<mapper namespace="com.cy.mapper.OrdersMapperCustom"> <!-- 查询订单关联查询用户信息 -->
<select id="findOrdersUser" resultType="com.cy.po.OrdersCustom">
SELECT orders.*,USER.username,USER.sex,USER.address
FROM orders,USER
WHERE orders.user_id = user.id
</select> </mapper>
Mapper接口:
/**
*
* <p>Title: OrdersMapperCustom</p>
* <p>Description: 订单mapper</p>
*/
public interface OrdersMapperCustom { //查询订单关联查询用户信息
public List<OrdersCustom> findOrdersUser()throws Exception; }
测试代码:
public class OrdersMapperCustomTest {
private SqlSessionFactory sqlSessionFactory; @Before
public void setUp() throws Exception {
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} @Test
public void testFindOrdersUser() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
OrdersMapperCustom ordersMapperCustom = sqlSession.getMapper(OrdersMapperCustom.class);
List<OrdersCustom> list = ordersMapperCustom.findOrdersUser();
System.out.println(list.toString());
sqlSession.close();
} }
2)使用resultMap方法:
思路:使用resultMap将查询结果中的订单信息映射到Orders对象中,在orders类中添加User属性,将关联查询出来的用户信息映射到orders对象中的user属性中。
Orders中保存user:
package com.cy.po; import java.util.Date;
import java.util.List; public class Orders {
private Integer id; private Integer userId; private String number; private Date createtime; private String note; private User user; public User getUser() {
return user;
} public void setUser(User user) {
this.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 == null ? null : number.trim();
} 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 == null ? null : note.trim();
}
}
包含user的Orders
Mapper.xml:
<mapper namespace="com.cy.mapper.OrdersMapperCustom">
<!-- 订单查询关联用户的resultMap 将整个查询的结果映射到com.cy.po.Orders中-->
<resultMap id="OrdersUserResultMap" type="com.cy.po.Orders">
<!-- 配置映射的订单信息 -->
<!-- id:指定查询列中的唯 一标识,订单信息的中的唯 一标识,如果有多个列组成唯一标识,配置多个id
column:订单信息的唯一标识列
property:订单信息的唯一标识 列所映射到Orders中哪个属性
-->
<id column="id" property="id"></id>
<result column="user_id" property="userId"></result>
<result column="number" property="number"></result>
<result column="createtime" property="createtime"></result>
<result column="note" property="note"></result>
<!-- 配置映射的关联的用户信息 -->
<!-- association:用于映射关联查询单个对象的信息 property:要将关联查询的用户信息映射到Orders中哪个属性 -->
<association property="user" javaType="com.cy.po.User">
<!-- id:关联查询用户的唯 一标识
column:指定唯一标识用户信息的列 property:映射到user的哪个属性 -->
<id column="user_id" property="id"/>
<result column="username" property="username"/>
<result column="sex" property="sex"/>
<result column="address" property="address"/>
</association>
</resultMap> <!-- 查询订单关联查询用户信息,使用resultmap -->
<select id="findOrdersUserResultMap" resultMap="OrdersUserResultMap">
SELECT
orders.*,
USER.username,
USER.sex,
USER.address
FROM
orders,
USER
WHERE orders.user_id = user.id
</select> </mapper>
mapper接口:
//查询订单关联查询用户使用resultMap
public List<Orders> findOrdersUserResultMap()throws Exception;
测试代码:
@Test
public void testFindOrdersUserResultMap() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
OrdersMapperCustom ordersMapperCustom = sqlSession.getMapper(OrdersMapperCustom.class); List<Orders> orders = ordersMapperCustom.findOrdersUserResultMap();
System.out.println(orders);
sqlSession.close();
}
使用resultType和resultMap实现一对一查询的总结:
resultType:使用resultType实现较为简单,如果pojo中没有包括查询出来的列名,需要增加列名对应的属性,即可完成映射。
resultMap:需要单独定义resultMap,如果对查询结果有特殊的要求,使用resultMap可以完成将关联查询映射pojo的属性中。
resultMap可以实现延迟加载,resultType无法实现延迟加载。
二、一对多查询
需求:查询订单及订单明细的信息。
在orders.java类中添加List<orderDetail> orderDetails属性:
package com.cy.po; import java.util.Date;
import java.util.List; public class Orders {
private Integer id; private Integer userId; private String number; private Date createtime; private String note; //用户信息
private User user; private List<Orderdetail> orderdetails; public List<Orderdetail> getOrderdetails() {
return orderdetails;
} public void setOrderdetails(List<Orderdetail> orderdetails) {
this.orderdetails = orderdetails;
} public User getUser() {
return user;
} public void setUser(User user) {
this.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 == null ? null : number.trim();
} 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 == null ? null : note.trim();
}
}
Orders中包含Orderdetail的集合属性
package com.cy.po; public class Orderdetail {
private Integer id; private Integer ordersId; private Integer itemsId; private Integer itemsNum; //明细对应的商品信息
private Items items; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public Integer getOrdersId() {
return ordersId;
} public void setOrdersId(Integer ordersId) {
this.ordersId = ordersId;
} public Integer getItemsId() {
return itemsId;
} public void setItemsId(Integer itemsId) {
this.itemsId = itemsId;
} public Integer getItemsNum() {
return itemsNum;
} public void setItemsNum(Integer itemsNum) {
this.itemsNum = itemsNum;
} public Items getItems() {
return items;
} public void setItems(Items items) {
this.items = items;
} @Override
public String toString() {
return "Orderdetail [id=" + id + ", ordersId=" + ordersId
+ ", itemsId=" + itemsId + ", itemsNum=" + itemsNum + "]";
} }
订单明细类Orderdetail
mapper.xml:
<mapper namespace="com.cy.mapper.OrdersMapperCustom">
<!-- 订单及订单明细的resultMap 使用extends继承,不用在中配置订单信息和用户信息的映射 -->
<resultMap id="OrdersAndOrderDetailResultMap" type="com.cy.po.Orders" extends="OrdersUserResultMap" >
<!-- 订单信息 --><!-- 用户信息 --><!-- 使用extends继承,不用在中配置订单信息和用户信息的映射 --> <!-- 订单明细信息 一个订单关联查询出了多条明细,要使用collection进行映射
collection:对关联查询到多条记录映射到集合对象中
property:将关联查询到多条记录映射到com.cy.po.Orders哪个属性
ofType:指定映射到list集合属性中pojo的类型
-->
<collection property="orderdetails" ofType="com.cy.po.Orderdetail">
<!-- id:订单明细唯一标识 property:要将订单明细的唯 一标识映射到com.cy.po.Orderdetail的哪个属性 -->
<id column="orderdetail_id" property="id"></id>
<result column="items_id" property="itemsId"/>
<result column="items_num" property="itemsNum"/>
<result column="orders_id" property="ordersId"/>
</collection>
</resultMap>
<!-- 查询订单关联查询用户及订单明细,使用resultmap -->
<select id="findOrdersAndOrderDetailResultMap" resultMap="OrdersAndOrderDetailResultMap">
SELECT
orders.*,
USER.username,
USER.sex,
USER.address,
orderdetail.id orderdetail_id,
orderdetail.items_id,
orderdetail.items_num,
orderdetail.orders_id
FROM
orders,
USER,
orderdetail
WHERE orders.user_id = user.id AND orderdetail.orders_id=orders.id
</select> </mapper>
mapper接口:
//查询订单(关联用户)及订单明细
public List<Orders> findOrdersAndOrderDetailResultMap() throws Exception;
测试代码:
@Test
public void FindOrdersAndOrderDetailResultMap() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
OrdersMapperCustom ordersMapperCustom = sqlSession.getMapper(OrdersMapperCustom.class); List<Orders> orders = ordersMapperCustom.findOrdersAndOrderDetailResultMap();
System.out.println(orders);
sqlSession.close();
}
三、多对多查询
需求:查询用户及用户购买商品信息
思路:
将用户信息映射到user中。
在user类中添加订单列表属性List<Orders> orderslist,将用户创建的订单映射到orderslist
在Orders中添加订单明细列表属性List<OrderDetail>orderdetials,将订单的明细映射到orderdetials
在OrderDetail中添加Items属性,将订单明细所对应的商品映射到Items
在上面代码基础上,User类中添加List<Orders> orderslist:
package com.cy.po; import java.util.Date;
import java.util.List; public class User {
//属性名和数据库表的字段对应
private int id;
private String username;// 用户姓名
private String sex;// 性别
private Date birthday;// 生日
private String address;// 地址 private List<Orders> orderslist; public List<Orders> getOrderslist() {
return orderslist;
}
public void setOrderslist(List<Orders> 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;
}
@Override
public String toString() {
return "------->> User [id=" + id + ", username=" + username + ", sex=" + sex
+ ", birthday=" + birthday + ", address=" + address + "]";
}
}
User中保存List
mapper.xml:
<mapper namespace="com.cy.mapper.OrdersMapperCustom">
<!-- 查询用户及购买的商品 -->
<resultMap type="com.cy.po.User" id="UserAndItemsResultMap">
<!-- 用户信息 -->
<id column="user_id" property="id"/>
<result column="username" property="username"/>
<result column="sex" property="sex"/>
<result column="address" property="address"/> <!-- 订单信息 一个用户对应多个订单,使用collection映射-->
<collection property="orderslist" ofType="com.cy.po.Orders">
<id column="id" property="id"></id>
<result column="user_id" property="userId"></result>
<result column="number" property="number"></result>
<result column="createtime" property="createtime"></result>
<result column="note" property="note"></result>
<!-- 订单明细 一个订单包括 多个明细-->
<collection property="orderdetails" ofType="com.cy.po.Orderdetail">
<id column="orderdetail_id" property="id"></id>
<result column="items_id" property="itemsId"/>
<result column="items_num" property="itemsNum"/>
<result column="orders_id" property="ordersId"/>
<!-- 商品信息一个订单明细对应一个商品-->
<association property="items" javaType="com.cy.po.Items">
<id column="items_id" property="id"/>
<result column="items_name" property="name"/>
<result column="items_detail" property="detail"/>
<result column="items_price" property="price"/>
</association>
</collection>
</collection>
</resultMap>
<!-- 查询用户及购买的商品信息,使用resultmap -->
<select id="findUserAndItemsResultMap" resultMap="UserAndItemsResultMap">
SELECT
orders.*,
USER.username,
USER.sex,
USER.address,
orderdetail.id orderdetail_id,
orderdetail.items_id,
orderdetail.items_num,
orderdetail.orders_id,
items.name items_name,
items.detail items_detail,
items.price items_price
FROM
orders,
USER,
orderdetail,
items
WHERE orders.user_id = user.id AND orderdetail.orders_id=orders.id AND orderdetail.items_id = items.id
</select> </mapper>
上面,其实思路就是用户中保存List<Orders>,然后Order中有多个订单明细List<Orderdetail>;一个订单明细对应一个商品,private Item;
顺着写来来就是了,不难。
mapper接口:
//查询用户购买商品信息
public List<User> findUserAndItemsResultMap() throws Exception;
测试代码:
@Test
public void FindUserAndItemsResultMap() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
OrdersMapperCustom ordersMapperCustom = sqlSession.getMapper(OrdersMapperCustom.class); List<User> users = ordersMapperCustom.findUserAndItemsResultMap();
System.out.println(users);
sqlSession.close();
}
多对多查询总结:
将查询用户购买的商品信息明细清单,(用户名、用户地址、购买商品名称、购买商品时间、购买商品数量)
针对上边的需求就使用resultType将查询到的记录映射到一个扩展的pojo中,很简单实现明细清单的功能。