SqlQuery.java 4.07 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
/**
* Copyright (c) 2011-2020, hubin (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.mapper;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.baomidou.mybatisplus.entity.TableInfo;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.baomidou.mybatisplus.toolkit.TableInfoHelper;

/**
* <p>
* SqlQuery 执行 SQL
* </p>
*
* @author Caratacus
* @Date 2016-12-11
*/
public class SqlQuery {
private static final Log logger = LogFactory.getLog(SqlQuery.class);
// 单例Query
public static final SqlQuery SQL_QUERY = new SqlQuery();
private SqlSessionFactory sqlSessionFactory;
private TableInfo tableInfo;

public SqlQuery() {
this.tableInfo = TableInfoHelper.getRandomTableInfo();
String configMark = tableInfo.getConfigMark();
GlobalConfiguration globalConfiguration = GlobalConfiguration.GlobalConfig(configMark);
this.sqlSessionFactory = globalConfiguration.getSqlSessionFactory();
}

public SqlQuery(Class<?> clazz) {
this.tableInfo = SqlHelper.table(clazz);
GlobalConfiguration globalConfiguration = GlobalConfiguration.GlobalConfig(tableInfo.getConfigMark());
this.sqlSessionFactory = globalConfiguration.getSqlSessionFactory();
}

public boolean insert(String sql, Object... args) {
return SqlHelper.retBool(sqlSession().insert(sqlStatement("insertSql"), StringUtils.sqlArgsFill(sql, args)));
}

public boolean delete(String sql, Object... args) {
return SqlHelper.retBool(sqlSession().delete(sqlStatement("deleteSql"), StringUtils.sqlArgsFill(sql, args)));
}

public boolean update(String sql, Object... args) {
return SqlHelper.retBool(sqlSession().update(sqlStatement("updateSql"), StringUtils.sqlArgsFill(sql, args)));
}

public List<Map<String, Object>> selectList(String sql, Object... args) {
return sqlSession().selectList(sqlStatement("selectListSql"), StringUtils.sqlArgsFill(sql, args));
}

public int selectCount(String sql, Object... args) {
return sqlSession().<Integer>selectOne(sqlStatement("selectCountSql"), StringUtils.sqlArgsFill(sql, args));
}

public Map<String, Object> selectOne(String sql, Object... args) {
List<Map<String, Object>> list = selectList(sql, args);
if (CollectionUtils.isNotEmpty(list)) {
int size = list.size();
if (size > 1) {
logger.warn(String.format("Warn: selectOne Method There are %s results.", size));
}
return list.get(0);
}
return Collections.emptyMap();
}

public List<Map<String, Object>> selectPage(Pagination page, String sql, Object... args) {
if (null == page) {
return null;
}
return sqlSession().selectList(sqlStatement("selectPageSql"), StringUtils.sqlArgsFill(sql, args), page);
}

/**
* 获取默认的SqlQuery(适用于单库)
*
* @return
*/
public static SqlQuery db() {
return SQL_QUERY;
}

/**
* 根据当前class对象获取SqlQuery(适用于多库)
*
* @param clazz
* @return
*/
public static SqlQuery db(Class<?> clazz) {
return new SqlQuery(clazz);
}

/**
* <p>
* 获取Session 默认自动提交
* <p/>
*/
private SqlSession sqlSession() {
return sqlSessionFactory.openSession(true);
}

private String sqlStatement(String sqlMethod) {
return tableInfo.getSqlStatement(sqlMethod);
}

}