Mybatis insert返回主键ID

Mybatis insert语句书写

1 <insert id="insertSelective" useGeneratedKeys="true" keyProperty="fileId" parameterType="fileAlias" >
2 
3     insert into t_file_info ...
4 
5 </insert>

useGeneratedKeys:是否生成主键

重点是写上 keyProperty = "fileId" fileId对应 fileAlias实体类的属性值

public class TFileInfo extends BaseModel implements Serializable {

private Integer fileId;

private String fileName;

//保存入库

fileInfoDao.insertSelective(fileInfo);

//返回入库ID

id = fileInfo.getFileId();

上述sql执行完毕后,fileInfo对象里的fileID就会被自动附加上fileId

posted @ 2014-04-04 15:04  踏刃而起  阅读(601)  评论(0编辑  收藏  举报