mybatis 添加一条新数据并返回此数据的ID(主键)
利用Mybatis 的 selectKey来获得:方法一:<!--添加部门返回部门ID--><insertid="addDept"parameterType="com.demo.model.Department"keyProperty="id"><selectKeykeyProperty='id'resultType='int'order='AFTER'>sel
·
利用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 标签中的 keyProperty 和 selectKey标签块中的 LAET_INSERT_ID() ,另外 order属性 对于 oracl为 BEFORE; mysql为AFTER
方法三:
<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>
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)