一:useGeneratedKeys
useGeneratedKeys 取值范围true|false 默认值是:false。 含义:设置是否使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。
<insert id="***" parameterType="***.***.***.***.***Entity" useGeneratedKeys="true" keyProperty="id">
</insert>会将自动生成的id插入到入参中,执行完之后提取入参实体中的id即可二:selectKey
跟第一种方法一样从入参实体中直接取出id即可
<insert id="***" parameterType="***.***.***.***.testTableEntity">
<selectKey keyProperty="tab_id" keyColumn="id" resultType="int">
select LASTVAL() as id
</selectKey>
INSERT INTO
test_table(
id
)VALUES(
#{id}
)
</insert>
本文介绍MyBatis中两种常用的主键生成策略:useGeneratedKeys和selectKey。useGeneratedKeys通过设置useGeneratedKeys为true,并指定keyProperty来获取自动生成的主键。selectKey则通过执行SQL获取最后生成的ID并赋值给指定属性。
935

被折叠的 条评论
为什么被折叠?



