单元测试spring中的jdbcTemplate调用存储过程详解
准备过程- //建表
- create table TEST_USERS
- (
- USER_ID VARCHAR2(10) not null,
- NAME VARCHAR2(10) not null,
- PASSWORD VARCHAR2(20) not null
- )
- 准备如下数据测试
- USER_ID=test1 name=aa password=aa
- USER_ID=test2 name=bb password=bb
- USER_ID=test3 name=cc password=cc
- // 创建存储过程
- create or replace procedure display_users_proc(results_out out SYS_REFCURSOR,
- userId in test_users.user_id%type) is
- begin
- if userId is not null then
- open results_out for
- select * from test_users where user_id like userId || '%';
- else
- open results_out for
- select * from test_users;
- end if;
- end display_users_proc;
复制代码- public class ProcCallableStatementCreator implements CallableStatementCreator {
- private String storedProc;
- private String params;
-
-
- /**
- * Constructs a callable statement.
- * @param storedProc The stored procedure's name.
- * @param params Input parameters.
- * @param outResultCount count of output result set.
- */
- public ProcCallableStatementCreator(String storedProc, String params) {
- this.params = params;
- this.storedProc = storedProc;
- }
-
- /**
- * Returns a callable statement
- * @param conn Connection to use to create statement
- * @return cs A callable statement
- */
- public CallableStatement createCallableStatement(Connection conn) {
- StringBuffer storedProcName = new StringBuffer("call ");
- storedProcName.append(storedProc + "(");
- //set output parameters
- storedProcName.append("?");
- storedProcName.append(", ");
-
- //set input parameters
- storedProcName.append("?");
- storedProcName.append(")");
- CallableStatement cs = null;
- try {
- // set the first parameter is OracleTyep.CURSOR for oracel stored procedure
- cs = conn.prepareCall(storedProcName.toString());
- cs.registerOutParameter (1, OracleTypes.CURSOR);
- // set the sencond paramter
- cs.setObject(2, params);
- }
- catch (SQLException e)
- {
- throw new RuntimeException("createCallableStatement method Error : SQLException " + e.getMessage());
- }
- return cs;
- }
- }
复制代码- public class ProcCallableStatementCallback implements CallableStatementCallback {
- /**
- * Constructs a ProcCallableStatementCallback.
- */
- public ProcCallableStatementCallback() {
- }
- /**
- * Returns a List(Map) collection.
- * @param cs object that can create a CallableStatement given a Connection
- * @return resultsList a result object returned by the action, or null
- */
- public Object doInCallableStatement(CallableStatement cs)
- {
- List<Map> resultsMap = new ArrayList<Map>();
- try
- {
- cs.execute();
- ResultSet rs = (ResultSet) cs.getObject(1);
- while (rs.next())
- {
- Map<String, String> rowMap = new HashMap<String, String>();
- rowMap.put("userId", rs.getString("USER_ID"));
- rowMap.put("name", rs.getString("NAME"));
- rowMap.put("password", rs.getString("PASSWORD"));
- resultsMap.add(rowMap);
- }
- rs.close();
- }catch(SQLException e)
- {
- throw new RuntimeException("doInCallableStatement method error : SQLException " + e.getMessage());
- }
- return resultsMap;
- }
- }
复制代码- public class SpringStoredProce extends JdbcDaoSupport{
- @SuppressWarnings("unchecked")
- public List<Map> execute(String storedProc, String params){
- List<Map> resultList = null;
- try
- {
- final DataSource ds = getDataSource();
- final JdbcTemplate template = new JdbcTemplate(ds);
- resultList = (List<Map>)template.execute(new ProcCallableStatementCreator(storedProc, params), new ProcCallableStatementCallback());
- }catch(DataAccessException e)
- {
- throw new RuntimeException("execute method error : DataAccessException " + e.getMessage());
- }
- return resultList;
- }
- }
复制代码- public class SpringStoredProceTest {
-
- private SpringStoredProce springStoredProce;
-
- /**
- * @throws java.lang.Exception
- */
- @Before
- public void setUp() throws Exception {
- springStoredProce = new SpringStoredProce();
- }
-
- /**
- * @throws java.lang.Exception
- */
- @After
- public void tearDown() throws Exception {
- springStoredProce = null;
- }
-
- /**
- * Test method
- */
- @Test
- public void testExecute() {
- final String storedProcName = "display_users_proc";
- final String param = "test";
- List<Map> resultList = springStoredProce.execute(storedProcName, param);
- assertNotNull(resultList);
- assertTrue(resultList.size() > 0);
- for (int i = 0; i < resultList.size(); i++)
- {
- Map rowMap = resultList.get(i);
- final String userId = rowMap.get("userId").toString();
- final String name = rowMap.get("name").toString();
- final String password = rowMap.get("password").toString();
- System.out.println("USER_ID=" + userId + "\t name=" + name + "\t password=" + password);
- }
-
- }
- }
复制代码- 单元测试 最后运行结果 (注意准备插入几条记录进去测试)
- [code]
- USER_ID=test1 name=aa password=aa
- USER_ID=test2 name=bb password=bb
- USER_ID=test3 name=cc password=cc
复制代码
[ 本帖最后由 heyitang 于 2010-5-26 16:50 编辑 ] |