织梦做视频网站,租远程服务器,聊城经济开发区人才网,全国公示信息查询平台mybatis配置获取自增主键
【/n】
01 使用场景
当需要刚刚插入数据库的数据对应的新增主键时#xff0c;通过配置xml文件#xff0c;使数据库返回新增主键id#xff0c;并把主键id与类参数对应
02 涉及配置
注解TableId(type IdType.AUTO)#xff1a;在类主键id通过配…mybatis配置获取自增主键
【/n】
01 使用场景
当需要刚刚插入数据库的数据对应的新增主键时通过配置xml文件使数据库返回新增主键id并把主键id与类参数对应
02 涉及配置
注解TableId(type IdType.AUTO)在类主键id通过配置实现插入数据库时主键自增
public class XzsQuestionsAnswerCreate {ApiModelProperty(value 选项内容,required true)NotBlank(message 选项内容不能为空)private String xzsOptionText;ApiModelProperty(value 选项标题,required true)NotBlank(message 选项标题不能为空)private String xzsOptionTitle;TableId(type IdType.AUTO)ApiModelProperty(标准答案id)private Long answerId;}mapper数据操作层正常插入接口
int saveQuestionAnswer(Param(questionId)Long questionId, Param(list)ListXzsQuestionsAnswerCreate XzsQuestionsAnswerCreate);
关键部分配置MySQL插入数据对应的返回值
insert idsaveQuestionAnswer useGeneratedKeystrue keyPropertylist.answerId keyColumnxzs_question_options_idINSERT INTO xzs_question_options (xzs_question_id,xzs_option_text,xzs_option_title)VALUESforeach collectionlist itemoption open( separator), ( close)#{questionId},#{option.xzsOptionText}, #{option.xzsOptionTitle}/foreach
/insert03 关键部分使用
字段作用useGeneratedKeyss“true”开启主键自增返回设置keyProperty类属性keyColumn数据库字段
keyProperty对应的是返回的自增主键对应的属性list中answerId属性会在插入语句后被赋值mapper层函数中的返回值int依旧是插入改变了多少行语句不是自增主键
int saveQuestionAnswer(Param(questionId)Long questionId, Param(list)ListXzsQuestionsAnswerCreate XzsQuestionsAnswerCreate);
keyColumn对应的是数据库中的字段