org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘MP_OPTLOCK_VERSION_ORIGINAL’ not found. Available parameters are [param1, et]
遇到这个异常 只有2种情况
- 拦截器弄错了
- version没有值, 如果是new出来的对象,没有给version值, 他是不知道当前版本号是多少的,没办法让版本号+1 所以报错
- 我遇到的问题是…以上都没错,就是给我报这个错…后面debug运行一次后,又正常了…玄学~
实体类
@Version
private Integer version;
拦截器
package com.itheima.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return mybatisPlusInterceptor;
}
}
测试类
@Test
public void testUpdateTwo() {
//先通过要修改的数据id将当前数据查询出来
User user1 = userMapper.selectById(12L); //version=2
User user2 = userMapper.selectById(12L); //version=2
user1.setName("Jack aaa");
userMapper.updateById(user1); //version=>3
user2.setName("Jack bbb");
userMapper.updateById(user2); //verion=2 更新失败
}
执行日志
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@50f4b83d] will not be managed by Spring
==> Preparing: SELECT id,deleted,name,gender,age,tel,version FROM user WHERE id=? AND deleted=0
==> Parameters: 12(Long)
<== Columns: id, deleted, name, gender, age, tel, version
<== Row: 12, 0, Jack aaa, 女, 22, 12345678910, 2
<== Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@375084c9]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@53bb71e5] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@50f4b83d] will not be managed by Spring
==> Preparing: SELECT id,deleted,name,gender,age,tel,version FROM user WHERE id=? AND deleted=0
==> Parameters: 12(Long)
<== Columns: id, deleted, name, gender, age, tel, version
<== Row: 12, 0, Jack aaa, 女, 22, 12345678910, 2
<== Total: 1
Closing non transactional SqlSession
[org.apache.ibatis.session.defaults.DefaultSqlSession@53bb71e5]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@13aed42b] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@50f4b83d] will not be managed by Spring
==> Preparing: UPDATE user SET name=?, gender=?, age=?, tel=?, version=? WHERE id=? AND version=? AND deleted=0
==> Parameters: Jack ccc(String), 女(String), 22(Integer), 12345678910(String), 3(Integer), 12(Long), 2(Integer)
<== Updates: 1
Closing non transactional SqlSession
[org.apache.ibatis.session.defaults.DefaultSqlSession@13aed42b]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1de7f898] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@50f4b83d] will not be managed by Spring
==> Preparing: UPDATE user SET name=?, gender=?, age=?, tel=?, version=? WHERE id=? AND version=? AND deleted=0
==> Parameters: Jack ddd(String), 女(String), 22(Integer), 12345678910(String), 3(Integer), 12(Long), 2(Integer)
<== Updates: 0
Closing non transactional SqlSession