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