mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 17:48:25 +08:00
feat: 在 insert 时,若 entity 有主键,则直接使用 entity 的主键,不再通过主键生成器来生成; close #I88TX1
This commit is contained in:
parent
90d6bc1051
commit
9c204fbcb0
@ -69,10 +69,15 @@ public class CustomKeyGenerator implements KeyGenerator {
|
|||||||
@Override
|
@Override
|
||||||
public void processBefore(Executor executor, MappedStatement ms, Statement stmt, Object parameter) {
|
public void processBefore(Executor executor, MappedStatement ms, Statement stmt, Object parameter) {
|
||||||
Object entity = ((Map) parameter).get(FlexConsts.ENTITY);
|
Object entity = ((Map) parameter).get(FlexConsts.ENTITY);
|
||||||
|
try {
|
||||||
|
Object existId = tableInfo.getValue(entity, idInfo.getProperty());
|
||||||
|
// 若用户主动设置了主键,则使用用户自己设置的主键,不再生成主键
|
||||||
|
if (existId != null){
|
||||||
|
return;
|
||||||
|
}
|
||||||
Configuration configuration = ms.getConfiguration();
|
Configuration configuration = ms.getConfiguration();
|
||||||
MetaObject metaParam = configuration.newMetaObject(parameter);
|
MetaObject metaParam = configuration.newMetaObject(parameter);
|
||||||
Object generateId = keyGenerator.generate(entity, idInfo.getColumn());
|
Object generateId = keyGenerator.generate(entity, idInfo.getColumn());
|
||||||
try {
|
|
||||||
MetaObject metaObjectForProperty = metaParam.metaObjectForProperty(FlexConsts.ENTITY);
|
MetaObject metaObjectForProperty = metaParam.metaObjectForProperty(FlexConsts.ENTITY);
|
||||||
Invoker setInvoker = tableInfo.getReflector().getSetInvoker(idInfo.getProperty());
|
Invoker setInvoker = tableInfo.getReflector().getSetInvoker(idInfo.getProperty());
|
||||||
Object id = ConvertUtil.convert(generateId, setInvoker.getType());
|
Object id = ConvertUtil.convert(generateId, setInvoker.getType());
|
||||||
|
|||||||
@ -55,8 +55,13 @@ public class RowCustomKeyGenerator implements KeyGenerator {
|
|||||||
@Override
|
@Override
|
||||||
public void processBefore(Executor executor, MappedStatement ms, Statement stmt, Object parameter) {
|
public void processBefore(Executor executor, MappedStatement ms, Statement stmt, Object parameter) {
|
||||||
Row row = (Row) ((Map) parameter).get(FlexConsts.ROW);
|
Row row = (Row) ((Map) parameter).get(FlexConsts.ROW);
|
||||||
Object generateId = keyGenerator.generate(row, rowKey.getKeyColumn());
|
|
||||||
try {
|
try {
|
||||||
|
Object existId = row.get(rowKey.getKeyColumn());
|
||||||
|
// 若用户主动设置了主键,则使用用户自己设置的主键,不再生成主键
|
||||||
|
if (existId != null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Object generateId = keyGenerator.generate(row, rowKey.getKeyColumn());
|
||||||
row.put(rowKey.getKeyColumn(), generateId);
|
row.put(rowKey.getKeyColumn(), generateId);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw FlexExceptions.wrap(e);
|
throw FlexExceptions.wrap(e);
|
||||||
|
|||||||
@ -35,7 +35,7 @@ public class ColumnInfo {
|
|||||||
protected String[] alias;
|
protected String[] alias;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* java entity 定义的属性名称。
|
* java entity 定义的属性名称(field name)。
|
||||||
*/
|
*/
|
||||||
protected String property;
|
protected String property;
|
||||||
|
|
||||||
|
|||||||
@ -718,6 +718,10 @@ public class TableInfo {
|
|||||||
return values;
|
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) {
|
private Object buildColumnSqlArg(MetaObject metaObject, String column) {
|
||||||
ColumnInfo columnInfo = columnInfoMapping.get(column);
|
ColumnInfo columnInfo = columnInfoMapping.get(column);
|
||||||
Object value = getPropertyValue(metaObject, columnInfo.property);
|
Object value = getPropertyValue(metaObject, columnInfo.property);
|
||||||
|
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
TypeHandler typeHandler = columnInfo.buildTypeHandler(null);
|
TypeHandler typeHandler = columnInfo.buildTypeHandler(null);
|
||||||
if (typeHandler != 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)) {
|
if (property != null && metaObject.hasGetter(property)) {
|
||||||
return metaObject.getValue(property);
|
return metaObject.getValue(property);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,77 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||||
|
* <p>
|
||||||
|
* 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
|
||||||
|
* <p>
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* <p>
|
||||||
|
* 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 +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -20,6 +20,7 @@ import com.mybatisflex.core.MybatisFlexBootstrap;
|
|||||||
import com.mybatisflex.core.audit.AuditManager;
|
import com.mybatisflex.core.audit.AuditManager;
|
||||||
import com.mybatisflex.core.audit.ConsoleMessageCollector;
|
import com.mybatisflex.core.audit.ConsoleMessageCollector;
|
||||||
import com.mybatisflex.core.audit.MessageCollector;
|
import com.mybatisflex.core.audit.MessageCollector;
|
||||||
|
import com.mybatisflex.core.keygen.KeyGeneratorFactory;
|
||||||
import com.mybatisflex.core.mybatis.Mappers;
|
import com.mybatisflex.core.mybatis.Mappers;
|
||||||
import com.mybatisflex.core.query.If;
|
import com.mybatisflex.core.query.If;
|
||||||
import com.mybatisflex.core.query.QueryWrapper;
|
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.update.UpdateWrapper;
|
||||||
import com.mybatisflex.core.util.UpdateEntity;
|
import com.mybatisflex.core.util.UpdateEntity;
|
||||||
import com.mybatisflex.mapper.Account6Mapper;
|
import com.mybatisflex.mapper.Account6Mapper;
|
||||||
|
import com.mybatisflex.mapper.Account7Mapper;
|
||||||
import com.mybatisflex.mapper.ArticleMapper;
|
import com.mybatisflex.mapper.ArticleMapper;
|
||||||
import org.apache.ibatis.logging.stdout.StdOutImpl;
|
import org.apache.ibatis.logging.stdout.StdOutImpl;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
@ -59,11 +61,14 @@ public class AccountTester {
|
|||||||
FlexGlobalConfig.getDefaultConfig()
|
FlexGlobalConfig.getDefaultConfig()
|
||||||
.setLogicDeleteColumn("is_delete");
|
.setLogicDeleteColumn("is_delete");
|
||||||
|
|
||||||
|
KeyGeneratorFactory.register("test", new TestKeyGenerator());
|
||||||
|
|
||||||
MybatisFlexBootstrap bootstrap = MybatisFlexBootstrap.getInstance()
|
MybatisFlexBootstrap bootstrap = MybatisFlexBootstrap.getInstance()
|
||||||
.setDataSource(dataSource)
|
.setDataSource(dataSource)
|
||||||
.setLogImpl(StdOutImpl.class)
|
.setLogImpl(StdOutImpl.class)
|
||||||
.addMapper(AccountMapper.class)
|
.addMapper(AccountMapper.class)
|
||||||
.addMapper(Account6Mapper.class)
|
.addMapper(Account6Mapper.class)
|
||||||
|
.addMapper(Account7Mapper.class)
|
||||||
.addMapper(ArticleMapper.class)
|
.addMapper(ArticleMapper.class)
|
||||||
.start();
|
.start();
|
||||||
|
|
||||||
@ -177,8 +182,6 @@ public class AccountTester {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSelectAsToDTO() {
|
public void testSelectAsToDTO() {
|
||||||
QueryWrapper queryWrapper = QueryWrapper.create();
|
QueryWrapper queryWrapper = QueryWrapper.create();
|
||||||
@ -289,4 +292,31 @@ public class AccountTester {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user