完成插入后,主键会自动返回到对象里面
public boolean addBlog(Blog blog) { int id = blogMapper.insertSelective(blog); log.info("blogid: " + blog.getId()); return true; }
配置 <insert id="insertSelective" parameterType="com.manage.blog.domain.Blog" useGeneratedKeys="true" keyProperty="id">
注意:keyProperty的值id对应的是对象中的字段,不是数据库中的属性。如果blog对象中为blogId,则是keyProperty="blogId"
<insert id="insertSelective" parameterType="com.manage.blog.domain.Blog" useGeneratedKeys="true" keyProperty="id"> insert into t_blog <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null"> id, </if> <if test="title != null"> title, </if> <if test="summary != null"> summary, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null"> #{id,jdbcType=INTEGER}, </if> <if test="title != null"> #{title,jdbcType=VARCHAR}, </if> <if test="summary != null"> #{summary,jdbcType=VARCHAR}, </if> </trim> </insert>