MybatisPlus-写BUG

1. MybatisPlus

mybatis-plus的pom依赖(详细见官网)

        
        
        
            com.baomidou
            mybatis-plus-boot-starter
        
        
        
            com.baomidou
            mybatis-plus-generator
        

2. BUG解决

1.查询部分字段, 但是返回所有字段,未查询字段显示为null

  1. 使用Mybatis-plus做自定义SQL查询部分字段时,查询出了表中所有的字段,sql语句中未提到的字段显示为空的问题:

    实体类 :

    package com.workorder.work_order.pojo;
    
    import com.baomidou.mybatisplus.annotation.TableName;
    import com.baomidou.mybatisplus.annotation.IdType;
    import com.baomidou.mybatisplus.annotation.TableId;
    import java.time.LocalDateTime;
    import com.baomidou.mybatisplus.annotation.TableField;
    import java.io.Serializable;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.EqualsAndHashCode;
    import lombok.NoArgsConstructor;
    import lombok.experimental.Accessors;
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @EqualsAndHashCode(callSuper = false)
    @Accessors(chain = true)
    @TableName("work_order_classification")
    public class WorkOrderClassification implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        @TableId(value = "id", type = IdType.AUTO)
        private Long id;
    
        /**
         * 工单分类信息
         */
        @TableField("name")
        private String name;
        /**
         * 工单关联分类信息
         */
        @TableField("node_id")
        private String nodeId;
    
        /**
         * 创建时间
         */
        @TableField("create_time")
        private LocalDateTime createTime;
    
        /**
         * 更新时间
         */
        @TableField("update_time")
        private LocalDateTime updateTime;
    }
    
    

    Mapper接口:

    package com.workorder.work_order.mapper;
    
    import com.baomidou.mybatisplus.core.conditions.Wrapper;
    import com.baomidou.mybatisplus.core.toolkit.Constants;
    import com.devloper.joker.mybatis.plus.query.core.QuerySupport;
    import com.workorder.work_order.pojo.WorkOrderClassification;
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import org.apache.ibatis.annotations.Param;
    import org.apache.ibatis.annotations.ResultMap;
    import org.apache.ibatis.annotations.Select;
    
    import java.util.List;
    import java.util.Map;
    
    /**
     *
     * @author lw
     * @since 2019-06-27
     */
    public interface WorkOrderClassificationMapper extends BaseMapper {
    
        /**
         * 获取数据列表
         * @param
         * @return
         */
    
        String  getOrderClassification= "SELECT ID,NAME ,NODE_ID FROM work_order_classification";
    
        @ResultMap("ClassificationResultMap")
        @Select(getOrderClassification)
        @QuerySupport
        List> getOrderClassification();
    
    }
    
    

    ps: 接口中定义为如下,会返回实体类所有的字段属性,

    ​ 而sql中未查询的字段,会默认为实体类中对应的字段赋值为null .

    List<WorkOrderClassification>getOrderClassification();
    

    ​ 应该设置以下此接口用map接收得到的sql查询数据

     List<Map<String,Object>> getOrderClassification();
    

    ​ 对应的在mapper的配置文件WorkOrderClassificationmapper.xml中 , 配置查询的返回值类型

    
    
    <mapper namespace="com.workorder.work_order.mapper.WorkOrderClassificationMapper">
    
        
        <resultMap id="ClassificationResult" type="com.workorder.work_order.pojo.WorkOrderClassification">
            <id column="id" property="id"/>
            <result column="node_id" property="nodeId"/>
            <result column="name" property="name"/>
            <result column="create_time" property="createTime"/>
           	<result column="update_time" property="updateTime"/>
        resultMap>
        
        <resultMap id="ClassificationResultMap" type="java.util.Map">
            <id column="id" property="id"/>
            <result column="node_id" property="nodeId"/>
            <result column="name" property="name"/>
    
        resultMap>
    
    mapper>
    
    

    最后在业务层写具体代码即可 , 定义一个 List>对象接收返回的SQL对象

    public Result getOrderClassification() {
     List<Map<String,Object>> list=workOrderClassificationMapper.getOrderClassification();
        if(list!=null){
            return Result.ok(list);
        }else{
            return Result.error(500,"查找失败");
        }
    
    }
    

你可能感兴趣的:(Mybatisplus)