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

网友投稿 622 2022-11-20


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

思路

自定义方法,使用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 {

@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 findByCondition(@Param(Constants.WRAPPER)Wrapper wrapper);

List selectOrderVOList(@Param(Constants.WRAPPER)Wrapper wrapper);

}

Mapper.xml

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<result column="ORDER_TYPE" property="orderType" jdbcType="INTEGER" />

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

select

from order_info

${ew.customSqlSegment}

测试类

@Test

public void selectOrderVOList(){

List orderVOList = orderInfoDao.selectOrderVOList(

Wrappers.lambdaQuery()

.eq(OrderInfo::getUserId, 123456)

.eq(OrderInfo::getCompanyId, 1)

);

System.out.println(orderVOList.size());

List byCondition = orderInfoDao.findByCondition(

Wrappers.lambdaQuery()

.eq(OrderInfo::getUserId, 123456)

.eq(OrderInfo::getCompanyId, 1)

);

System.out.println(byCondition.size());

List orderInfoList = orderInfoDao.selectList(

Wrappers.lambdaQuery()

.eq(OrderInfo::getUserId, 123456)

.eq(OrderInfo::getCompanyId, 1)

);

System.out.println(orderInfoList.size());

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java8 多个list对象用lambda求差集操作
下一篇:在C#和Java语言中for和foreach的区别详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~