Test Spring stored procedures

xiaoxiao2021-04-08  265

Test Spring's stored procedure Spring encapsulates the storage process. Its implementation details are tested below JDBC 1): Write storage procedure Execute class: package jdbc; import java.sql.types; import java.util.hashmap; import java .util.Map; import javax.sql.DataSource; import org.springframework.jdbc.core.SqlParameter; import org.springframework.jdbc.object.StoredProcedure; import org.springframework.jdbc.core.SqlOutParameter; import org.springframework. jdbc.core.SqlParameter; public class StoreTemplate extends StoredProcedure {HashMap map = new HashMap (); public StoreTemplate () {super ();} public void setValue (String key, Object obj) {map.put (key, obj); } Public storeMplate (Datasource DS) {setDataSource (DS);} public map execute () {if (this.getsql () == null || this.getsql (). Equals (")) Return Null; this.Compile (); return execute (map);} public void setVarcharParam (String param) {this.declareParameter (new SqlParameter (param, Types.VARCHAR));} public void setDoubleParam (String param) {this.declareParameter (new SqlParameter ( param, Types.DOUBLE));} public void setIntegerParam (String param) {this.declareParameter (new SqlParameter (param, Types.INTEGER));} public void setVarcharOutParam (String param) {this.declareParameter (new SqlOutParameter (param, Types.VARCHAR));} public void setDoubleOutParam (String param) {this.declareParameter (new SqlOutParameter (param, Types.DOUBLE));} public void setIntegerOutParam (String param) {this.declareParameter (new SqlOutParameter (param, Types. Integer);}} 2): Configure data sources with Spring:

com.microsoft.jdbc.sqlserver.SQLServerDriver JDBC: Microsoft: SQLServer: //127.0.0.1: 1433; DatabaseName = Pubs; selectMethAME = PUBS; selectMethod = Cursor sa 3): Write junit: package jdbc; import junit .framework *;. import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import java.util.List; import java.util.Map; import javax.sql.DataSource; public class TestStoreTemplate extends TestCase {ApplicationContext ctx = null; protected void setUp () throws Exception {ctx = new FileSystemXmlApplicationContext ( "D: //work//jpetstore//src//jdbc//Context-jdbc.xml");} public void testStore ( ) {DataSource DataSource = (Datasource) CTX .getBean ("Datasource"); StoreTemplate Qry = New StoreMplate (Datasource); QRY.SetSQL ("TestSP"); QRY.setIntegerParam ("count"); QRY.setIntegeroutParam ("Ret"); QRY.SetValue ", new integer (1)); map map = qry.execute (); if (map! = null) {system.out.println (map.get (" RET "));}} protected Exception {}} 4): Write test stored procedure set quoted_identifier on goset ANSI_NULLS ON GOCREATE Procedure TestSp (@count int, @

转载请注明原文地址:https://www.9cbs.com/read-132690.html

New Post(0)