MyBatis insert返回主键不成功

##使用的环境

spring boot 1.5.9.RELEASE
mybatis 3.4.6
mysql 5.6

##项目结构
MyBatis insert返回主键不成功_第1张图片

##部分配置文件

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 into message_log
    
      
        id,
      
      
        context,
      
      
        path,
      
      
        type,
      
      
        user_id,
      
      
        remark,
      
      
        create_time,
      
    
    
      
        #{id,jdbcType=INTEGER},
      
      
        #{context,jdbcType=VARCHAR},
      
      
        #{path,jdbcType=VARCHAR},
      
      
        #{type,jdbcType=INTEGER},
      
      
        #{userId,jdbcType=INTEGER},
      
      
        #{remark,jdbcType=VARCHAR},
      
      
        #{createTime,jdbcType=TIMESTAMP},
      
    
  

#说明
出现问题可能不知道mybatis 返回字段会直接映射到我们的模型里面
MyBatis insert返回主键不成功_第2张图片
仅仅记录自己出现的问题

你可能感兴趣的:(java)