mybatis-plus连接pgsql,插入数据报错且不能再次添加一条数据

1、问题显示:

今天扫描二维码添加数据的时候,发现添加不了数据了,打开数据库发现自增长的ID断层的情况。(自己的数据库已经处理了,下面截图为网友的数据库截图)

少了ID为5和6的数据,经过确认,是postgresql对高并发处理机制导致的,

在pg中,每次新增数据,都会将一个ID锁定到这条数据上,如果这条数据因为某些原因插入失败(主键重复等),那么这个ID也不会再赋予新的数据,因此出现ID跳过的现象,官方解释:

If transactions rollback, the serial value assigned during the rolled
back transaction is skipped. This has been discussed many times, it's
a tradeoff between losing some #s now and again and taking a huge
performance and code complexity hit to avoid it.
 
If you absolutely need consecutive #s, then serial is not for you and
you should implement your own method of acquiring sequential numbers.

2、解决方式:自己试了网上几种方式,发现下面这种能处理,查询添加这个sql。但是有个缺点:偶尔也添加不了,但是多试几次就可以了。不会出现一直新增不了数据的情况。如果网友有其他更好的方法,请留言讨论。(col_store_factory_id_seq)为序列,(col_store_factory)为表名。

SELECT setval('col_store_factory_id_seq', (SELECT MAX(id) FROM col_store_factory));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Mybatis-Plus查询一条数据可以通过以下步骤实现: 1. 导入Mybatis-Plus的依赖包。 2. 创建一个实体类,用于映射数据库中的表。 3. 创建一个Mapper接口,继承BaseMapper接口,并指定泛型为实体类。 4. 在Mapper接口中定义一个查询方法,使用@Select注解,并指定SQL语句。 5. 在Service层中调用Mapper接口的查询方法,获取查询结果。 6. 在Controller层中将查询结果返回给前端。 示例代码如下: 1. 导入依赖包 ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.1</version> </dependency> ``` 2. 创建实体类 ```java @Data public class User { private Long id; private String name; private Integer age; private String email; } ``` 3. 创建Mapper接口 ```java public interface UserMapper extends BaseMapper<User> { @Select("SELECT * FROM user WHERE id = #{id}") User selectById(Long id); } ``` 4. 在Service层中调用Mapper接口的查询方法 ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User getUserById(Long id) { return userMapper.selectById(id); } } ``` 5. 在Controller层中将查询结果返回给前端 ```java @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/{id}") public User getUserById(@PathVariable Long id) { return userService.getUserById(id); } } ``` 以上就是使用Mybatis-Plus查询一条数据的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值