若依整合Mybatis-Plus遇到一个奇怪的问题

人间五十年
  • 109

问题背景

参考官方文档教程整合了MybatisPlus后,项目能正常启动并基础代码能正常运行。
但是以下代码存在问题:

DailyDoorInoutChangeType dailyDoorInoutChangeType = dailyDoorInoutChangeTypeService
                .lambdaQuery()
                .eq(DailyDoorInoutChangeType::getChangeTypeName, changeTypeName)
                .one();

表的DDL语句

CREATE TABLE `daily_door_inout_change_type` (
  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增ID',
  `change_type_name` varchar(32) COLLATE utf8mb4_general_ci NOT NULL COMMENT '变动类型名称',
  `change_type_color` char(7) COLLATE utf8mb4_general_ci NOT NULL COMMENT '变动类型显示颜色',
  `create_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '创建者',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '更新者',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',
  `remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '备注',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='日常管理-出入变动类型管理表

运行异常结果截图

image.png
SQL中出现了一个未定义的字段search_value,导致程序运行异常。

详细代码

DailyDoorInoutChangeTypeMapper.java

package cc.hugesoft.jiefang.mapper;

import java.util.List;
import cc.hugesoft.jiefang.domain.DailyDoorInoutChangeType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

/**
 * 日常管理-出入变动类型管理Mapper接口
 * 
 * @author ruoyi
 * @date 2021-05-14
 */
public interface DailyDoorInoutChangeTypeMapper  extends BaseMapper<DailyDoorInoutChangeType>
{
    /**
     * 查询日常管理-出入变动类型管理
     * 
     * @param id 日常管理-出入变动类型管理ID
     * @return 日常管理-出入变动类型管理
     */
    public DailyDoorInoutChangeType selectDailyDoorInoutChangeTypeById(Integer id);

    /**
     * 查询日常管理-出入变动类型管理列表
     * 
     * @param dailyDoorInoutChangeType 日常管理-出入变动类型管理
     * @return 日常管理-出入变动类型管理集合
     */
    public List<DailyDoorInoutChangeType> selectDailyDoorInoutChangeTypeList(DailyDoorInoutChangeType dailyDoorInoutChangeType);

    /**
     * 新增日常管理-出入变动类型管理
     * 
     * @param dailyDoorInoutChangeType 日常管理-出入变动类型管理
     * @return 结果
     */
    public int insertDailyDoorInoutChangeType(DailyDoorInoutChangeType dailyDoorInoutChangeType);

    /**
     * 修改日常管理-出入变动类型管理
     * 
     * @param dailyDoorInoutChangeType 日常管理-出入变动类型管理
     * @return 结果
     */
    public int updateDailyDoorInoutChangeType(DailyDoorInoutChangeType dailyDoorInoutChangeType);

    /**
     * 删除日常管理-出入变动类型管理
     * 
     * @param id 日常管理-出入变动类型管理ID
     * @return 结果
     */
    public int deleteDailyDoorInoutChangeTypeById(Integer id);

    /**
     * 批量删除日常管理-出入变动类型管理
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    public int deleteDailyDoorInoutChangeTypeByIds(String[] ids);
}

DailyDoorInoutChangeTypeMapper.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="cc.hugesoft.jiefang.mapper.DailyDoorInoutChangeTypeMapper">
    
    <resultMap type="DailyDoorInoutChangeType" id="DailyDoorInoutChangeTypeResult">
        <result property="id"    column="id"    />
        <result property="changeTypeName"    column="change_type_name"    />
        <result property="changeTypeColor"    column="change_type_color"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectDailyDoorInoutChangeTypeVo">
        select id, change_type_name, change_type_color, create_by, create_time, update_by, update_time, remark from daily_door_inout_change_type
    </sql>

    <select id="selectDailyDoorInoutChangeTypeList" parameterType="DailyDoorInoutChangeType" resultMap="DailyDoorInoutChangeTypeResult">
        <include refid="selectDailyDoorInoutChangeTypeVo"/>
        <where>  
            <if test="changeTypeName != null  and changeTypeName != ''"> and change_type_name like concat('%', #{changeTypeName}, '%')</if>
            <if test="changeTypeColor != null  and changeTypeColor != ''"> and change_type_color = #{changeTypeColor}</if>
        </where>
    </select>
    
    <select id="selectDailyDoorInoutChangeTypeById" parameterType="Integer" resultMap="DailyDoorInoutChangeTypeResult">
        <include refid="selectDailyDoorInoutChangeTypeVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertDailyDoorInoutChangeType" parameterType="DailyDoorInoutChangeType" useGeneratedKeys="true" keyProperty="id">
        insert into daily_door_inout_change_type
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="changeTypeName != null and changeTypeName != ''">change_type_name,</if>
            <if test="changeTypeColor != null and changeTypeColor != ''">change_type_color,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null and updateBy != ''">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null and remark != ''">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="changeTypeName != null and changeTypeName != ''">#{changeTypeName},</if>
            <if test="changeTypeColor != null and changeTypeColor != ''">#{changeTypeColor},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null and remark != ''">#{remark},</if>
         </trim>
    </insert>

    <update id="updateDailyDoorInoutChangeType" parameterType="DailyDoorInoutChangeType">
        update daily_door_inout_change_type
        <trim prefix="SET" suffixOverrides=",">
            <if test="changeTypeName != null and changeTypeName != ''">change_type_name = #{changeTypeName},</if>
            <if test="changeTypeColor != null and changeTypeColor != ''">change_type_color = #{changeTypeColor},</if>
            <if test="createBy != null and createBy != ''">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null and remark != ''">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDailyDoorInoutChangeTypeById" parameterType="Integer">
        delete from daily_door_inout_change_type where id = #{id}
    </delete>

    <delete id="deleteDailyDoorInoutChangeTypeByIds" parameterType="String">
        delete from daily_door_inout_change_type where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>

IDailyDoorInoutChangeTypeService.java

package cc.hugesoft.jiefang.service;

import java.util.List;
import cc.hugesoft.jiefang.domain.DailyDoorInoutChangeType;
import com.baomidou.mybatisplus.extension.service.IService;

/**
 * 日常管理-出入变动类型管理Service接口
 * 
 * @author ruoyi
 * @date 2021-05-14
 */
public interface IDailyDoorInoutChangeTypeService extends IService<DailyDoorInoutChangeType>
{
    /**
     * 查询日常管理-出入变动类型管理
     * 
     * @param id 日常管理-出入变动类型管理ID
     * @return 日常管理-出入变动类型管理
     */
    public DailyDoorInoutChangeType selectDailyDoorInoutChangeTypeById(Integer id);

    /**
     * 查询日常管理-出入变动类型管理列表
     * 
     * @param dailyDoorInoutChangeType 日常管理-出入变动类型管理
     * @return 日常管理-出入变动类型管理集合
     */
    public List<DailyDoorInoutChangeType> selectDailyDoorInoutChangeTypeList(DailyDoorInoutChangeType dailyDoorInoutChangeType);

    /**
     * 新增日常管理-出入变动类型管理
     * 
     * @param dailyDoorInoutChangeType 日常管理-出入变动类型管理
     * @return 结果
     */
    public int insertDailyDoorInoutChangeType(DailyDoorInoutChangeType dailyDoorInoutChangeType);

    /**
     * 修改日常管理-出入变动类型管理
     * 
     * @param dailyDoorInoutChangeType 日常管理-出入变动类型管理
     * @return 结果
     */
    public int updateDailyDoorInoutChangeType(DailyDoorInoutChangeType dailyDoorInoutChangeType);

    /**
     * 批量删除日常管理-出入变动类型管理
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    public int deleteDailyDoorInoutChangeTypeByIds(String ids);

    /**
     * 删除日常管理-出入变动类型管理信息
     * 
     * @param id 日常管理-出入变动类型管理ID
     * @return 结果
     */
    public int deleteDailyDoorInoutChangeTypeById(Integer id);
}

DailyDoorInoutChangeTypeServiceImpl.java

package cc.hugesoft.jiefang.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cc.hugesoft.jiefang.mapper.DailyDoorInoutChangeTypeMapper;
import cc.hugesoft.jiefang.domain.DailyDoorInoutChangeType;
import cc.hugesoft.jiefang.service.IDailyDoorInoutChangeTypeService;
import com.ruoyi.common.core.text.Convert;

/**
 * 日常管理-出入变动类型管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-05-14
 */
@Service
public class DailyDoorInoutChangeTypeServiceImpl extends ServiceImpl<DailyDoorInoutChangeTypeMapper, DailyDoorInoutChangeType> implements IDailyDoorInoutChangeTypeService
{
    @Autowired
    private DailyDoorInoutChangeTypeMapper dailyDoorInoutChangeTypeMapper;
    
    // ....省略具体实现成员方法代码
}
回复
阅读 434
3 个回答

表中没有search_value字段,使用@TableField(exist=false)排除

实体类属性名称与数据库不相同的情况下需要使用:@TableField注明,

 @TableField(value = "search_value")

eg: private String searchVal;

解决方案

没有注意到若依的BaseEntity中有两个非数据库字段的定义,加上@TableField(exist = false)注明即可解决问题:

BaseEntity.java

package com.ruoyi.common.core.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;

/**
 * Entity基类
 * 
 * @author ruoyi
 */
public class BaseEntity implements Serializable
{
    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    /** 搜索值 */
    @TableField(exist = false)
    private String searchValue;

    /** 创建者 */
    private String createBy;

    /** 创建时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    /** 更新者 */
    private String updateBy;

    /** 更新时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    /** 备注 */
    private String remark;

    /** 请求参数 */
    @TableField(exist = false)
    private Map<String, Object> params;

    public String getSearchValue()
    {
        return searchValue;
    }

    public void setSearchValue(String searchValue)
    {
        this.searchValue = searchValue;
    }

    public String getCreateBy()
    {
        return createBy;
    }

    public void setCreateBy(String createBy)
    {
        this.createBy = createBy;
    }

    public Date getCreateTime()
    {
        return createTime;
    }

    public void setCreateTime(Date createTime)
    {
        this.createTime = createTime;
    }

    public String getUpdateBy()
    {
        return updateBy;
    }

    public void setUpdateBy(String updateBy)
    {
        this.updateBy = updateBy;
    }

    public Date getUpdateTime()
    {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime)
    {
        this.updateTime = updateTime;
    }

    public String getRemark()
    {
        return remark;
    }

    public void setRemark(String remark)
    {
        this.remark = remark;
    }

    public Map<String, Object> getParams()
    {
        if (params == null)
        {
            params = new HashMap<>();
        }
        return params;
    }

    public void setParams(Map<String, Object> params)
    {
        this.params = params;
    }
}
你知道吗?

宣传栏