##使用的环境
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 返回字段会直接映射到我们的模型里面
仅仅记录自己出现的问题