springに基づいて過程の小さいツール種類を貯蓄します.

6964 ワード

一つの記憶プロセスにとって
  • 着信パラメータ
  • プロセス自体
  • 実行結果:結果は、すべての結果セット(ResultSet)と、引数(Out)を含む.戻り値は、特殊な引数(Out)
  • であると考えられてもよい.
    データベースsybaseおよび類似の挙動を示すデータベース格納プロセスを適用します.
    ツールコード、Tは結果の対象の汎型で、Pは着信パラメータの汎型です.
    
    import java.sql.CallableStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import org.springframework.dao.DataAccessException;
    import org.springframework.jdbc.core.CallableStatementCallback;
    import org.springframework.jdbc.support.JdbcUtils;
    
    public abstract class SpCallableStatementCallback<T> implements CallableStatementCallback<T> {
    
    	@Override
    	public final T doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException {
    		cs.execute();
    		T t= this.creatResultObject();
    		int resultSetNum = 0;
    		while (cs.getMoreResults()) {
    			handleResultSet(resultSetNum++, cs, t);
    		}
    		this.handleOutParameter(cs, t);
    		return t;
    	}
    	
    	private void handleResultSet(int resultSetNum, CallableStatement cs, T t) throws SQLException {
    		ResultSet rs = cs.getResultSet();
    		int rowNum = 0;
    		while(rs.next()){
    			this.handleResultSetMapping(resultSetNum, rs, rowNum++, t);
    		}
    		JdbcUtils.closeResultSet(rs);
    	}
    	
    	protected abstract T creatResultObject();
    	
    	// int resultSetNum,                ,  Num       index。
    	protected abstract void handleResultSetMapping(int resultSetNum, ResultSet rs, int rowNum, T t) throws SQLException;
    	
    	protected abstract void handleOutParameter(CallableStatement cs, T t) throws SQLException;
    }
    
    
    
    
    import java.sql.CallableStatement;
    import java.sql.Connection;
    import java.sql.SQLException;
    
    import org.springframework.jdbc.core.CallableStatementCreator;
    import org.springframework.jdbc.core.JdbcTemplate;
    
    public abstract class StoreProcedureCallWithParamThreadSafe<P, T> extends SpCallableStatementCallback<T> {
    	
    	private JdbcTemplate jdbcTemplate;
    	
    	public StoreProcedureCallWithParamThreadSafe() {
    	}
    	
    	public StoreProcedureCallWithParamThreadSafe(JdbcTemplate jdbcTemplate) {
    		this.jdbcTemplate = jdbcTemplate;
    	}
    	
    	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    		this.jdbcTemplate = jdbcTemplate;
    	}
    	
    	protected abstract CallableStatement createCallableStatement(Connection con, P inParamObj) throws SQLException ;
    
    	protected CallableStatementCreator createCallableStatement(final P inParamObj) {
    		return new CallableStatementCreator() {
    			@Override
    			public CallableStatement createCallableStatement(Connection con) throws SQLException {
    				return StoreProcedureCallWithParamThreadSafe.this.createCallableStatement(con, inParamObj);
    			}
    		};
    	}
    	
    	public T call(P inputParameterObject){
    		return jdbcTemplate.execute(this.createCallableStatement(inputParameterObject), this);
    	}
    	
    }
    
    
    Sampleを使う
    データベース作成記憶プロセスは、2つの着信パラメータ、1つの引数、3つの結果セット(それぞれ1行の結果のみ)、および1つの戻り値99.
    
    CREATE PROCEDURE dbo.mathtutor2 
    @mult1 int, @mult2 int, @result int output 
    as 
    select @result = @mult1 * @mult2
    select 10
    select 20
    select 30
    return 99
    
    パラメータオブジェクト
    
    public class TempSpInParam {
    	private int a;
    	private int b;
    
    	public TempSpInParam(int a, int b) {
    		super();
    		this.a = a;
    		this.b = b;
    	}
    
    	@Override
    	public String toString() {
    		return "TempSpIn [a=" + a + ", b=" + b + "]";
    	}
    
    	public int getA() {
    		return a;
    	}
    
    	public void setA(int a) {
    		this.a = a;
    	}
    
    	public int getB() {
    		return b;
    	}
    
    	public void setB(int b) {
    		this.b = b;
    	}
    
    }
    
    実行結果オブジェクト
    
    public class TempSpResult {
    	private int returnVal;
    	private int outVal;
    	private List<Integer> list = new ArrayList<>();
    
    	@Override
    	public String toString() {
    		return "SpObj [returnVal=" + returnVal + ", outVal=" + outVal
    				+ ", list=" + list + "]";
    	}
    
    	public int getReturnVal() {
    		return returnVal;
    	}
    
    	public void setReturnVal(int returnVal) {
    		this.returnVal = returnVal;
    	}
    
    	public int getOutVal() {
    		return outVal;
    	}
    
    	public void setOutVal(int outVal) {
    		this.outVal = outVal;
    	}
    
    	public List<Integer> getList() {
    		return list;
    	}
    
    	public void setList(List<Integer> list) {
    		this.list = list;
    	}
    
    }
    
    プロセス自体のオブジェクト
    
    public class TempSp extends StoreProcedureCallWithParamThreadSafe<TempSpInParam, TempSpResult>{
    	// 
    	private String spSql = "{? = call tempdb..mathtutor2 (?,?,?)}";
    	
    	@Override
    	protected CallableStatement createCallableStatement(Connection con, TempSpInParam inParamObj) throws SQLException {
    		CallableStatement cs = con.prepareCall(spSql);
    		cs.registerOutParameter(1, java.sql.Types.INTEGER);
    		cs.setInt(2, inParamObj.getA());
    		cs.setInt(3, inParamObj.getB());
    		cs.registerOutParameter(4, java.sql.Types.INTEGER);
    		return cs;
    	}
    	
    	@Override
    	protected TempSpResult creatResultObject() {
    		return new TempSpResult();
    	}
    	
    	//          t list 。        ,         resultSetNum   case switch,           。
    	@Override
    	protected void handleResultSetMapping(int resultSetNum, ResultSet rs, int rowNum, TempSpResult t) throws SQLException {
    		System.out.println(String.format("resultSetNum=%s",  resultSetNum));
    		t.getList().add(rs.getInt(1));
    	}
    
    	@Override
    	protected void handleOutParameter(CallableStatement cs, TempSpResult t) throws SQLException {
    		t.setReturnVal(cs.getInt(1));
    		t.setOutVal(cs.getInt(4));
    	}
    	
    }
    
    Testクラス
    
    @RunWith(SpringJUnit4ClassRunner.class)
    public class TestSample extends GenericTestCase {
    
    	@Autowired
    	protected JdbcTemplate jdbcTemplate;
    
    	@Test
    	public void test1() {
    		TempSp tempSp = new TempSp();
    		tempSp.setJdbcTemplate(jdbcTemplate);
    		System.out.println(tempSp.call(new TempSpInParam(2, 3)));
    	}
    	
    }
    
    実行結果
    レスルトンSetNum=0
    レスルトンSetNum=1
    レスルトンSetNum=2
    SpObj[returnVal=99,outVal=6,list=[10,20,30]