当向Mysql插入一条数据时,需要返回相应主键id
方法如下:
1.mybatis 配置文件如下:id为数据库自增长主键
<insert id="insert" <span style="background-color: rgb(255, 0, 0);">useGeneratedKeys="true" keyProperty="id"</span> parameterType="com.mfw.entity.PaperInfo" >
insert into ec_paper_info (ID, VERSION, PAPER_CODE,
PAPER_NAME, PAPER_MODE, QUESTION_COUNT,
PAPER_STATUS, REMARK, UNVALID_DATE,
CREATED_BY, USER_NAME, creation_date,
LAST_UPDATED_BY, LAST_UPDATED_DATE, enabled_flag,
times, year, paper_end_content,
paper_desc, batchId, template_id
)
values (#{id,jdbcType=INTEGER}, #{version,jdbcType=INTEGER}, #{paperCode,jdbcType=VARCHAR},
#{paperName,jdbcType=VARCHAR}, #{paperMode,jdbcType=VARCHAR}, #{questionCount,jdbcType=INTEGER},
#{paperStatus,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR}, #{unvalidDate,jdbcType=TIMESTAMP},
#{createdBy,jdbcType=VARCHAR}, #{userName,jdbcType=VARCHAR}, #{creationDate,jdbcType=TIMESTAMP},
#{lastUpdatedBy,jdbcType=VARCHAR}, #{lastUpdatedDate,jdbcType=TIMESTAMP}, #{enabledFlag,jdbcType=CHAR},
#{times,jdbcType=VARCHAR}, #{year,jdbcType=VARCHAR}, #{paperEndContent,jdbcType=VARCHAR},
#{paperDesc,jdbcType=VARCHAR}, #{batchid,jdbcType=INTEGER}, #{templateId,jdbcType=VARCHAR}
)
</insert>
2.dao如下
public int addPaper(PaperInfo paper) throws Exception{
return (Integer)dao.save("PaperInfoMapper.insert", paper);
}