RestJdbcTemplate.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. package com.uas.erp.schedular.database;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.TypeReference;
  4. import com.uas.erp.schedular.b2b.domain.IdObject;
  5. import com.uas.erp.schedular.service.SettingService;
  6. import com.uas.erp.schedular.util.BeanUtil;
  7. import com.uas.erp.schedular.web.ResultListWrap;
  8. import com.uas.erp.schedular.web.ResultWrap;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.core.ParameterizedTypeReference;
  11. import org.springframework.http.HttpEntity;
  12. import org.springframework.http.HttpMethod;
  13. import org.springframework.http.ResponseEntity;
  14. import org.springframework.stereotype.Component;
  15. import org.springframework.util.CollectionUtils;
  16. import org.springframework.web.client.RestTemplate;
  17. import java.util.*;
  18. /**
  19. * Created by Pro1 on 2017/7/27.
  20. * 基于REST接口的JDBC操作
  21. */
  22. @Component
  23. public class RestJdbcTemplate {
  24. @Autowired
  25. private RestTemplate restTemplate;
  26. @Autowired
  27. private SettingService settingService;
  28. private String getUrl() {
  29. return settingService.getValue("api.database.url");
  30. }
  31. /**
  32. * 执行语句
  33. * @param exec
  34. * @param args
  35. */
  36. public void execute(String exec, Object... args) {
  37. ResultWrap<String> result = restTemplate.postForObject(getUrl() + "/v1/exec", Executable.execute(exec, args), ResultWrap.class);
  38. if (!result.isSuccess()) {
  39. throw new RuntimeException(result.getMessage());
  40. }
  41. }
  42. /**
  43. * 批量执行
  44. * @param execs
  45. */
  46. public void batchExecute(String[] execs) {
  47. ResultWrap<String> result = restTemplate.postForObject(getUrl() + "/v1/exec", Executable.execute(execs), ResultWrap.class);
  48. if (!result.isSuccess()) {
  49. throw new RuntimeException(result.getMessage());
  50. }
  51. }
  52. /**
  53. * 批量执行
  54. * @param execs
  55. */
  56. public void batchExecute(List<String> execs) {
  57. batchExecute(execs.toArray(new String[]{}));
  58. }
  59. /**
  60. * 查询列表
  61. * @param exec
  62. * @param args
  63. * @return
  64. */
  65. public List<Map<String, Object>> queryForList(String exec, Object... args){
  66. HttpEntity request = new HttpEntity<Executable>(Executable.query(exec, args));
  67. ParameterizedTypeReference<ResultWrap<List<Map<String, Object>>>> responseType = new ParameterizedTypeReference<ResultWrap<List<Map<String, Object>>>>(){};
  68. ResponseEntity<ResultWrap<List<Map<String, Object>>>> response = restTemplate.exchange(getUrl() + "/v1/exec", HttpMethod.POST, request, responseType);
  69. ResultWrap<List<Map<String, Object>>> result = response.getBody();
  70. if (result.isSuccess()) {
  71. return result.getContent();
  72. }
  73. throw new RuntimeException(result.getMessage());
  74. }
  75. /**
  76. * 查询单行
  77. * @param exec
  78. * @param args
  79. * @return
  80. */
  81. public Map<String, Object> queryForMap(String exec, Object... args){
  82. List<Map<String, Object>> data = queryForList(exec, args);
  83. if (!CollectionUtils.isEmpty(data)) {
  84. return data.get(0);
  85. }
  86. return null;
  87. }
  88. /**
  89. * 查询单行,返回值的数组
  90. * @param exec
  91. * @param args
  92. * @return
  93. */
  94. public Object[] queryForArray(String exec, Object... args){
  95. Map<String, Object> data = queryForMap(exec, args);
  96. if (null != data) {
  97. return data.values().toArray();
  98. }
  99. return null;
  100. }
  101. /**
  102. * 查询,返回单个字段的值
  103. * @param exec
  104. * @param args
  105. * @return
  106. */
  107. public Object getObject(String exec, Object... args){
  108. Map<String, Object> data = queryForMap(exec, args);
  109. if (null != data) {
  110. return data.values().iterator().next();
  111. }
  112. return null;
  113. }
  114. /**
  115. * 查询,返回单个Integer类型的值
  116. * @param exec
  117. * @param args
  118. * @return
  119. */
  120. public int getInt(String exec, Object... args){
  121. Object data = getObject(exec, args);
  122. if (null != data) {
  123. return (int) data;
  124. }
  125. return 0;
  126. }
  127. /**
  128. * 查询,返回单个Double类型的值
  129. * @param exec
  130. * @param args
  131. * @return
  132. */
  133. public double getDouble(String exec, Object... args){
  134. Object data = getObject(exec, args);
  135. if (null != data) {
  136. return (double) data;
  137. }
  138. return 0D;
  139. }
  140. /**
  141. * 查询,返回单个Float类型的值
  142. * @param exec
  143. * @param args
  144. * @return
  145. */
  146. public float getFloat(String exec, Object... args){
  147. Object data = getObject(exec, args);
  148. if (null != data) {
  149. return (float) data;
  150. }
  151. return 0F;
  152. }
  153. /**
  154. * 查询,返回单个String类型的值
  155. * @param exec
  156. * @param args
  157. * @return
  158. */
  159. public String getString(String exec, Object... args){
  160. Object data = getObject(exec, args);
  161. if (null != data) {
  162. return data.toString();
  163. }
  164. return null;
  165. }
  166. /**
  167. * 统计行
  168. * @param exec
  169. * @param args
  170. * @return
  171. */
  172. public int count(String exec, Object... args){
  173. if (!exec.toLowerCase().startsWith("select ")) {
  174. exec = "select count(1) " + exec;
  175. }
  176. return getInt(exec, args);
  177. }
  178. /**
  179. * 取序列值
  180. * @param sequenceName
  181. * @return
  182. */
  183. public int generate(String sequenceName) {
  184. return getInt("select " + sequenceName + ".nextval from dual");
  185. }
  186. /**
  187. * 取编号值
  188. * @param sequenceName
  189. * @param type
  190. * @return
  191. */
  192. public String generateCode(String sequenceName, int type) {
  193. return callForString("{call Sp_GetMaxNumber(?,?,?)}", sequenceName, type);
  194. }
  195. /**
  196. * 查询对象列表
  197. * @param <T>
  198. * @return
  199. */
  200. public <T> List<T> queryForBeanList(String exec, Class<T> targetCls, Object... args) {
  201. HttpEntity request = new HttpEntity<Executable>(Executable.query(exec, BeanUtil.getProperties(targetCls), args));
  202. String resultStr = restTemplate.postForObject(getUrl() + "/v1/exec", request, String.class);
  203. // json工具对超过两层的嵌套泛型无法解析,用ResultListWrap<T>替代ResultWrap<List<T>>
  204. ResultListWrap<T> result = JSON.parseObject(resultStr, new TypeReference<ResultListWrap<T>>(targetCls){});
  205. if (result.isSuccess()) {
  206. return result.getContent();
  207. }
  208. throw new RuntimeException(result.getMessage());
  209. }
  210. /**
  211. * 查询对象
  212. * @param <T>
  213. * @return
  214. */
  215. public <T> T queryForBean(String exec, Class<T> targetCls, Object... args) {
  216. List<T> beans = queryForBeanList(exec, targetCls, args);
  217. if (!CollectionUtils.isEmpty(beans)) {
  218. return beans.get(0);
  219. }
  220. return null;
  221. }
  222. /**
  223. * call procedure
  224. * @param exec call语句
  225. * @param args in参数
  226. * @param outParams out参数
  227. * @return
  228. */
  229. public Map<String, Object> callForMap(String exec, Object[] args, String[] outParams){
  230. HttpEntity request = new HttpEntity<Executable>(Executable.call(exec, outParams, args));
  231. ParameterizedTypeReference<ResultWrap<List<Map<String, Object>>>> responseType = new ParameterizedTypeReference<ResultWrap<List<Map<String, Object>>>>(){};
  232. ResponseEntity<ResultWrap<List<Map<String, Object>>>> response = restTemplate.exchange(getUrl() + "/v1/exec", HttpMethod.POST, request, responseType);
  233. ResultWrap<List<Map<String, Object>>> result = response.getBody();
  234. if (result.isSuccess()) {
  235. List<Map<String, Object>> list = result.getContent();
  236. if (!CollectionUtils.isEmpty(list)) {
  237. return list.get(0);
  238. }
  239. return null;
  240. }
  241. throw new RuntimeException(result.getMessage());
  242. }
  243. /**
  244. * call procedure and return string
  245. * @param exec
  246. * @param args
  247. * @return
  248. */
  249. public String callForString(String exec, Object... args) {
  250. Map<String, Object> data = callForMap(exec, args, new String[]{"v_return"});
  251. Object code = null == data ? null : data.get("v_return");
  252. return null == code ? null : code.toString();
  253. }
  254. public <T> List<Long> queryIdList(String sql, Class<T> elementType) {
  255. HttpEntity request = new HttpEntity<Executable>(Executable.query(sql, BeanUtil.getProperties(elementType), null));
  256. String resultStr = restTemplate.postForObject(getUrl() + "/v1/exec", request, String.class);
  257. ResultListWrap<IdObject> result = JSON.parseObject(resultStr, new TypeReference<ResultListWrap<IdObject>>(elementType){});
  258. List<Long> list = new ArrayList<>();
  259. if (!CollectionUtils.isEmpty(result.getContent())) {
  260. for (IdObject object : result.getContent()) {
  261. list.add(object.getId());
  262. }
  263. }
  264. return list;
  265. }
  266. }