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

MybatisPlus

程序员文章站 2024-03-05 23:05:19
...

1. MybatisPlus

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

        <!--mybatis-plus-->
        <!--mybatis-plus与spring-boot的集成包,不用再导入mybatis和mybatis-spring的包-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>
        <!--代码生成器-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
        </dependency>

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<WorkOrderClassification> {
    
        /**
         * 获取数据列表
         * @param
         * @return
         */
    
        String  getOrderClassification= "SELECT ID,NAME ,NODE_ID FROM work_order_classification";
    
        @ResultMap("ClassificationResultMap")
        @Select(getOrderClassification)
        @QuerySupport
        List<Map<String,Object>> getOrderClassification();
    
    }
    
    

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

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

    List<WorkOrderClassification>getOrderClassification();
    

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

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

    ​ 对应的在mapper的配置文件WorkOrderClassificationmapper.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.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<Map<String,Object>>对象接收返回的SQL对象

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