fix Db.updateEntitiesBatch更新部分字段时报错的问题

This commit is contained in:
tangxin 2023-12-21 00:15:59 +08:00
parent f14ebbf764
commit 23a55bfdca
2 changed files with 25 additions and 3 deletions

View File

@ -26,6 +26,7 @@ import com.mybatisflex.core.row.RowMapper;
import com.mybatisflex.core.table.TableInfo; import com.mybatisflex.core.table.TableInfo;
import com.mybatisflex.core.table.TableInfoFactory; import com.mybatisflex.core.table.TableInfoFactory;
import com.mybatisflex.core.util.ArrayUtil; import com.mybatisflex.core.util.ArrayUtil;
import org.apache.ibatis.javassist.util.proxy.ProxyObject;
import java.util.*; import java.util.*;
@ -244,8 +245,13 @@ public class RowSqlProvider {
FlexAssert.notNull(entity, "entity can not be null"); FlexAssert.notNull(entity, "entity can not be null");
Class<?> entityClass = entity.getClass();
//如果是代理mybatis代理对象
if(entity instanceof ProxyObject){
entityClass = entityClass.getSuperclass();
}
// Mapper 是通用 Mapper 无法通过 ProviderContext 获取直接使用 TableInfoFactory // Mapper 是通用 Mapper 无法通过 ProviderContext 获取直接使用 TableInfoFactory
TableInfo tableInfo = TableInfoFactory.ofEntityClass(entity.getClass()); TableInfo tableInfo = TableInfoFactory.ofEntityClass(entityClass);
// 执行 onUpdate 监听器 // 执行 onUpdate 监听器
tableInfo.invokeOnUpdateListener(entity); tableInfo.invokeOnUpdateListener(entity);

View File

@ -17,9 +17,12 @@
package com.mybatisflex.test; package com.mybatisflex.test;
import com.mybatisflex.core.MybatisFlexBootstrap; import com.mybatisflex.core.MybatisFlexBootstrap;
import com.mybatisflex.core.audit.AuditManager;
import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Db; import com.mybatisflex.core.row.Db;
import com.mybatisflex.core.row.Row; import com.mybatisflex.core.row.Row;
import com.mybatisflex.core.update.UpdateWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.Configuration;
import org.junit.Assert; import org.junit.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;
@ -28,6 +31,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -78,7 +82,19 @@ public class DbTest {
Map map2 = Db.selectFirstAndSecondColumnsAsMap("select * from tb_account"); Map map2 = Db.selectFirstAndSecondColumnsAsMap("select * from tb_account");
System.out.println(map); System.out.println(map);
System.out.println(map2); System.out.println(map2);
Assert.assertEquals(map,map2);
}
} }
@Test
public void test03() {
Account account = UpdateEntity.of(Account.class,1);
account.setAge(1);
List<Account> accounts = new ArrayList<>();
accounts.add(account);
Account account2 = UpdateEntity.of(Account.class,2);
account2.setAge(2);
UpdateWrapper updateWrapper = UpdateWrapper.of(account2);
updateWrapper.setRaw("age","age+1");
accounts.add(account2);
Db.updateEntitiesBatch(accounts);
}
}