SqlHelper.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**
  2. * Copyright (c) 2011-2020, hubin (jobob@qq.com).
  3. * <p>
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. * <p>
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * <p>
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.baomidou.mybatisplus.mapper;
  17. import java.util.List;
  18. import org.apache.ibatis.logging.Log;
  19. import org.apache.ibatis.logging.LogFactory;
  20. import org.apache.ibatis.session.Configuration;
  21. import org.apache.ibatis.session.ExecutorType;
  22. import org.apache.ibatis.session.SqlSession;
  23. import org.apache.ibatis.session.SqlSessionFactory;
  24. import com.baomidou.mybatisplus.entity.TableInfo;
  25. import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
  26. import com.baomidou.mybatisplus.plugins.Page;
  27. import com.baomidou.mybatisplus.toolkit.CollectionUtils;
  28. import com.baomidou.mybatisplus.toolkit.GlobalConfigUtils;
  29. import com.baomidou.mybatisplus.toolkit.MapUtils;
  30. import com.baomidou.mybatisplus.toolkit.TableInfoHelper;
  31. /**
  32. * <p>
  33. * SQL 辅助类
  34. * </p>
  35. *
  36. * @author hubin
  37. * @Date 2016-11-06
  38. */
  39. public class SqlHelper {
  40. private static final Log logger = LogFactory.getLog(SqlHelper.class);
  41. /**
  42. * <p>
  43. * 获取Session 默认自动提交
  44. * </p>
  45. * <p>
  46. * 特别说明:这里获取SqlSession时这里虽然设置了自动提交但是如果事务托管了的话 是不起作用的 切记!!
  47. * <p/>
  48. *
  49. * @return SqlSession
  50. */
  51. public static SqlSession sqlSession(Class<?> clazz) {
  52. return sqlSession(clazz, true);
  53. }
  54. /**
  55. * <p>
  56. * 批量操作 SqlSession
  57. * </p>
  58. *
  59. * @param clazz 实体类
  60. * @return SqlSession
  61. */
  62. public static SqlSession sqlSessionBatch(Class<?> clazz) {
  63. return GlobalConfigUtils.currentSessionFactory(clazz).openSession(ExecutorType.BATCH);
  64. }
  65. /**
  66. * <p>
  67. * 获取sqlSession
  68. * </p>
  69. *
  70. * @param clazz 对象类
  71. * @return
  72. */
  73. private static SqlSession getSqlSession(Class<?> clazz) {
  74. SqlSession session = null;
  75. try {
  76. SqlSessionFactory sqlSessionFactory = GlobalConfigUtils.currentSessionFactory(clazz);
  77. Configuration configuration = sqlSessionFactory.getConfiguration();
  78. session = GlobalConfigUtils.getGlobalConfig(configuration).getSqlSession();
  79. } catch (Exception e) {
  80. // ignored
  81. }
  82. return session;
  83. }
  84. /**
  85. * <p>
  86. * 获取Session
  87. * </p>
  88. *
  89. * @param clazz 实体类
  90. * @param autoCommit true自动提交false则相反
  91. * @return SqlSession
  92. */
  93. public static SqlSession sqlSession(Class<?> clazz, boolean autoCommit) {
  94. SqlSession sqlSession = getSqlSession(clazz);
  95. return (sqlSession != null) ? sqlSession : GlobalConfigUtils.currentSessionFactory(clazz).openSession(autoCommit);
  96. }
  97. /**
  98. * <p>
  99. * 获取TableInfo
  100. * </p>
  101. *
  102. * @param clazz 对象类
  103. * @return TableInfo 对象表信息
  104. */
  105. public static TableInfo table(Class<?> clazz) {
  106. TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz);
  107. if (null == tableInfo) {
  108. throw new MybatisPlusException("Error: Cannot execute table Method, ClassGenricType not found .");
  109. }
  110. return tableInfo;
  111. }
  112. /**
  113. * <p>
  114. * 判断数据库操作是否成功
  115. * </p>
  116. *
  117. * @param result 数据库操作返回影响条数
  118. * @return boolean
  119. */
  120. public static boolean retBool(Integer result) {
  121. return null != result && result >= 1;
  122. }
  123. /**
  124. * <p>
  125. * 删除不存在的逻辑上属于成功
  126. * </p>
  127. *
  128. * @param result 数据库操作返回影响条数
  129. * @return boolean
  130. */
  131. public static boolean delBool(Integer result) {
  132. return null != result && result >= 0;
  133. }
  134. /**
  135. * <p>
  136. * 返回SelectCount执行结果
  137. * </p>
  138. *
  139. * @param result
  140. * @return int
  141. */
  142. public static int retCount(Integer result) {
  143. return (null == result) ? 0 : result;
  144. }
  145. /**
  146. * <p>
  147. * 从list中取第一条数据返回对应List中泛型的单个结果
  148. * </p>
  149. *
  150. * @param list
  151. * @param <E>
  152. * @return
  153. */
  154. public static <E> E getObject(List<E> list) {
  155. if (CollectionUtils.isNotEmpty(list)) {
  156. int size = list.size();
  157. if (size > 1) {
  158. logger.warn(String.format("Warn: execute Method There are %s results.", size));
  159. }
  160. return list.get(0);
  161. }
  162. return null;
  163. }
  164. /**
  165. * <p>
  166. * 填充Wrapper
  167. * </p>
  168. *
  169. * @param page 分页对象
  170. * @param wrapper SQL包装对象
  171. */
  172. public static void fillWrapper(Page<?> page, Wrapper<?> wrapper) {
  173. if (null == page) {
  174. return;
  175. }
  176. // wrapper 不存创建一个 Condition
  177. if (isEmptyOfWrapper(wrapper)) {
  178. wrapper = Condition.create();
  179. }
  180. // 排序
  181. if (page.isOpenSort()) {
  182. wrapper.orderAsc(page.getAsc());
  183. wrapper.orderDesc(page.getDesc());
  184. }
  185. // MAP 参数查询
  186. if (MapUtils.isNotEmpty(page.getCondition())) {
  187. wrapper.allEq(page.getCondition());
  188. }
  189. }
  190. /**
  191. * <p>
  192. * 判断Wrapper为空
  193. * </p>
  194. *
  195. * @param wrapper SQL包装对象
  196. * @return
  197. */
  198. public static boolean isEmptyOfWrapper(Wrapper<?> wrapper) {
  199. return null == wrapper || Condition.EMPTY.equals(wrapper);
  200. }
  201. /**
  202. * <p>
  203. * 判断Wrapper不为空
  204. * </p>
  205. *
  206. * @param wrapper SQL包装对象
  207. * @return
  208. */
  209. public static boolean isNotEmptyOfWrapper(Wrapper<?> wrapper) {
  210. return !isEmptyOfWrapper(wrapper);
  211. }
  212. }