Mybatis insert返回主鍵ID

Mybatis insert語句書寫sql

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

useGeneratedKeys:是否生成主鍵spa

重點是寫上 keyProperty = "fileId" fileId對應 fileAlias實體類的屬性值code

public class TFileInfo extends BaseModel implements Serializable {

private Integer fileId;

private String fileName;

//保存入庫

fileInfoDao.insertSelective(fileInfo);

//返回入庫ID

id = fileInfo.getFileId();

上述sql執行完畢後,fileInfo對象裏的fileID就會被自動附加上fileId對象

相關文章
相關標籤/搜索