| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- package com.uas.erp.schedular.database;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.TypeReference;
- import com.uas.erp.schedular.b2b.domain.IdObject;
- import com.uas.erp.schedular.service.SettingService;
- import com.uas.erp.schedular.util.BeanUtil;
- import com.uas.erp.schedular.web.ResultListWrap;
- import com.uas.erp.schedular.web.ResultWrap;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.core.ParameterizedTypeReference;
- import org.springframework.http.HttpEntity;
- import org.springframework.http.HttpMethod;
- import org.springframework.http.ResponseEntity;
- import org.springframework.stereotype.Component;
- import org.springframework.util.CollectionUtils;
- import org.springframework.web.client.RestTemplate;
- import java.util.*;
- /**
- * Created by Pro1 on 2017/7/27.
- * 基于REST接口的JDBC操作
- */
- @Component
- public class RestJdbcTemplate {
- @Autowired
- private RestTemplate restTemplate;
- @Autowired
- private SettingService settingService;
- private String getUrl() {
- return settingService.getValue("api.database.url");
- }
- /**
- * 执行语句
- * @param exec
- * @param args
- */
- public void execute(String exec, Object... args) {
- ResultWrap<String> result = restTemplate.postForObject(getUrl() + "/v1/exec", Executable.execute(exec, args), ResultWrap.class);
- if (!result.isSuccess()) {
- throw new RuntimeException(result.getMessage());
- }
- }
- /**
- * 批量执行
- * @param execs
- */
- public void batchExecute(String[] execs) {
- ResultWrap<String> result = restTemplate.postForObject(getUrl() + "/v1/exec", Executable.execute(execs), ResultWrap.class);
- if (!result.isSuccess()) {
- throw new RuntimeException(result.getMessage());
- }
- }
- /**
- * 批量执行
- * @param execs
- */
- public void batchExecute(List<String> execs) {
- batchExecute(execs.toArray(new String[]{}));
- }
- /**
- * 查询列表
- * @param exec
- * @param args
- * @return
- */
- public List<Map<String, Object>> queryForList(String exec, Object... args){
- HttpEntity request = new HttpEntity<Executable>(Executable.query(exec, args));
- ParameterizedTypeReference<ResultWrap<List<Map<String, Object>>>> responseType = new ParameterizedTypeReference<ResultWrap<List<Map<String, Object>>>>(){};
- ResponseEntity<ResultWrap<List<Map<String, Object>>>> response = restTemplate.exchange(getUrl() + "/v1/exec", HttpMethod.POST, request, responseType);
- ResultWrap<List<Map<String, Object>>> result = response.getBody();
- if (result.isSuccess()) {
- return result.getContent();
- }
- throw new RuntimeException(result.getMessage());
- }
- /**
- * 查询单行
- * @param exec
- * @param args
- * @return
- */
- public Map<String, Object> queryForMap(String exec, Object... args){
- List<Map<String, Object>> data = queryForList(exec, args);
- if (!CollectionUtils.isEmpty(data)) {
- return data.get(0);
- }
- return null;
- }
- /**
- * 查询单行,返回值的数组
- * @param exec
- * @param args
- * @return
- */
- public Object[] queryForArray(String exec, Object... args){
- Map<String, Object> data = queryForMap(exec, args);
- if (null != data) {
- return data.values().toArray();
- }
- return null;
- }
- /**
- * 查询,返回单个字段的值
- * @param exec
- * @param args
- * @return
- */
- public Object getObject(String exec, Object... args){
- Map<String, Object> data = queryForMap(exec, args);
- if (null != data) {
- return data.values().iterator().next();
- }
- return null;
- }
- /**
- * 查询,返回单个Integer类型的值
- * @param exec
- * @param args
- * @return
- */
- public int getInt(String exec, Object... args){
- Object data = getObject(exec, args);
- if (null != data) {
- return (int) data;
- }
- return 0;
- }
- /**
- * 查询,返回单个Double类型的值
- * @param exec
- * @param args
- * @return
- */
- public double getDouble(String exec, Object... args){
- Object data = getObject(exec, args);
- if (null != data) {
- return (double) data;
- }
- return 0D;
- }
- /**
- * 查询,返回单个Float类型的值
- * @param exec
- * @param args
- * @return
- */
- public float getFloat(String exec, Object... args){
- Object data = getObject(exec, args);
- if (null != data) {
- return (float) data;
- }
- return 0F;
- }
- /**
- * 查询,返回单个String类型的值
- * @param exec
- * @param args
- * @return
- */
- public String getString(String exec, Object... args){
- Object data = getObject(exec, args);
- if (null != data) {
- return data.toString();
- }
- return null;
- }
- /**
- * 统计行
- * @param exec
- * @param args
- * @return
- */
- public int count(String exec, Object... args){
- if (!exec.toLowerCase().startsWith("select ")) {
- exec = "select count(1) " + exec;
- }
- return getInt(exec, args);
- }
- /**
- * 取序列值
- * @param sequenceName
- * @return
- */
- public int generate(String sequenceName) {
- return getInt("select " + sequenceName + ".nextval from dual");
- }
- /**
- * 取编号值
- * @param sequenceName
- * @param type
- * @return
- */
- public String generateCode(String sequenceName, int type) {
- return callForString("{call Sp_GetMaxNumber(?,?,?)}", sequenceName, type);
- }
- /**
- * 查询对象列表
- * @param <T>
- * @return
- */
- public <T> List<T> queryForBeanList(String exec, Class<T> targetCls, Object... args) {
- HttpEntity request = new HttpEntity<Executable>(Executable.query(exec, BeanUtil.getProperties(targetCls), args));
- String resultStr = restTemplate.postForObject(getUrl() + "/v1/exec", request, String.class);
- // json工具对超过两层的嵌套泛型无法解析,用ResultListWrap<T>替代ResultWrap<List<T>>
- ResultListWrap<T> result = JSON.parseObject(resultStr, new TypeReference<ResultListWrap<T>>(targetCls){});
- if (result.isSuccess()) {
- return result.getContent();
- }
- throw new RuntimeException(result.getMessage());
- }
- /**
- * 查询对象
- * @param <T>
- * @return
- */
- public <T> T queryForBean(String exec, Class<T> targetCls, Object... args) {
- List<T> beans = queryForBeanList(exec, targetCls, args);
- if (!CollectionUtils.isEmpty(beans)) {
- return beans.get(0);
- }
- return null;
- }
- /**
- * call procedure
- * @param exec call语句
- * @param args in参数
- * @param outParams out参数
- * @return
- */
- public Map<String, Object> callForMap(String exec, Object[] args, String[] outParams){
- HttpEntity request = new HttpEntity<Executable>(Executable.call(exec, outParams, args));
- ParameterizedTypeReference<ResultWrap<List<Map<String, Object>>>> responseType = new ParameterizedTypeReference<ResultWrap<List<Map<String, Object>>>>(){};
- ResponseEntity<ResultWrap<List<Map<String, Object>>>> response = restTemplate.exchange(getUrl() + "/v1/exec", HttpMethod.POST, request, responseType);
- ResultWrap<List<Map<String, Object>>> result = response.getBody();
- if (result.isSuccess()) {
- List<Map<String, Object>> list = result.getContent();
- if (!CollectionUtils.isEmpty(list)) {
- return list.get(0);
- }
- return null;
- }
- throw new RuntimeException(result.getMessage());
- }
- /**
- * call procedure and return string
- * @param exec
- * @param args
- * @return
- */
- public String callForString(String exec, Object... args) {
- Map<String, Object> data = callForMap(exec, args, new String[]{"v_return"});
- Object code = null == data ? null : data.get("v_return");
- return null == code ? null : code.toString();
- }
- public <T> List<Long> queryIdList(String sql, Class<T> elementType) {
- HttpEntity request = new HttpEntity<Executable>(Executable.query(sql, BeanUtil.getProperties(elementType), null));
- String resultStr = restTemplate.postForObject(getUrl() + "/v1/exec", request, String.class);
- ResultListWrap<IdObject> result = JSON.parseObject(resultStr, new TypeReference<ResultListWrap<IdObject>>(elementType){});
- List<Long> list = new ArrayList<>();
- if (!CollectionUtils.isEmpty(result.getContent())) {
- for (IdObject object : result.getContent()) {
- list.add(object.getId());
- }
- }
- return list;
- }
- }
|