服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|

服务器之家 - 编程语言 - JAVA教程 - MyBatis Plus 将查询结果封装到指定实体的方法步骤

MyBatis Plus 将查询结果封装到指定实体的方法步骤

2020-09-26 00:42魔有追求 JAVA教程

这篇文章主要介绍了MyBatis Plus 将查询结果封装到指定实体的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

思路

自定义方法,使用Wrapper,自定义映射结果集

Mapper接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.mozq.boot.mpsand01.dao;
 
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.mozq.boot.mpsand01.pojo.OrderInfo;
import com.mozq.boot.mpsand01.vo.OrderVO;
import org.apache.ibatis.annotations.*;
 
import java.util.List;
 
@Mapper
public interface OrderInfoDao extends BaseMapper<OrderInfo> {
  @Select("select * from order_info ${ew.customSqlSegment}")
  /* 只指定2个,其他列能自动匹配的,也会被映射。
  @Results({
      @Result(id = true, column = "ORDER_ID", property = "orderId", jdbcType = JdbcType.VARCHAR),
      @Result(column = "USER_ID", property = "userId", jdbcType = JdbcType.INTEGER)
  })
  */
  List<OrderVO> findByCondition(@Param(Constants.WRAPPER)Wrapper wrapper);
 
  List<OrderVO> selectOrderVOList(@Param(Constants.WRAPPER)Wrapper wrapper);
}

Mapper.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?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">
<mapper namespace="com.mozq.boot.mpsand01.dao.OrderInfoDao">
 
  <resultMap id="BaseResultMapVO" type="com.mozq.boot.mpsand01.vo.OrderVO">
    <id column="ORDER_ID" property="orderId" jdbcType="VARCHAR" />
    <result column="USER_ID" property="userId" jdbcType="INTEGER" />
    <result column="COMPANY_ID" property="companyId" jdbcType="INTEGER" />
    <result column="CUSTOMER_ID" property="customerId" jdbcType="INTEGER" />
    <result column="CUSTOMER_NAME" property="customerName" jdbcType="VARCHAR" />
    <result column="MOBILE_PHONE" property="mobilePhone" jdbcType="VARCHAR" />
    <result column="ACCOUNT_ID" property="accountId" jdbcType="VARCHAR" />
    <result column="PRODUCT_ID" property="productId" jdbcType="INTEGER" />
    <result column="PRODUCT_NAME" property="productName" jdbcType="VARCHAR" />
    <result column="ORDER_TYPE" property="orderType" jdbcType="INTEGER" />
    <result column="QUANTITY" property="quantity" jdbcType="DECIMAL" />
    <result column="LICENSE_PLATE_NUMBER" property="licensePlateNumber" jdbcType="VARCHAR" />
    <result column="PRICE" property="price" jdbcType="DECIMAL" />
    <result column="TOTAL_MONEY" property="totalMoney" jdbcType="DECIMAL" />
    <result column="PAY_QUANTITY" property="payQuantity" jdbcType="DECIMAL" />
    <result column="PAY_MONEY" property="payMoney" jdbcType="DECIMAL" />
    <result column="THE_WEIGHT" property="theWeight" jdbcType="INTEGER" />
    <result column="DELIVERY_ADDRESS" property="deliveryAddress" jdbcType="VARCHAR" />
    <result column="RECEIVE_ADDRESS" property="receiveAddress" jdbcType="VARCHAR" />
    <result column="ORDER_STATUS" property="orderStatus" jdbcType="INTEGER" />
    <result column="REMARK" property="remark" jdbcType="VARCHAR" />
    <result column="PICKUP_TIME" property="pickupTime" jdbcType="TIMESTAMP" />
    <result column="CREATE_TIME" property="createTime" jdbcType="TIMESTAMP" />
    <result column="UPDATE_TIME" property="updateTime" jdbcType="TIMESTAMP" />
  </resultMap>
 
  <sql id="Base_Column_List">
    ORDER_ID,
    USER_ID,
    COMPANY_ID,
    CUSTOMER_ID,
    CUSTOMER_NAME,
    MOBILE_PHONE,
    ACCOUNT_ID,
    PRODUCT_ID,
    PRODUCT_NAME,
    ORDER_TYPE,
    QUANTITY,
    LICENSE_PLATE_NUMBER,
    PRICE,
    TOTAL_MONEY,
    PAY_QUANTITY,
    PAY_MONEY,
    THE_WEIGHT,
    DELIVERY_ADDRESS,
    RECEIVE_ADDRESS,
    ORDER_STATUS,
    REMARK,
    PICKUP_TIME,
    CREATE_TIME,
    UPDATE_TIME
  </sql>
 
  <select id="selectOrderVOList" resultMap="BaseResultMapVO">
    select <include refid="Base_Column_List"></include>
    from order_info
    ${ew.customSqlSegment}
  </select>
 
</mapper>

测试类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Test
public void selectOrderVOList(){
  List<OrderVO> orderVOList = orderInfoDao.selectOrderVOList(
    Wrappers.<OrderInfo>lambdaQuery()
    .eq(OrderInfo::getUserId, 123456)
    .eq(OrderInfo::getCompanyId, 1)
  );
  System.out.println(orderVOList.size());
  List<OrderVO> byCondition = orderInfoDao.findByCondition(
    Wrappers.<OrderInfo>lambdaQuery()
    .eq(OrderInfo::getUserId, 123456)
    .eq(OrderInfo::getCompanyId, 1)
  );
  System.out.println(byCondition.size());
  List<OrderInfo> orderInfoList = orderInfoDao.selectList(
    Wrappers.<OrderInfo>lambdaQuery()
    .eq(OrderInfo::getUserId, 123456)
    .eq(OrderInfo::getCompanyId, 1)
  );
  System.out.println(orderInfoList.size());
}

到此这篇关于MyBatis Plus 将查询结果封装到指定实体的方法步骤的文章就介绍到这了,更多相关MyBatis Plus查询结果封装到指定实体内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/mozq/p/11755375.html

延伸 · 阅读

精彩推荐