mybatis 添加一条新数据并返回此数据的ID(主键)
mybatis 添加一条新数据并返回此数据的ID(主键)
利用Mybatis 的 selectKey来获得:
- <!-- 添加部门 返回部门ID -->
- <insert id="addDept" parameterType="com.demo.model.Department" keyProperty="id">
- <selectKey keyProperty='id' resultType='int' order='AFTER' >
- select LAST_INSERT_ID();
- </selectKey>
- insert into department(<include refid="departmentAllField"/>)
- values(#{departmentId},#{departmentName},#{departmentManagerName},#{companyId});
- </insert>
或
- <insert id="addDept" parameterType="com.demo.model.Department" useGeneratedKeys="true" keyProperty="id">
- insert into department(<include refid="departmentAllField"/>)
- values(#{departmentId},#{departmentName},#{departmentManagerName},#{companyId});
- </insert>
方法三:
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.edu.archive.business.domain.grade.pojo.model.ApplicationRecord" useGeneratedKeys="true">
insert into bj_application_record (parent_id, class_no, student_name,
kinsfolk_relation, `status`, refuse_remark,
create_time, update_time, version
)
values (#{parentId,jdbcType=INTEGER}, #{classNo,jdbcType=VARCHAR}, #{studentName,jdbcType=VARCHAR},
#{kinsfolkRelation,jdbcType=VARCHAR}, #{status,jdbcType=TINYINT}, #{refuseRemark,jdbcType=VARCHAR},
#{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, #{version,jdbcType=INTEGER}
)
</insert>