瀏覽代碼

调用存储过程BUG

yingp 8 年之前
父節點
當前提交
f21aa31247

+ 11 - 0
src/main/java/com/uas/erp/database/domain/Executable.java

@@ -15,6 +15,8 @@ public class Executable {
     private Object[] args;
     private Object[] args;
     // 是否查询
     // 是否查询
     private boolean isQuery;
     private boolean isQuery;
+    // 是否call procedure
+    private boolean isCall;
     // 返回字段
     // 返回字段
     private String[] returnFields;
     private String[] returnFields;
 
 
@@ -58,6 +60,14 @@ public class Executable {
         this.returnFields = returnFields;
         this.returnFields = returnFields;
     }
     }
 
 
+    public boolean isCall() {
+        return isCall;
+    }
+
+    public void setCall(boolean call) {
+        isCall = call;
+    }
+
     @Override
     @Override
     public String toString() {
     public String toString() {
         return "Executable{" +
         return "Executable{" +
@@ -65,6 +75,7 @@ public class Executable {
                 ", execs=" + Arrays.toString(execs) +
                 ", execs=" + Arrays.toString(execs) +
                 ", args=" + Arrays.toString(args) +
                 ", args=" + Arrays.toString(args) +
                 ", isQuery=" + isQuery +
                 ", isQuery=" + isQuery +
+                ", isCall=" + isCall +
                 ", returnFields=" + Arrays.toString(returnFields) +
                 ", returnFields=" + Arrays.toString(returnFields) +
                 '}';
                 '}';
     }
     }

+ 37 - 0
src/main/java/com/uas/erp/database/repository/BaseRepository.java

@@ -1,14 +1,18 @@
 package com.uas.erp.database.repository;
 package com.uas.erp.database.repository;
 
 
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.dao.DataAccessException;
 import org.springframework.dao.EmptyResultDataAccessException;
 import org.springframework.dao.EmptyResultDataAccessException;
 import org.springframework.jdbc.core.BeanPropertyRowMapper;
 import org.springframework.jdbc.core.BeanPropertyRowMapper;
+import org.springframework.jdbc.core.CallableStatementCallback;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.jdbc.core.RowMapper;
 import org.springframework.jdbc.core.RowMapper;
 import org.springframework.stereotype.Repository;
 import org.springframework.stereotype.Repository;
 
 
+import java.sql.CallableStatement;
 import java.sql.ResultSet;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.SQLException;
+import java.sql.Types;
 import java.util.HashMap;
 import java.util.HashMap;
 import java.util.List;
 import java.util.List;
 import java.util.Map;
 import java.util.Map;
@@ -95,4 +99,37 @@ public class BaseRepository {
         }
         }
     }
     }
 
 
+    public Map<String, Object> callForMap(String exec, final Object[] vars, final String[] outParams) {
+        try {
+            return jdbcTemplate.execute(exec, new CallableStatementCallback<Map<String, Object>>() {
+                @Override
+                public Map<String, Object> doInCallableStatement(CallableStatement stmt) throws SQLException, DataAccessException {
+                    int i = 1;
+                    if (null != vars) {
+                        for (Object var : vars) {
+                            stmt.setObject(i++, var);
+                        }
+                    }
+                    int j = i;
+                    if (null != outParams) {
+                        for (String param : outParams) {
+                            stmt.registerOutParameter(i++, Types.VARCHAR);
+                        }
+                    }
+                    stmt.execute();
+                    if (null != outParams) {
+                        Map<String, Object> data = new HashMap<>();
+                        for (String param : outParams) {
+                            data.put(param, stmt.getObject(j++));
+                        }
+                        return data;
+                    }
+                    return null;
+                }
+            });
+        } catch (EmptyResultDataAccessException e) {
+            return null;
+        }
+    }
+
 }
 }

+ 16 - 2
src/main/java/com/uas/erp/database/service/ExecuteService.java

@@ -6,7 +6,9 @@ import com.uas.erp.database.domain.Master;
 import com.uas.erp.database.repository.BaseRepository;
 import com.uas.erp.database.repository.BaseRepository;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
+import org.springframework.util.StringUtils;
 
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Arrays;
 import java.util.List;
 import java.util.List;
 import java.util.Map;
 import java.util.Map;
@@ -25,8 +27,20 @@ public class ExecuteService {
         String[] execs = executable.getExecs();
         String[] execs = executable.getExecs();
         if (null != execs && execs.length > 0) {
         if (null != execs && execs.length > 0) {
             if (execs.length == 1) {
             if (execs.length == 1) {
-                if (executable.isQuery()) {
-                    String[] returnFields = executable.getReturnFields();
+                String[] returnFields = executable.getReturnFields();
+                // 支持存储过程
+                if (executable.isCall()) {
+                    if (null != returnFields && returnFields.length > 0) {
+                        Map<String, Object> data = baseRepository.callForMap(execs[0], executable.getArgs(),
+                                executable.getReturnFields());
+                        List<Map<String, Object>> list = new ArrayList<>();
+                        list.add(data);
+                        return list;
+                    } else {
+                        baseRepository.execute(execs[0], executable.getArgs());
+                        return null;
+                    }
+                } else if (executable.isQuery()) {
                     if (null != returnFields && returnFields.length > 0) {
                     if (null != returnFields && returnFields.length > 0) {
                         return baseRepository.queryForFieldsList(execs[0], returnFields, executable.getArgs());
                         return baseRepository.queryForFieldsList(execs[0], returnFields, executable.getArgs());
                     }
                     }

+ 10 - 5
src/test/java/com/uas/erp/test/MasterTest.java

@@ -1,5 +1,6 @@
 package com.uas.erp.test;
 package com.uas.erp.test;
 
 
+import com.alibaba.fastjson.JSON;
 import com.uas.erp.database.UasDatabaseApplication;
 import com.uas.erp.database.UasDatabaseApplication;
 import com.uas.erp.database.domain.Executable;
 import com.uas.erp.database.domain.Executable;
 import com.uas.erp.database.domain.Master;
 import com.uas.erp.database.domain.Master;
@@ -16,6 +17,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 import org.springframework.util.Assert;
 import org.springframework.util.Assert;
 
 
 import java.util.List;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import java.util.Set;
 
 
 /**
 /**
@@ -37,8 +39,8 @@ public class MasterTest {
 //    @Before
 //    @Before
     public void testScanMasters() {
     public void testScanMasters() {
         DBA dba = new DBA();
         DBA dba = new DBA();
-        dba.setUrl("jdbc:oracle:thin:@192.168.253.12:1521:orcl");
-        dba.setUsername("UAS");
+        dba.setUrl("jdbc:oracle:thin:@202.104.134.72:1522:orcl");
+        dba.setUsername("ODLF");
         dba.setPassword("select!#%*(");
         dba.setPassword("select!#%*(");
         dbaService.save(dba);
         dbaService.save(dba);
         masterService.scanAll();
         masterService.scanAll();
@@ -49,9 +51,12 @@ public class MasterTest {
         Set<Master> masters = masterService.findAll(false);
         Set<Master> masters = masterService.findAll(false);
         Assert.notEmpty(masters);
         Assert.notEmpty(masters);
         Executable executable = new Executable();
         Executable executable = new Executable();
-        executable.setQuery(true);
-        executable.setExecs(new String[]{ "select 1 from dual" });
-        executeService.execute(masters.iterator().next(), executable);
+        executable.setCall(true);
+        executable.setExecs(new String[]{ "{call Sp_GetMaxNumber(?,?,?)}" });
+        executable.setArgs(new Object[]{"AcceptNotify", 2});
+        executable.setReturnFields(new String[]{"v_ReturnStr"});
+        List<Map<String, Object>> result = executeService.execute(masters.iterator().next(), executable);
+        System.out.println(JSON.toJSONString(result));
     }
     }
 
 
 }
 }