Please indicate the source for reprinting:
What is coming before: Spring+SpringMVC+MyBatis in-depth learning and construction (5) - dynamic sql
(1) The data content recorded in each table
The content recorded in each table is divided into modules. Familiarity is equivalent to the process of learning system requirements (functions).
(2) Important field settings for each table
Non-empty fields and foreign key fields
(3)The relationship between database-level tables
Foreign key relationship
(4) Business relationship between tables
When analyzing the business relationship between tables, it must be based on a certain business meaning. Go up and analyze.
Query order information, association Query order user information.
Determine the main table of the query: the order table
Determine the associated table of the query: the user table
Does associated query use internal links? Or an external link?
Since there is a foreign key (user_id) in the orders table, querying the user table through foreign key association can only query one record, and internal links can be used.
SELECT orders.*, USER.username, USER.sex, USER.address FROM orders, USER WHERE orders.user_id = user.id
Map the results of the above SQL query to the pojo. The pojo must include all query column names.
The original Orders.java cannot map all fields, and a new pojo needs to be created.
Create a pojo class that inherits more query fields.
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--namespace命名空间,作用就是对sql进行分类化的管理,理解为sql隔离 注意:使用mapper代理开发时,namespace有特殊作用,namespace等于mapper接口地址 --><mapper namespace="joanna.yan.mybatis.mapper.OrdersCustomMapper"><!--查询订单,关联查询用户信息 --><select id="findOrdersUser" resultType="joanna.yan.mybatis.entity.OrdersCustom">SELECT orders.*, USER.username, USER.sex, USER.address FROM orders, USER WHERE orders.user_id = user.id</select></mapper>
public interface OrdersCustomMapper {//查询订单,关联查询用户信息public List<OrdersCustom> findOrdersUser() throws Exception; }
@Testpublic void findOrdersUserTest() throws Exception{ SqlSession sqlSession=sqlSessionFactory.openSession(); OrdersCustomMapper ordersCustomMapper=sqlSession.getMapper(OrdersCustomMapper.class); List<OrdersCustom> list=ordersCustomMapper.findOrdersUser(); System.out.println(list); sqlSession.close(); }
Sql implemented with resultType
Use resultMap to map the order information in the query results to the Orders object, add the User attribute in the Orders class, and associate the query The user information that comes out is mapped to the user attribute in the orders object.
<!--订单关联查询用户的resultMap 将整个查询的结果映射到oanna.yan.mybatis.entity.Orders中 --><resultMap type="joanna.yan.mybatis.entity.Orders" id="OrdersUserResultMap"><!-- 1.配置映射的订单信息 --><!-- id:指定查询列中的唯一标识,订单信息中的唯一标识,如果有多个列组成唯一标识,配置多个id column:订单信息中的唯一标识列 property:订单信息中的唯一标识列所映射到Orders类中的哪个属性 --><id column="id" property="id"/><result column="user_id" property="userId"/><result column="number" property="number"/><result column="createtime" property="createtime"/><result column="note" property="note"/><!-- 2.配置映射的关联的用户信息 --><!-- association:用于映射关联查询单个对象的信息 property:要将关联查询的用户信息映射到Orders类中的哪个属性 --><association property="user" javaType="joanna.yan.mybatis.entity.User"><!-- 关联查询用户的唯一标识 column:指定唯一标识用户信息的列 property:映射到user的哪个属性 --><id column="user_id" property="id"/><result column="username" property="username"/><result column="sex" property="sex"/><result column="address" property="sex"/></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>
public interface OrdersCustomMapper {//查询订单,关联查询用户信息,使用resultMappublic List<Orders> findOrdersUserResultMap() throws Exception; }
@Testpublic void findOrdersUserResultMapTest() throws Exception{ SqlSession sqlSession=sqlSessionFactory.openSession(); OrdersCustomMapper ordersCustomMapper=sqlSession.getMapper(OrdersCustomMapper.class); List<Orders> list=ordersCustomMapper.findOrdersUserResultMap(); System.out.println(list); sqlSession.close(); }
Realize one-to-one query:
resultType: Use resultType to achieve more Simple, if the pojo does not include the queried column name, you need to add the attribute corresponding to the column name to complete the mapping.
If there are no special requirements for query results, it is recommended to use resultType.
resultMap: You need to define resultMap separately, which is a bit troublesome to implement. If you have special requirements for the query results, you can use resultMap to map the associated query to the attributes of the pojo.
resultMap can implement delayed loading, but resultType cannot implement delayed loading.
Query order and order details information
Determine the master Query table: Order table
Determine the association Query table: Order details table
Just add the order details table association based on one-to-one query.
SELECT orders.*, USER.username, USER.sex, USER.address, orderdetail.id orderdetail_id, orderdetail.items_id, orderdetail.items_num, orderdetail.orders_idFROM orders, USER, orderdetailWHERE orders.user_id = user.id AND orderdetail.orders_id=orders.id
Use resultType to map the above query results to pojo, and the order information will be repeated.
Requirements:
The mapping of orders cannot have duplicate records.
Solution:
Add the List
The order information will eventually be mapped to Orders, and the order details corresponding to the order will be mapped to the orderDetails attribute in orders.
The number of mapped orders records is two (orders information is not repeated)
The orderDetails attribute in each order stores the order details corresponding to the order.
<!--订单及订单明细的resultMap 使用extends继承,就不需要再配置订单信息和用户信息的映射了 --><resultMap type="joanna.yan.mybatis.entity.Orders" id="OrdersAndOrderDetailResultMap" extends="OrdersUserResultMap"><!-- 1.配置映射的订单信息 --><!-- 2.配置映射的关联的用户信息 --><!-- 使用extends继承,就不需要再配置订单信息和用户信息的映射了 --><!-- 3.配置映射的订单明细信息 --><!-- 订单明细信息 一个订单关联查询出了多条明细,要使用collection进行映射 collection:对关联查询到的多条记录映射到集合对象中 property:将关联查询到多条记录映射到joanna.yan.mybatis.entity.Orders中的哪个属性 ofType:指定映射到list集合属性中pojo的类型 --><collection property="orderdetails" ofType="joanna.yan.mybatis.entity.Orderdetail"><!-- id:订单明细的唯一标识 property:要讲订单明细的唯一标识映射到joanna.yan.mybatis.entity.Orderdetail的哪个属性 --><id column="orderdetail_id" property="id"/><result column="items_id" property="itemsId"/><result column="items_num" property="itemsNum"/><result column="orders_id" property="ordersId"/></collection></resultMap>3.6mapper. xml
<!-- 查询订单,关联查询用户及订单明细,使用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>3.7mapper.java
public interface OrdersCustomMapper {//查询订单,关联查询用户信息public List<OrdersCustom> findOrdersUser() throws Exception;//查询订单,关联查询用户信息,使用resultMappublic List<Orders> findOrdersUserResultMap() throws Exception;//查询订单(关联用户)及订单明细public List<Orders> findOrdersAndOrderDetailResultMap() throws Exception; }3.8 Test program
@Testpublic void findOrdersAndOrderDetailResultMapTest() throws Exception{ SqlSession sqlSession=sqlSessionFactory.openSession(); OrdersCustomMapper ordersCustomMapper=sqlSession.getMapper(OrdersCustomMapper.class); List<Orders> list=ordersCustomMapper.findOrdersAndOrderDetailResultMap(); System.out.println(list); sqlSession.close(); }
mybatis使用resultMap的collection对关联查询的多条记录映射到有个list集合属性中。
使用resultType实现:
将订单明细映射到orders中的orderdetails中,需要自己处理,使用双重循环遍历,去掉重复记录,将订单明细放在ordertails中。
查询用户及用户购买的商品信息。
查询主表:用户表
关联表:由于用户和商品没有直接关联,通过订单和订单明细进行关联,所有关联表:orders、orderdetail、items。
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_priceFROM orders, USER, orderdetail, itemsWHERE orders.user_id = user.id AND orderdetail.orders_id=orders.id AND orderdetail.items_id = items.id
将用户信息映射到user中。
在User类中添加订单列表属性List
在Orders中田间订单明细列表属性List
在OrderDetail中添加Items属性,将订单明细所对应的商品映射到Items。
<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>
<!-- 查询用户及购买商品 --><resultMap type="joanna.yan.mybatis.entity.User" id="UserAndItemsResultMap"><!-- 1.用户信息 --><id column="user_id" property="id"/><result column="username" property="username"/><result column="sex" property="sex"/><result column="address" property="address"/><!-- 2.订单信息 --><!-- 一个用户对应多个订单,使用collection映射 --><collection property="ordersList" ofType="joanna.yan.mybatis.entity.Orders"><id column="id" property="id"/><result column="user_id" property="userId"/><result column="number" property="number"/><result column="createtime" property="createtime"/><result column="note" property="note"/><!-- 3.订单明细 --><!-- 一个订单包括多个明细 --><collection property="orderdetails" ofType="joanna.yan.mybatis.entity.Orderdetail"><id column="orderdetail_id" property="id"/><result column="items_id" property="itemsId"/><result column="items_num" property="itemsNum"/><result column="orders_id" property="ordersId"/><!-- 4.商品信息 --><!-- 一个订单明细对应一个商品 --><association property="items" javaType="joanna.yan.mybatis.entity.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>
public interface OrdersCustomMapper {//查询订单,关联查询用户信息public List<OrdersCustom> findOrdersUser() throws Exception;//查询订单,关联查询用户信息,使用resultMappublic List<Orders> findOrdersUserResultMap() throws Exception;//查询订单(关联用户)及订单明细public List<Orders> findOrdersAndOrderDetailResultMap() throws Exception;//查询用户购买商品信息public List<User> findUserAndItemsResultMap() throws Exception; }
@Testpublic void findUserAndItemsResultMapTest() throws Exception{ SqlSession sqlSession=sqlSessionFactory.openSession(); OrdersCustomMapper ordersCustomMapper=sqlSession.getMapper(OrdersCustomMapper.class); List<User> list=ordersCustomMapper.findUserAndItemsResultMap(); System.out.println(list); sqlSession.close(); }
将查询用户购买的商品信息明细清单(用户名、用户地址、购买商品名称、购买商品时间、购买商品数量)
针对上面的需求就使用resultType将查询到的记录映射到一个扩展的pojo中,很简单实现明细清单的功能。
一对多是多对多的特例,如下需求:
查询用户购买的商品信息,用户和商品的关系是多对多关系。
需求1:
查询字段:用户账号、用户名称、用户性别、商品名称、商品价格(最常见)
企业开发中常见明细列表,用户购买商品明细列表,
使用resultType将上边查询列映射到pojo输出。
需求2:
查询字段:用户账号、用户名称、购买商品数量、商品明细(鼠标移上显示明细)
使用resultMap将用户购买的商品明细列表映射到user对象中。
总结:
使用resultMap是针对那些对查询结果映射有特殊要求的功能,比如特殊要求映射成list中包含多个list。
resultType:
作用:将查询结果按照sql列名pojo属性一致性映射到pojo中。
场合:
常见一些明细记录的展示,比如用户购买商品明细,将关联查询信息全部展示在页面时,此时可直接使用resultType将每一条记录映射到pojo中,在前端页面遍历list(list中是pojo)即可。
resultMap:
使用association和collection完成一对一和一对多高级映射(对结果又特殊的映射要求)。
association:
作用:将关联查询信息映射到一个pojo对象中。
场合:
为了方便查询关联信息可以使用association将关联订单信息映射为用户对象的pojo属性中,比如:查询订单及关联用户信息。
使用resultType无法将查询结果映射到pojo对象的pojo属性中,根据对结果集查询遍历的需要选择使用resultType还是resultMap。
collection:
作用:将关联查询信息映射到一个list集合中。
场合:为了方便擦还行遍历关联信息可以使用collection将关联信息映射到list集合中,比如:查询用户权限范围模块及模块下的菜单,可使用collection将模块映射到模块list中,将菜单列表映射到模块对象的菜单list属性中,这样做册目的也是方便对查询结果集进行遍历查询。
如果使用resultType无法将查询结果映射到list集合中。
The above is the detailed content of Example tutorial of MyBatis related query. For more information, please follow other related articles on the PHP Chinese website!