BaseMapper 是 MyBatis 的一个接口,用于插入一条记录。该接口的

insert

方法返回值是 int 类型,表示插入记录的数量。如果插入成功,则返回 1;否则返回 0。

例如:

int result = userMapper.insert(user);if (result == 1) {
    // 插入成功
} else {
    // 插入失败
}

注意:在 MyBatis 中,如果数据库表中有自增主键,那么插入成功后,自增主键的值会被设置到插入的对象中。如果需要获取自增主键的值,可以使用

useGeneratedKeys



keyProperty

属性。

例如:

<insert id="insert" parameterType="User" useGeneratedKeys="true" keyProperty="id">
  INSERT INTO user(name, age) VALUES (#{name}, #{age})
</insert>



版权声明:本文为weixin_35755640原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_35755640/article/details/128874166