baomidou / mybatis-plus

An powerful enhanced toolkit of MyBatis for simplify development
https://baomidou.com
Apache License 2.0
16.28k stars 4.29k forks source link

[错误报告]: 遇到的问题: postgresql使用lambdaUpdate().update(T entity) 生成SQL异常 #6460

Closed huage0524 closed 1 week ago

huage0524 commented 1 week ago

确认

当前程序版本

3.5.2

问题描述

已经使用3.5.5版本,也不行。找不到相关问题

具体代码如下: @Override public void testUpdateUser(int id, String address) { TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), User.class); boolean update = lambdaUpdate().eq(User::getId, id).set(User::getAddress, address).update(); System.out.println(update); }

@Override
public void testUpdateDomanUser(User i,User rs) {
    TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), User.class);
    boolean update = lambdaUpdate().eq(User::getId, i.getId()).set(User::getAddress,i.getAddress()).set(User::getAge,i.getAge()).update(rs);
    System.out.println(update);
}

@Test public void testupdate(){ int id = 5; String address = "湛江"; userService.testUpdateUser(id,address); queryUser(id); } @Test public void testUpdateUser(){ User i = new User(); i.setId(6); i.setAddress("test11111"); i.setAge(45); User user2 = new User(); user2.setId(6); user2.setAge(40); List rs = userService.queryUser(user2); System.out.println(rs.get(0)); userService.testUpdateDomanUser(i,rs.get(0)); queryUser(i.getId()); }

update()是正常的,当update(T entity)会出现异常。要set的字段,在SQL中会出现两次,一次是SQL对象获取到的值,一次是要更新的值

详细堆栈日志

JDBC Connection [org.postgresql.jdbc.PgConnection@50fb33a] will not be managed by Spring
==>  Preparing: SELECT id,user_name,password,name,age,email,address FROM tb_user WHERE (id = ?)
==> Parameters: 6(Integer)
<==    Columns: id, user_name, password, name, age, email, address
<==        Row: 6, user6, pass6, David Wilson, 40, david.wilson@example.com, 1313 Cedar Street
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6b2e46af]
User(id=6, userName=user6, password=pass6, name=David Wilson, age=40, email=david.wilson@example.com, address=1313 Cedar Street)
This primary key of "id" is primitive !不建议如此请使用包装类 in Class: "com.gbase.mpdemo.domain.User"
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4596f8f3] was not registered for synchronization because synchronization is not active
JDBC Connection [org.postgresql.jdbc.PgConnection@50fb33a] will not be managed by Spring
==>  Preparing: UPDATE tb_user SET user_name=?, password=?, name=?, age=?, email=?, address=?, address=?,age=? WHERE (id = ?)
==> Parameters: user6(String), pass6(String), David Wilson(String), 40(Integer), david.wilson@example.com(String), 1313 Cedar Street(String), test11111(String), 45(Integer), 6(Integer)
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4596f8f3]

org.springframework.jdbc.BadSqlGrammarException: 
### Error updating database.  Cause: org.postgresql.util.PSQLException: ERROR: multiple assignments to same column "address"
### The error may exist in com/gbase/mpdemo/mapper/UserMapper.java (best guess)
### The error may involve com.gbase.mpdemo.mapper.UserMapper.update-Inline
### The error occurred while setting parameters
### SQL: UPDATE tb_user  SET user_name=?, password=?, name=?, age=?, email=?, address=?,  address=?,age=?      WHERE (id = ?)
### Cause: org.postgresql.util.PSQLException: ERROR: multiple assignments to same column "address"
; bad SQL grammar []; nested exception is org.postgresql.util.PSQLException: ERROR: multiple assignments to same column "address"
huage0524 commented 1 week ago

mysql是正常的,postgres会异常

huage0524 commented 1 week ago
JDBC Connection [com.mysql.jdbc.JDBC4Connection@2a369e14] will not be managed by Spring
==>  Preparing: SELECT id,user_name,password,name,age,email,address FROM tb_user WHERE (id = ?)
==> Parameters: 6(Integer)
<==    Columns: id, user_name, password, name, age, email, address
<==        Row: 6, user6, pass6, David Wilson, 45, david.wilson@example.com, test11111
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5e1d03d7]
User(id=6, userName=user6, password=pass6, name=David Wilson, age=45, email=david.wilson@example.com, address=test11111)
This primary key of "id" is primitive !不建议如此请使用包装类 in Class: "com.gbase.mpdemo.domain.User"
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3c3c4a71] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mysql.jdbc.JDBC4Connection@2a369e14] will not be managed by Spring
==>  Preparing: UPDATE tb_user SET user_name=?, password=?, name=?, age=?, email=?, address=?, address=?,age=? WHERE (id = ?)
==> Parameters: user6(String), pass6(String), David Wilson(String), 45(Integer), david.wilson@example.com(String), test11111(String), CN GZ(String), 45(Integer), 6(Integer)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3c3c4a71]
true
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3d96fa9e] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mysql.jdbc.JDBC4Connection@2a369e14] will not be managed by Spring
==>  Preparing: SELECT id,user_name,password,name,age,email,address FROM tb_user WHERE (id = ?)
==> Parameters: 6(Integer)
<==    Columns: id, user_name, password, name, age, email, address
<==        Row: 6, user6, pass6, David Wilson, 45, david.wilson@example.com, CN GZ
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3d96fa9e]
User(id=6, userName=user6, password=pass6, name=David Wilson, age=45, email=david.wilson@example.com, address=CN GZ)

这是mysql的堆栈

miemieYaho commented 1 week ago

sql生成和数据库无关,wrapper手动set的字段就不要在entity里再次这个字段出现值

huage0524 commented 1 week ago

这里跟数据库有关,mysql支持重复绑定,postgres不支持。只能修改代码了吗?

miemieYaho commented 1 week ago

mp不会给你去重,自己改代码吧

huage0524 commented 1 week ago

好的,谢谢!