DAOService.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package com.primeton.filetransfer.server.template;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.apache.commons.lang.StringUtils;
  4. import org.springframework.jdbc.core.ColumnMapRowMapper;
  5. import org.springframework.jdbc.core.JdbcTemplate;
  6. import org.springframework.jdbc.core.RowCallbackHandler;
  7. import javax.validation.constraints.NotNull;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.util.ArrayList;
  11. import java.util.LinkedList;
  12. import java.util.List;
  13. import java.util.Map;
  14. import java.util.Optional;
  15. /**
  16. * 为了升级元数据,兼容 hibernate 的 API
  17. *
  18. * <pre>
  19. *
  20. * Created by zhaopx.
  21. * User: zhaopx
  22. * Date: 2020/6/3
  23. * Time: 11:12
  24. *
  25. * </pre>
  26. *
  27. * @author zhaopx
  28. */
  29. @Slf4j
  30. public class DAOService {
  31. /**
  32. * 构造方法传入
  33. */
  34. private final JdbcTemplate jdbcTemplate;
  35. public DAOService(JdbcTemplate jdbcTemplate) {
  36. this.jdbcTemplate = jdbcTemplate;
  37. }
  38. /** Get HQL from xxxSql.xml file*/
  39. public String getSQL(String name) {
  40. return HibernateStatementSessionFactoryBean.getStatementText(name);
  41. }
  42. /** Get dynamic HQL from xxxSql.xml file, voOrMap is a VO or a map.*/
  43. public String getSQL(String name, Object voOrMap) {
  44. return HibernateStatementSessionFactoryBean.getStatementText(name, voOrMap);
  45. }
  46. /** Get dynamic Params from xxxSql.xml file, voOrMap is a VO or a map.*/
  47. public Object[] getParam(String name, Object voOrMap) {
  48. return HibernateStatementSessionFactoryBean.getStatementParam(name, voOrMap);
  49. }
  50. /** Get dynamic Params from xxxSql.xml file, voOrMap is a VO or a map.*/
  51. public Map getMapParam(String name, Object voOrMap) {
  52. return HibernateStatementSessionFactoryBean.getStatementParamMap(name, voOrMap);
  53. }
  54. /**
  55. * 执行查询 SQL 无参数
  56. *
  57. * @param sql
  58. * @return
  59. */
  60. public List<Map> queryForMap(String sql) {
  61. return queryForMap(sql, new String[]{});
  62. }
  63. /**
  64. * 执行一次查询
  65. *
  66. * @param sql
  67. * @param params
  68. * @return
  69. */
  70. public List<Map> queryForMap(String sql, Object[] params) {
  71. params = Optional.ofNullable(params).orElse(new Object[]{});
  72. return (List) jdbcTemplate.query(sql, new ColumnMapRowMapper(), params);
  73. }
  74. /**
  75. * 执行一次查询
  76. * @param sql
  77. * @param entityClass
  78. * @param <T>
  79. * @return
  80. */
  81. public <T> T queryForObject(String sql, Class<T> entityClass) {
  82. return queryForObject(sql, null, entityClass);
  83. }
  84. /**
  85. * 执行一次查询
  86. *
  87. * @param sql
  88. * @param params
  89. * @param entityClass
  90. * @return
  91. */
  92. public <T> T queryForObject(String sql, Object[] params, Class<T> entityClass) {
  93. params = Optional.ofNullable(params).orElse(new Object[]{});
  94. final ResultTransformer resultTransformer = (entityClass == null || entityClass == Map.class) ? new MapResultTransformer() : new DOResultTransformer(entityClass);
  95. EntityRowCallbackHandler<T> rowCallbackHandler = new EntityRowCallbackHandler(resultTransformer);
  96. jdbcTemplate.query(sql, params, rowCallbackHandler);
  97. final List<T> result = rowCallbackHandler.getResult();
  98. return result.isEmpty() ? null : result.get(0);
  99. }
  100. /**
  101. * 执行SQL查询
  102. * @param id statement id
  103. * @return Object[]/Object
  104. */
  105. public List<Map<String, Object>> queryById(String id) {
  106. return queryById(id, null, null);
  107. }
  108. /**
  109. * 执行SQL查询
  110. * @param id statement id
  111. * @return Object[]/Object
  112. */
  113. public <T> List<T> queryById(String id, Class<T> entityClass) {
  114. return queryById(id, null, entityClass);
  115. }
  116. /**
  117. * 执行SQL查询
  118. * @param id 脚本
  119. * @param params 参数
  120. * @return Object[]/Object
  121. */
  122. public List<Map<String, Object>> queryById(String id, Map<String, Object> params){
  123. return queryById(id, params, null);
  124. }
  125. /**
  126. * 执行 statement id 的查询,返回 ResultTransformer 的转换结果
  127. * @param id statement id
  128. * @param params 参数
  129. * @param entityClass 结果转换
  130. * @return 返回 ResultTransformer 的转换结果
  131. */
  132. public <T> List<T> queryById(String id, Map<String, Object> params, Class<T> entityClass) {
  133. final String sql = getSQL(id, params);
  134. if (StringUtils.isBlank(sql)) {
  135. throw new IllegalArgumentException("undefined statement id: " + id);
  136. }
  137. Object[] sqlParams = (params == null ? null : getParam(id, params));
  138. sqlParams = Optional.ofNullable(sqlParams).orElse(new Object[]{});
  139. final ResultTransformer resultTransformer = (entityClass == null || entityClass == Map.class) ? new MapResultTransformer() : new DOResultTransformer(entityClass);
  140. EntityRowCallbackHandler<T> rowCallbackHandler = new EntityRowCallbackHandler(resultTransformer);
  141. jdbcTemplate.query(sql, sqlParams, rowCallbackHandler);
  142. return rowCallbackHandler.getResult();
  143. }
  144. /**
  145. * 执行一个 更新 SQL
  146. *
  147. * @param sql
  148. * @return
  149. */
  150. public int updateSQL(String sql) {
  151. return updateSQL(sql, new String[]{});
  152. }
  153. /**
  154. * 执行一个 更新 SQL
  155. *
  156. * @param sql
  157. * @param params
  158. * @return
  159. */
  160. public int updateSQL(String sql, Object[] params) {
  161. params = Optional.ofNullable(params).orElse(new Object[]{});
  162. return jdbcTemplate.update(sql, params);
  163. }
  164. /**
  165. * 执行一个 更新 SQL
  166. *
  167. * @param id
  168. * @return
  169. */
  170. public int updateById(String id) {
  171. return updateSQL(getSQL(id), new Object[]{});
  172. }
  173. /**
  174. * 执行一个 更新 SQL
  175. *
  176. * @param id
  177. * @param params
  178. * @return
  179. */
  180. public int updateById(String id, Map<String, Object> params) {
  181. return updateSQL(getSQL(id, params), getParam(id, params));
  182. }
  183. /**
  184. * 批量执行 SQL
  185. *
  186. * @param batchSql
  187. * @return
  188. */
  189. public int[] batchUpdateSQL(String... batchSql) throws SQLException {
  190. if (batchSql.length == 0) {
  191. return new int[]{};
  192. }
  193. return jdbcTemplate.batchUpdate(batchSql);
  194. }
  195. class EntityRowCallbackHandler<T> implements RowCallbackHandler {
  196. private final ResultTransformer<T> resultTransformer;
  197. /**
  198. * 结果集
  199. */
  200. private final List<T> result = new LinkedList<>();
  201. public EntityRowCallbackHandler(ResultTransformer<T> resultTransformer) {
  202. this.resultTransformer = resultTransformer;
  203. }
  204. @Override
  205. public void processRow(ResultSet rs) throws SQLException {
  206. final int columnCount = rs.getMetaData().getColumnCount();
  207. // 返回的字段名字
  208. List<String> columnNames = new ArrayList<>(columnCount);
  209. for (int i = 1; i <= columnCount; i++) {
  210. // 转为大写
  211. columnNames.add(StringUtils.upperCase(rs.getMetaData().getColumnName(i)));
  212. }
  213. // 字段列表
  214. String[] aliass = columnNames.toArray(new String[0]);
  215. // 循环遍历获取值
  216. while (rs.next()) {
  217. List<Object> tuple = new ArrayList<>(columnCount);
  218. for (String columnName : columnNames) {
  219. tuple.add(rs.getObject(columnName));
  220. }
  221. result.add(resultTransformer.transformTuple(tuple.toArray(new Object[0]), aliass));
  222. }
  223. }
  224. @NotNull
  225. public List<T> getResult() {
  226. return result;
  227. }
  228. }
  229. }