From a24ea25d5c338c4ca6fc06c32873fe2d5dcf1045 Mon Sep 17 00:00:00 2001 From: VampireAchao Date: Wed, 1 Jun 2022 10:04:58 +0800 Subject: [PATCH] =?UTF-8?q?3.5.2=E7=9A=84SimpleQuery=E5=92=8CSqlHelper?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=88=B0mp4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../extension/toolkit/SimpleQuery.java | 47 ++++++++-------- .../extension/toolkit/SqlHelper.java | 54 ++++++++----------- 2 files changed, 45 insertions(+), 56 deletions(-) diff --git a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/SimpleQuery.java b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/SimpleQuery.java index b8c9f49..7be35a7 100644 --- a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/SimpleQuery.java +++ b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/SimpleQuery.java @@ -1,13 +1,24 @@ +/* + * Copyright (c) 2011-2022, baomidou (jobob@qq.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.baomidou.mybatisplus.extension.toolkit; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; -import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; -import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils; import com.baomidou.mybatisplus.core.toolkit.LambdaUtils; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; -import org.apache.ibatis.session.SqlSession; -import org.mybatis.spring.SqlSessionUtils; import java.util.*; import java.util.function.*; @@ -109,7 +120,7 @@ public class SimpleQuery { * ignore */ @SafeVarargs - public static > M group(LambdaQueryWrapper wrapper, SFunction sFunction, Collector downstream, Consumer... peeks) { + public static Map group(LambdaQueryWrapper wrapper, SFunction sFunction, Collector downstream, Consumer... peeks) { return listGroupBy(selectList(getType(sFunction), wrapper), sFunction, downstream, false, peeks); } @@ -125,11 +136,10 @@ public class SimpleQuery { * @param 实体中的分组依据对应类型,也是Map中key的类型 * @param 下游操作对应返回类型,也是Map中value的类型 * @param 下游操作在进行中间操作时对应类型 - * @param 最后返回结果Map类型 * @return Map<实体中的属性, List < 实体>> */ @SafeVarargs - public static > M group(LambdaQueryWrapper wrapper, SFunction sFunction, Collector downstream, boolean isParallel, Consumer... peeks) { + public static Map group(LambdaQueryWrapper wrapper, SFunction sFunction, Collector downstream, boolean isParallel, Consumer... peeks) { return listGroupBy(selectList(getType(sFunction), wrapper), sFunction, downstream, isParallel, peeks); } @@ -199,7 +209,7 @@ public class SimpleQuery { * ignore */ @SafeVarargs - public static > M listGroupBy(List list, SFunction sFunction, Collector downstream, Consumer... peeks) { + public static Map listGroupBy(List list, SFunction sFunction, Collector downstream, Consumer... peeks) { return listGroupBy(list, sFunction, downstream, false, peeks); } @@ -215,14 +225,13 @@ public class SimpleQuery { * @param 实体中的分组依据对应类型,也是Map中key的类型 * @param 下游操作对应返回类型,也是Map中value的类型 * @param 下游操作在进行中间操作时对应类型 - * @param 最后返回结果Map类型 * @return Map<实体中的属性, List < 实体>> */ @SafeVarargs @SuppressWarnings("unchecked") - public static > M listGroupBy(List list, SFunction sFunction, Collector downstream, boolean isParallel, Consumer... peeks) { + public static Map listGroupBy(List list, SFunction sFunction, Collector downstream, boolean isParallel, Consumer... peeks) { boolean hasFinished = downstream.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH); - return peekStream(list, isParallel, peeks).collect(new Collector, M>() { + return peekStream(list, isParallel, peeks).collect(new Collector, Map>() { @Override public Supplier> supplier() { return HashMap::new; @@ -249,11 +258,11 @@ public class SimpleQuery { } @Override - public Function, M> finisher() { - return hasFinished ? i -> (M) i : intermediate -> { + public Function, Map> finisher() { + return hasFinished ? i -> (Map) i : intermediate -> { intermediate.replaceAll((k, v) -> (A) downstream.finisher().apply(v)); @SuppressWarnings("unchecked") - M castResult = (M) intermediate; + Map castResult = (Map) intermediate; return castResult; }; } @@ -317,15 +326,7 @@ public class SimpleQuery { * @return 查询列表结果 */ public static List selectList(Class entityClass, LambdaQueryWrapper wrapper) { - SqlSession sqlSession = SqlHelper.sqlSession(entityClass); - List result; - try { - BaseMapper userMapper = SqlHelper.getMapper(entityClass, sqlSession); - result = userMapper.selectList(wrapper); - } finally { - SqlSessionUtils.closeSqlSession(sqlSession, GlobalConfigUtils.currentSessionFactory(entityClass)); - } - return result; + return SqlHelper.execute(entityClass, m -> m.selectList(wrapper)); } } diff --git a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/SqlHelper.java b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/SqlHelper.java index dcae9c8..8223d70 100644 --- a/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/SqlHelper.java +++ b/mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/SqlHelper.java @@ -20,11 +20,11 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.core.toolkit.*; +import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import lombok.SneakyThrows; import org.apache.ibatis.exceptions.PersistenceException; import org.apache.ibatis.logging.Log; import org.apache.ibatis.reflection.ExceptionUtil; -import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; @@ -264,33 +264,6 @@ public final class SqlHelper { return mapper.getName() + StringPool.DOT + sqlMethod.getMethod(); } - - /** - * 通过entityClass获取Mapper - * - * @param entityClass 实体 - * @param 实体类型 - * @return Mapper - * @deprecated 使用后未释放连接 {@link SqlHelper#getMapper(Class, SqlSession)} - */ - @SuppressWarnings("unchecked") - @Deprecated - public static BaseMapper getMapper(Class entityClass) { - Optional.ofNullable(entityClass).orElseThrow(() -> ExceptionUtils.mpe("entityClass can't be null!")); - TableInfo tableInfo = Optional.ofNullable(TableInfoHelper.getTableInfo(entityClass)).orElseThrow(() -> ExceptionUtils.mpe("Can not find TableInfo from Class: \"%s\".", entityClass.getName())); - String namespace = tableInfo.getCurrentNamespace(); - - Configuration configuration = tableInfo.getConfiguration(); - SqlSession sqlSession = sqlSession(entityClass); - BaseMapper mapper; - try { - mapper = (BaseMapper) configuration.getMapper(Class.forName(namespace), sqlSession); - } catch (ClassNotFoundException e) { - throw ExceptionUtils.mpe(e); - } - return mapper; - } - /** * 通过entityClass获取Mapper,记得要释放连接 * 例: {@code @@ -308,13 +281,28 @@ public final class SqlHelper { */ @SuppressWarnings("unchecked") public static BaseMapper getMapper(Class entityClass, SqlSession sqlSession) { - Optional.ofNullable(entityClass).orElseThrow(() -> ExceptionUtils.mpe("entityClass can't be null!")); + Assert.notNull(entityClass, "entityClass can't be null!"); TableInfo tableInfo = Optional.ofNullable(TableInfoHelper.getTableInfo(entityClass)).orElseThrow(() -> ExceptionUtils.mpe("Can not find TableInfo from Class: \"%s\".", entityClass.getName())); + Class mapperClass = ClassUtils.toClassConfident(tableInfo.getCurrentNamespace()); + return (BaseMapper) tableInfo.getConfiguration().getMapper(mapperClass, sqlSession); + } + + /** + * 通过entityClass获取BaseMapper,再传入lambda使用该mapper,本方法自动释放链接 + * + * @param entityClass 实体类 + * @param sFunction lambda操作,例如 {@code m->m.selectList(wrapper)} + * @param 实体类的类型 + * @param 返回值类型 + * @return 返回lambda执行结果 + */ + public static R execute(Class entityClass, SFunction, R> sFunction) { + SqlSession sqlSession = SqlHelper.sqlSession(entityClass); try { - Configuration configuration = tableInfo.getConfiguration(); - return (BaseMapper) configuration.getMapper(Class.forName(tableInfo.getCurrentNamespace()), sqlSession); - } catch (ClassNotFoundException e) { - throw ExceptionUtils.mpe(e); + BaseMapper baseMapper = SqlHelper.getMapper(entityClass, sqlSession); + return sFunction.apply(baseMapper); + } finally { + SqlSessionUtils.closeSqlSession(sqlSession, GlobalConfigUtils.currentSessionFactory(entityClass)); } } } -- Gitee