From 434b948b8aee497f8856622e00809d80fa852f53 Mon Sep 17 00:00:00 2001 From: tangxin <617054137@qq.com> Date: Wed, 3 Jan 2024 16:48:33 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix=20Db.updateEntitiesBatch=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E9=83=A8=E5=88=86=E5=AD=97=E6=AE=B5=E6=97=B6=E6=8A=A5?= =?UTF-8?q?=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/provider/RowSqlProvider.java | 8 +++-- .../java/com/mybatisflex/test/DbTest.java | 29 ++++++++++++------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/RowSqlProvider.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/RowSqlProvider.java index 6ea5246b..cdc23fec 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/RowSqlProvider.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/RowSqlProvider.java @@ -245,9 +245,13 @@ public class RowSqlProvider { Object entity = ProviderUtil.getEntity(params); FlexAssert.notNull(entity, "entity can not be null"); - + Class entityClass = entity.getClass(); + //如果是代理mybatis代理对象 + if(entity instanceof ProxyObject){ + entityClass = entityClass.getSuperclass(); + } // 该 Mapper 是通用 Mapper 无法通过 ProviderContext 获取,直接使用 TableInfoFactory - TableInfo tableInfo = TableInfoFactory.ofEntityClass(ClassUtil.getUsefulClass(entity.getClass())); + TableInfo tableInfo = TableInfoFactory.ofEntityClass(entityClass); // 执行 onUpdate 监听器 tableInfo.invokeOnUpdateListener(entity); diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/DbTest.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/DbTest.java index d2517e0e..ec8ffb83 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/DbTest.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/DbTest.java @@ -82,19 +82,28 @@ public class DbTest { Map map2 = Db.selectFirstAndSecondColumnsAsMap("select * from tb_account"); System.out.println(map); System.out.println(map2); + assert map.equals(map2); } @Test public void test03() { - Account account = UpdateEntity.of(Account.class,1); - account.setAge(1); - List 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); + try { + Account account = UpdateEntity.of(Account.class, 1); + account.setAge(1); + List 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); + Account account3 = new Account(); + account3.setId(3L); + account3.setAge(4); + accounts.add(account3); + Db.updateEntitiesBatch(accounts); + }catch (Exception e){ + assert false; + } } } From df11f52e3164145e9bb2f30ffa0f81f27d870099 Mon Sep 17 00:00:00 2001 From: tangxin <617054137@qq.com> Date: Thu, 4 Jan 2024 09:58:38 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix=20Db.updateEntitiesBatch=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E9=83=A8=E5=88=86=E5=AD=97=E6=AE=B5=E6=97=B6=E6=8A=A5?= =?UTF-8?q?=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mybatisflex/core/provider/RowSqlProvider.java | 10 +++------- .../main/java/com/mybatisflex/core/util/ClassUtil.java | 9 +++++---- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/RowSqlProvider.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/RowSqlProvider.java index cdc23fec..57c3dbef 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/RowSqlProvider.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/RowSqlProvider.java @@ -245,14 +245,10 @@ public class RowSqlProvider { Object entity = ProviderUtil.getEntity(params); FlexAssert.notNull(entity, "entity can not be null"); - Class entityClass = entity.getClass(); - //如果是代理mybatis代理对象 - if(entity instanceof ProxyObject){ - entityClass = entityClass.getSuperclass(); - } - // 该 Mapper 是通用 Mapper 无法通过 ProviderContext 获取,直接使用 TableInfoFactory - TableInfo tableInfo = TableInfoFactory.ofEntityClass(entityClass); + // 该 Mapper 是通用 Mapper 无法通过 ProviderContext 获取,直接使用 TableInfoFactory + + TableInfo tableInfo = TableInfoFactory.ofEntityClass(ClassUtil.getUsefulClass(entity.getClass())); // 执行 onUpdate 监听器 tableInfo.invokeOnUpdateListener(entity); diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/ClassUtil.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/ClassUtil.java index f2037541..e76c1e5e 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/ClassUtil.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/ClassUtil.java @@ -70,14 +70,15 @@ public class ClassUtil { } public static Class getUsefulClass(Class clazz) { + + if (ProxyObject.class.isAssignableFrom(clazz)) { + return (Class) clazz.getSuperclass(); + } + if (isProxy(clazz)) { return getJdkProxySuperClass(clazz); } -// if (ProxyObject.class.isAssignableFrom(clazz)){ -// return (Class) clazz.getSuperclass(); -// } - //ControllerTest$ServiceTest$$EnhancerByGuice$$40471411#hello -------> Guice //com.demo.blog.Blog$$EnhancerByCGLIB$$69a17158 ----> CGLIB //io.jboot.test.app.TestAppListener_$$_jvstb9f_0 ------> javassist