欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-25 11:45:57
思路自定义方法,使用wrapper,自定义映射结果集mapper接口package com.mozq.boot.mpsand01.dao;import com.baomidou.mybatisplus...

思路

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

mapper接口

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

<?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>

测试类

@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查询结果封装到指定实体内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!