From 9c204fbcb0c4540b5363eecf17aeeff546ba5e02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Tue, 17 Oct 2023 19:51:57 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=9C=A8=20insert=20=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E8=8B=A5=20entity=20=E6=9C=89=E4=B8=BB=E9=94=AE=EF=BC=8C?= =?UTF-8?q?=E5=88=99=E7=9B=B4=E6=8E=A5=E4=BD=BF=E7=94=A8=20entity=20?= =?UTF-8?q?=E7=9A=84=E4=B8=BB=E9=94=AE=EF=BC=8C=E4=B8=8D=E5=86=8D=E9=80=9A?= =?UTF-8?q?=E8=BF=87=E4=B8=BB=E9=94=AE=E7=94=9F=E6=88=90=E5=99=A8=E6=9D=A5?= =?UTF-8?q?=E7=94=9F=E6=88=90=EF=BC=9B=20close=20#I88TX1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/keygen/CustomKeyGenerator.java | 11 ++- .../core/keygen/RowCustomKeyGenerator.java | 7 +- .../mybatisflex/core/table/ColumnInfo.java | 2 +- .../com/mybatisflex/core/table/TableInfo.java | 7 +- .../java/com/mybatisflex/test/Account7.java | 77 +++++++++++++++++++ .../com/mybatisflex/test/AccountTester.java | 44 +++++++++-- .../mybatisflex/test/TestKeyGenerator.java | 10 +++ 7 files changed, 144 insertions(+), 14 deletions(-) create mode 100644 mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account7.java create mode 100644 mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/TestKeyGenerator.java diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/keygen/CustomKeyGenerator.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/keygen/CustomKeyGenerator.java index 5079f6b0..1cdd00ab 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/keygen/CustomKeyGenerator.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/keygen/CustomKeyGenerator.java @@ -69,10 +69,15 @@ public class CustomKeyGenerator implements KeyGenerator { @Override public void processBefore(Executor executor, MappedStatement ms, Statement stmt, Object parameter) { Object entity = ((Map) parameter).get(FlexConsts.ENTITY); - Configuration configuration = ms.getConfiguration(); - MetaObject metaParam = configuration.newMetaObject(parameter); - Object generateId = keyGenerator.generate(entity, idInfo.getColumn()); try { + Object existId = tableInfo.getValue(entity, idInfo.getProperty()); + // 若用户主动设置了主键,则使用用户自己设置的主键,不再生成主键 + if (existId != null){ + return; + } + Configuration configuration = ms.getConfiguration(); + MetaObject metaParam = configuration.newMetaObject(parameter); + Object generateId = keyGenerator.generate(entity, idInfo.getColumn()); MetaObject metaObjectForProperty = metaParam.metaObjectForProperty(FlexConsts.ENTITY); Invoker setInvoker = tableInfo.getReflector().getSetInvoker(idInfo.getProperty()); Object id = ConvertUtil.convert(generateId, setInvoker.getType()); diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/keygen/RowCustomKeyGenerator.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/keygen/RowCustomKeyGenerator.java index 47e29bf5..125ae042 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/keygen/RowCustomKeyGenerator.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/keygen/RowCustomKeyGenerator.java @@ -55,8 +55,13 @@ public class RowCustomKeyGenerator implements KeyGenerator { @Override public void processBefore(Executor executor, MappedStatement ms, Statement stmt, Object parameter) { Row row = (Row) ((Map) parameter).get(FlexConsts.ROW); - Object generateId = keyGenerator.generate(row, rowKey.getKeyColumn()); try { + Object existId = row.get(rowKey.getKeyColumn()); + // 若用户主动设置了主键,则使用用户自己设置的主键,不再生成主键 + if (existId != null) { + return; + } + Object generateId = keyGenerator.generate(row, rowKey.getKeyColumn()); row.put(rowKey.getKeyColumn(), generateId); } catch (Exception e) { throw FlexExceptions.wrap(e); diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/ColumnInfo.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/ColumnInfo.java index 41d3ae02..7d8f021b 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/ColumnInfo.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/ColumnInfo.java @@ -35,7 +35,7 @@ public class ColumnInfo { protected String[] alias; /** - * java entity 定义的属性名称。 + * java entity 定义的属性名称(field name)。 */ protected String property; diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java index 4379db9a..6f18d52a 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java @@ -718,6 +718,10 @@ public class TableInfo { return values; } + public Object getValue(Object entity, String property) { + FieldWrapper fieldWrapper = FieldWrapper.of(entityClass, property); + return fieldWrapper.get(entity); + } /** * 获取主键值 @@ -1119,7 +1123,6 @@ public class TableInfo { private Object buildColumnSqlArg(MetaObject metaObject, String column) { ColumnInfo columnInfo = columnInfoMapping.get(column); Object value = getPropertyValue(metaObject, columnInfo.property); - if (value != null) { TypeHandler typeHandler = columnInfo.buildTypeHandler(null); if (typeHandler != null) { @@ -1137,7 +1140,7 @@ public class TableInfo { } - private Object getPropertyValue(MetaObject metaObject, String property) { + public Object getPropertyValue(MetaObject metaObject, String property) { if (property != null && metaObject.hasGetter(property)) { return metaObject.getValue(property); } diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account7.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account7.java new file mode 100644 index 00000000..16878c2c --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account7.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.mybatisflex.test; + +import com.mybatisflex.annotation.ColumnMask; +import com.mybatisflex.annotation.Id; +import com.mybatisflex.annotation.KeyType; +import com.mybatisflex.annotation.Table; +import com.mybatisflex.core.mask.Masks; + +import java.io.Serializable; + +@Table(value = "tb_account") +public class Account7 extends BaseEntity implements Serializable, AgeAware { + + private static final long serialVersionUID = 1L; + + @Id(keyType = KeyType.Generator, value = "test") + private Long id; + + @ColumnMask(Masks.CHINESE_NAME) + private String userName; + + private int age; + + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + @Override + public int getAge() { + return age; + } + + @Override + public void setAge(int age) { + this.age = age; + } + + + @Override + public String toString() { + return "Account{" + + "id=" + id + + ", userName='" + userName + '\'' + + ", age=" + age + + '}'; + } + +} diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AccountTester.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AccountTester.java index d25c954f..1f76de7d 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AccountTester.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AccountTester.java @@ -20,6 +20,7 @@ import com.mybatisflex.core.MybatisFlexBootstrap; import com.mybatisflex.core.audit.AuditManager; import com.mybatisflex.core.audit.ConsoleMessageCollector; import com.mybatisflex.core.audit.MessageCollector; +import com.mybatisflex.core.keygen.KeyGeneratorFactory; import com.mybatisflex.core.mybatis.Mappers; import com.mybatisflex.core.query.If; import com.mybatisflex.core.query.QueryWrapper; @@ -28,6 +29,7 @@ import com.mybatisflex.core.update.UpdateChain; import com.mybatisflex.core.update.UpdateWrapper; import com.mybatisflex.core.util.UpdateEntity; import com.mybatisflex.mapper.Account6Mapper; +import com.mybatisflex.mapper.Account7Mapper; import com.mybatisflex.mapper.ArticleMapper; import org.apache.ibatis.logging.stdout.StdOutImpl; import org.junit.Assert; @@ -59,11 +61,14 @@ public class AccountTester { FlexGlobalConfig.getDefaultConfig() .setLogicDeleteColumn("is_delete"); + KeyGeneratorFactory.register("test", new TestKeyGenerator()); + MybatisFlexBootstrap bootstrap = MybatisFlexBootstrap.getInstance() .setDataSource(dataSource) .setLogImpl(StdOutImpl.class) .addMapper(AccountMapper.class) .addMapper(Account6Mapper.class) + .addMapper(Account7Mapper.class) .addMapper(ArticleMapper.class) .start(); @@ -151,7 +156,7 @@ public class AccountTester { public void testLeftJoinSelectWithIgnoreColumn() { QueryWrapper queryWrapper = QueryWrapper.create(); queryWrapper - .select(ACCOUNT.ID,ACCOUNT.AGE,ARTICLE.TITLE) + .select(ACCOUNT.ID, ACCOUNT.AGE, ARTICLE.TITLE) .from(ACCOUNT) .leftJoin(ARTICLE).on(ACCOUNT.ID.eq(ARTICLE.ACCOUNT_ID)) .where(ACCOUNT.ID.ge(1)); @@ -168,7 +173,7 @@ public class AccountTester { Account account = new Account(); account.setId(1L); account = UpdateWrapper.of(account) - .set(Account::getId,1) + .set(Account::getId, 1) .set(Account::getAge, 20) //设置 Ignore 字段,会被自动忽略 .setRaw(Account::getTitle, "xxxx") @@ -177,13 +182,11 @@ public class AccountTester { } - - @Test public void testSelectAsToDTO() { QueryWrapper queryWrapper = QueryWrapper.create(); // queryWrapper.select(ACCOUNT.ALL_COLUMNS,ARTICLE.TITLE.as(AccountDTO::getPermissions)) - queryWrapper.select(ACCOUNT.ALL_COLUMNS,ACCOUNT.USER_NAME.as(AccountDTO::getTestOtherField)) + queryWrapper.select(ACCOUNT.ALL_COLUMNS, ACCOUNT.USER_NAME.as(AccountDTO::getTestOtherField)) // queryWrapper.select(ACCOUNT.ALL_COLUMNS) .from(ACCOUNT).leftJoin(ARTICLE).on(ACCOUNT.ID.eq(ARTICLE.ACCOUNT_ID)); List accountDTOS = accountMapper.selectListByQueryAs(queryWrapper, AccountDTO.class); @@ -277,7 +280,7 @@ public class AccountTester { account1.setUserName("michael"); account1.setAge(5); - Assert.assertEquals(mapper.insertSelective(account1),1); + Assert.assertEquals(mapper.insertSelective(account1), 1); Account6 account2 = new Account6(); @@ -285,7 +288,34 @@ public class AccountTester { account2.setUserName("michael"); account2.setAge(5); - Assert.assertEquals(mapper.insertSelective(account2),1); + Assert.assertEquals(mapper.insertSelective(account2), 1); + } + + + /** + * issues https://gitee.com/mybatis-flex/mybatis-flex/issues/I88TX1 + */ + @Test + public void testInsertWithEntityId() { + Account7Mapper mapper = MybatisFlexBootstrap.getInstance() + .getMapper(Account7Mapper.class); + + Account7 account1 = new Account7(); + account1.setId(1L); + account1.setUserName("michael"); + account1.setAge(5); + + int result1 = mapper.insert(account1); + Assert.assertEquals(result1, 1); + + + Account7 account2 = new Account7(); +// account2.setId(1L); 不设置主键,自动生成主键 + account2.setUserName("michael"); + account2.setAge(5); + + int result2 = mapper.insert(account2); + Assert.assertEquals(result2, 1); } diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/TestKeyGenerator.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/TestKeyGenerator.java new file mode 100644 index 00000000..43174e5e --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/TestKeyGenerator.java @@ -0,0 +1,10 @@ +package com.mybatisflex.test; + +import com.mybatisflex.core.keygen.IKeyGenerator; + +public class TestKeyGenerator implements IKeyGenerator { + @Override + public Object generate(Object entity, String keyColumn) { + return System.currentTimeMillis() / 1000; + } +}