123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- /*
- * Copyright (c) 2011-2025, 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.assist.ISqlRunner;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
- import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
- import org.apache.ibatis.logging.Log;
- import org.apache.ibatis.logging.LogFactory;
- import org.apache.ibatis.parsing.GenericTokenParser;
- import org.apache.ibatis.session.SqlSession;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.mybatis.spring.SqlSessionUtils;
- import org.springframework.transaction.annotation.Transactional;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Optional;
- /**
- * SqlRunner 执行 SQL
- *
- * @author Caratacus
- * @since 2016-12-11
- */
- public class SqlRunner implements ISqlRunner {
- private static final Log LOG = LogFactory.getLog(SqlRunner.class);
- // 单例Query
- public static final SqlRunner DEFAULT = new SqlRunner();
- private Class<?> clazz;
- public SqlRunner() {
- }
- public SqlRunner(Class<?> clazz) {
- this.clazz = clazz;
- }
- /**
- * 获取默认的SqlQuery(适用于单库)
- *
- * @return ignore
- */
- public static SqlRunner db() {
- return DEFAULT;
- }
- /**
- * 根据当前class对象获取SqlQuery(适用于多库)
- *
- * @param clazz ignore
- * @return ignore
- */
- public static SqlRunner db(Class<?> clazz) {
- return new SqlRunner(clazz);
- }
- @Transactional
- @Override
- public boolean insert(String sql, Object... args) {
- SqlSession sqlSession = sqlSession();
- try {
- return SqlHelper.retBool(sqlSession.insert(INSERT, sqlMap(sql, args)));
- } finally {
- closeSqlSession(sqlSession);
- }
- }
- @Transactional
- @Override
- public boolean delete(String sql, Object... args) {
- SqlSession sqlSession = sqlSession();
- try {
- return SqlHelper.retBool(sqlSession.delete(DELETE, sqlMap(sql, args)));
- } finally {
- closeSqlSession(sqlSession);
- }
- }
- /**
- * 获取sqlMap参数
- *
- * @param sql 指定参数的格式: {0}, {1}
- * @param args 仅支持String
- * @return ignore
- */
- private Map<String, Object> sqlMap(String sql, Object... args) {
- Map<String, Object> sqlMap = getParams(args);
- sqlMap.put(SQL, parse(sql));
- return sqlMap;
- }
- /**
- * 获取执行语句
- *
- * @param sql 原始sql
- * @return 执行语句
- */
- private String parse(String sql) {
- return new GenericTokenParser("{", "}", content -> "#{" + content + "}").parse(sql);
- }
- /**
- * 获取参数列表
- *
- * @param args 参数
- * @return 参数map
- * @since 3.5.12
- */
- private Map<String, Object> getParams(Object... args) {
- if (args != null) {
- Map<String, Object> params = CollectionUtils.newHashMapWithExpectedSize(args.length);
- for (int i = 0; i < args.length; i++) {
- params.put(String.valueOf(i), args[i]);
- }
- return params;
- }
- return new HashMap<>();
- }
- /**
- * 获取sqlMap参数
- *
- * @param sql 指定参数的格式: {0}, {1}
- * @param page 分页模型
- * @param args 仅支持String
- * @return ignore
- */
- private Map<String, Object> sqlMap(String sql, IPage<?> page, Object... args) {
- Map<String, Object> sqlMap = getParams(args);
- sqlMap.put(PAGE, page);
- sqlMap.put(SQL, parse(sql));
- return sqlMap;
- }
- @Transactional
- @Override
- public boolean update(String sql, Object... args) {
- SqlSession sqlSession = sqlSession();
- try {
- return SqlHelper.retBool(sqlSession.update(UPDATE, sqlMap(sql, args)));
- } finally {
- closeSqlSession(sqlSession);
- }
- }
- /**
- * 根据sql查询Map结果集
- * <p>SqlRunner.db().selectList("select * from tbl_user where name={0}", "Caratacus")</p>
- *
- * @param sql sql语句,可添加参数,格式:{0},{1}
- * @param args 只接受String格式
- * @return ignore
- */
- @Override
- public List<Map<String, Object>> selectList(String sql, Object... args) {
- SqlSession sqlSession = sqlSession();
- try {
- return sqlSession.selectList(SELECT_LIST, sqlMap(sql, args));
- } finally {
- closeSqlSession(sqlSession);
- }
- }
- /**
- * 根据sql查询一个字段值的结果集
- * <p>注意:该方法只会返回一个字段的值, 如果需要多字段,请参考{@code selectList()}</p>
- *
- * @param sql sql语句,可添加参数,格式:{0},{1}
- * @param args 只接受String格式
- * @return ignore
- */
- @Override
- public List<Object> selectObjs(String sql, Object... args) {
- SqlSession sqlSession = sqlSession();
- try {
- return sqlSession.selectList(SELECT_OBJS, sqlMap(sql, args));
- } finally {
- closeSqlSession(sqlSession);
- }
- }
- /**
- * 根据sql查询一个字段值的一条结果
- * <p>注意:该方法只会返回一个字段的值, 如果需要多字段,请参考{@code selectOne()}</p>
- *
- * @param sql sql语句,可添加参数,格式:{0},{1}
- * @param args 只接受String格式
- * @return ignore
- */
- @Override
- public Object selectObj(String sql, Object... args) {
- return SqlHelper.getObject(LOG, selectObjs(sql, args));
- }
- @Override
- public long selectCount(String sql, Object... args) {
- SqlSession sqlSession = sqlSession();
- try {
- return SqlHelper.retCount(sqlSession.<Long>selectOne(COUNT, sqlMap(sql, args)));
- } finally {
- closeSqlSession(sqlSession);
- }
- }
- @Override
- public Map<String, Object> selectOne(String sql, Object... args) {
- return SqlHelper.getObject(LOG, selectList(sql, args));
- }
- @Override
- public <E extends IPage<Map<String, Object>>> E selectPage(E page, String sql, Object... args) {
- if (null == page) {
- return null;
- }
- SqlSession sqlSession = sqlSession();
- try {
- page.setRecords(sqlSession.selectList(SELECT_LIST, sqlMap(sql, page, args)));
- } finally {
- closeSqlSession(sqlSession);
- }
- return page;
- }
- /**
- * 获取Session 默认自动提交
- */
- private SqlSession sqlSession() {
- return SqlSessionUtils.getSqlSession(getSqlSessionFactory());
- }
- /**
- * 释放sqlSession
- *
- * @param sqlSession session
- */
- private void closeSqlSession(SqlSession sqlSession) {
- SqlSessionUtils.closeSqlSession(sqlSession, getSqlSessionFactory());
- }
- /**
- * 获取SqlSessionFactory
- */
- private SqlSessionFactory getSqlSessionFactory() {
- return Optional.ofNullable(clazz).map(GlobalConfigUtils::currentSessionFactory).orElse(SqlHelper.FACTORY);
- }
- /**
- * @deprecated 3.5.3.2
- */
- @Deprecated
- public void close() {
- }
- }
|