feat: add UpdateChain.java

This commit is contained in:
开源海哥 2023-07-25 14:38:23 +08:00
parent 290335f958
commit fe8b4f5890
6 changed files with 174 additions and 19 deletions

View File

@ -55,12 +55,6 @@ public interface BaseMapper<T> {
*/
int DEFAULT_BATCH_SIZE = 1000;
default QueryWrapperChain<T> queryChain() {
return new QueryWrapperChain<>(this);
}
// === insert ===
/**

View File

@ -23,10 +23,14 @@ import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.util.MapUtil;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
@ -77,6 +81,8 @@ public class Mappers {
static class MapperHandler implements InvocationHandler {
private static final Set<String> ignoreMethods = new HashSet<>(Arrays.asList("queryChain","updateChain"));
private static final MethodHandles.Lookup lookup = MethodHandles.lookup();
private Class<?> mapperClass;
private final SqlSessionFactory sqlSessionFactory = FlexGlobalConfig.getDefaultConfig().getSqlSessionFactory();
private final ExecutorType executorType = FlexGlobalConfig.getDefaultConfig().getConfiguration().getDefaultExecutorType();

View File

@ -41,18 +41,6 @@ public class QueryWrapperChain<T> extends QueryWrapperAdapter<QueryWrapperChain<
return new QueryWrapperChain<>(baseMapper);
}
public boolean remove() {
return SqlUtil.toBool(baseMapper.deleteByQuery(this));
}
public boolean update(T entity) {
return SqlUtil.toBool(baseMapper.updateByQuery(entity, this));
}
public boolean update(T entity, boolean ignoreNulls) {
return SqlUtil.toBool(baseMapper.updateByQuery(entity, ignoreNulls, this));
}
public long count() {
return baseMapper.selectCountByQuery(this);
}

View File

@ -530,7 +530,7 @@ public interface IService<T> {
}
default QueryWrapperChain<T> queryChain() {
return getMapper().queryChain();
return new QueryWrapperChain<>(getMapper());
}
}

View File

@ -0,0 +1,111 @@
/*
* 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.core.update;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.mybatis.Mappers;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryWrapperAdapter;
import com.mybatisflex.core.util.ClassUtil;
import com.mybatisflex.core.util.LambdaGetter;
import com.mybatisflex.core.util.SqlUtil;
import com.mybatisflex.core.util.UpdateEntity;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
/**
* 用于数据更新删除的链式操作
*
* @author michale
* @since 2023-07-25
*/
public class UpdateChain<T> extends QueryWrapperAdapter<UpdateChain<T>> {
private final BaseMapper<T> baseMapper;
private final T entity;
private final UpdateWrapper entityWrapper;
public static <T> UpdateChain<T> of(Class<T> entityClass) {
BaseMapper<T> baseMapper = Mappers.ofEntityClass(entityClass);
return new UpdateChain<>(baseMapper);
}
public UpdateChain(BaseMapper<T> baseMapper) {
this.baseMapper = baseMapper;
this.entity = createEntity(ClassUtil.getUsefulClass(baseMapper.getClass()));
this.entityWrapper = (UpdateWrapper) entity;
}
private T createEntity(Class<?> mapperClass) {
Type type = mapperClass.getGenericInterfaces()[0];
if (type instanceof ParameterizedType) {
Class<T> modelClass = (Class<T>) ((ParameterizedType) type).getActualTypeArguments()[0];
return UpdateEntity.of(modelClass);
}
throw FlexExceptions.wrap("Can not get entity class from mapper: " + mapperClass.getName());
}
public static <E> UpdateChain<E> create(BaseMapper<E> baseMapper) {
return new UpdateChain<>(baseMapper);
}
public UpdateChain<T> set(String property, Object value) {
entityWrapper.set(property, value);
return this;
}
public UpdateChain<T> set(LambdaGetter<T> getter, Object value) {
entityWrapper.set(getter, value);
return this;
}
public UpdateChain<T> set(QueryColumn queryColumn, Object value) {
entityWrapper.set(queryColumn, value);
return this;
}
public UpdateChain<T> setRaw(String property, Object value) {
entityWrapper.setRaw(property, value);
return this;
}
public UpdateChain<T> setRaw(LambdaGetter<T> getter, Object value) {
entityWrapper.setRaw(getter, value);
return this;
}
public UpdateChain<T> setRaw(QueryColumn queryColumn, Object value) {
entityWrapper.set(queryColumn, value);
return this;
}
public boolean remove() {
return SqlUtil.toBool(baseMapper.deleteByQuery(this));
}
public boolean update() {
return SqlUtil.toBool(baseMapper.updateByQuery(entity, this));
}
}

View File

@ -0,0 +1,56 @@
package com.mybatisflex.test;
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.update.UpdateChain;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import javax.sql.DataSource;
public class UpdateChainTest {
static AccountMapper accountMapper;
@BeforeClass
public static void init() {
DataSource dataSource = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("schema.sql")
.addScript("data.sql")
.build();
MybatisFlexBootstrap bootstrap = MybatisFlexBootstrap.getInstance()
.setDataSource(dataSource)
.setLogImpl(StdOutImpl.class)
.addMapper(AccountMapper.class)
.start();
//开启审计功能
AuditManager.setAuditEnable(true);
//设置 SQL 审计收集器
MessageCollector collector = new ConsoleMessageCollector();
AuditManager.setMessageCollector(collector);
accountMapper = bootstrap.getMapper(AccountMapper.class);
}
@Test
public void testUpdateChain() {
UpdateChain.of(Account.class)
.set(Account::getUserName,"张三")
.setRaw(Account::getAge,"age + 1")
.where(Account::getId).eq(1)
.update();
Account account = accountMapper.selectOneById(1);
System.out.println(account);
}
}