MyBatis insert返回主键不成功

##使用的环境

spring boot 1.5.9.RELEASE
mybatis 3.4.6
mysql 5.6

##项目结构
这里写图片描述

##部分配置文件

mybatis.mapper-locations = classpath:mapping/*.xml
mybatis.type-aliases-package = com.gf.model
mybatis.configuration.cache-enabled = true
mybatis.configuration.cacheEnabled = true
mybatis.configuration.useGeneratedKeys = true
mybatis.configuration.lazyLoadingEnabled = true

#model

package com.model;

import java.util.Date;

/**
 * @author 
 * @date 2018/4/2 10:03
 */
public class MessageLog {
    private Integer msgLogId;
    private Integer id;

    private String context;

    private String path;

    private Integer type;

    private Integer userId;


    private String remark;

    private Date createTime;

    public Integer getMsgLogId() {
        return msgLogId;
    }

    public void setMsgLogId(Integer msgLogId) {
        this.msgLogId = msgLogId;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
    ....省略

#serve

package com.service;

import com.mapper.MessageLogMapper;
import com.model.MessageLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 
 * @date 2018/4/2.
 */
@Service
public class MessageLogService {

    @Autowired
    private MessageLogMapper messageLogMapper;

    /**
     * @author 
     * @date 2018/4/2 14:52
     * 插入一条记录 返回主键
     * @param context
     * @param path
     * @param type
     * @param userId
     * @return
     */
    public Integer messageLogSave(
            String context
            , String path
            , Integer type
            , Integer userId
    ) {
        MessageLog messageLog = new MessageLog();
        messageLog.setType(type);
        if(type>0){
            messageLog.setContext(context);
        }else {
            messageLog.setPath(path);
        }
        messageLog.setUserId(userId);
        int count = messageLogMapper.insertSelectiveRecord(messageLog);
        return messageLog.getId();
    }

}

#Mapper

package com.mapper;

import com.model.MessageLog;
import org.springframework.stereotype.Repository;

/**
 * @author 
 * @date 2018/4/2 14:12
 *
 */
@Repository
public interface MessageLogMapper {
    int deleteByPrimaryKey(Integer id);

    int insert(MessageLog record);

    int insertSelective(MessageLog record);

    Integer insertSelectiveRecord(MessageLog record);

    MessageLog selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(MessageLog record);

    int updateByPrimaryKey(MessageLog record);
}

#部分MessageLogMapper.xml

<insert id="insertSelectiveRecord" useGeneratedKeys="true" keyProperty="id" parameterType="com.gf.model.MessageLog"   >
    insert into message_log
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="context != null">
        context,
      </if>
      <if test="path != null">
        path,
      </if>
      <if test="type != null">
        type,
      </if>
      <if test="userId != null">
        user_id,
      </if>
      <if test="remark != null">
        remark,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="context != null">
        #{context,jdbcType=VARCHAR},
      </if>
      <if test="path != null">
        #{path,jdbcType=VARCHAR},
      </if>
      <if test="type != null">
        #{type,jdbcType=INTEGER},
      </if>
      <if test="userId != null">
        #{userId,jdbcType=INTEGER},
      </if>
      <if test="remark != null">
        #{remark,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>

#说明
出现问题可能不知道mybatis 返回字段会直接映射到我们的模型里面
这里写图片描述
仅仅记录自己出现的问题

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值