5 Mart 2018 Pazartesi

SpringData StoredProcedure Sınıfı - Stored Procedure Çağırır

Giriş
Şu satırı dahil ederiz.
import org.springframework.jdbc.core.SqlInOutParameter;
import org.springframework.jdbc.core.SqlReturnType;
import org.springframework.jdbc.object.StoredProcedure;
import org.springframework.jdbc.support.SqlValue;
declareParameter metodu
Şöyle yaparız.
declareParameter(new SqlInOutParameter("io_col", Types.CLOB, null,
  new SqlReturnType() {...}
));
execute metodu
Örnek
Elimizde şöyle bir kod olsun
@Log4j2
@Repository
public class MyProcedureRepositoryImpl implements MyProcedureRepository{
  private TestProc testProc;
  private DataSource dataSource;
 
  @Autowired
  public MyProcedureRepositoryImpl(DataSource datasource) {
    this.dataSource = datasource;
    this.testProc = new TestProc();
  }

  @Override
  public String  testProc(Request request) {
    Map<String, Object> responseMap = testProc.execute(request);
    return responseMap.get("result").toString()
 }
}
Şöyle yaparız
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.StoredProcedure;

import org.springframework.util.ObjectUtils;

public class TestProc extends StoredProcedure {
  public TestProc() {
    super(dataSource, "PACKAGE.PROC_TEST");
    declareParameter(new SqlParameter("evnt", Types.VARCHAR));
    declareParameter(new SqlParameter("session", Types.VARCHAR));
    declareParameter(new SqlParameter("key", Types.VARCHAR));
   
    declareParameter(new SqlOutParameter("result", Types.VARCHAR));
    declareParameter(new SqlOutParameter("msg", Types.VARCHAR));
   
    compile();
  }
  @Override
  public Map<String, Object> execute(Request request) {
    Map<String, Object> inParams = new HashMap<>();

    inParams.put("Event", request.getEvent);
    inParams.put("session", request.getEvent);
    inParams.put("key", request.getEvent);

    log.debug("Input params {}", inParams);
    Map<String, Object> outParams = super.execute(inParams);
    log.debug("Output params {}", outParams);
    return outParams;
  }
}




Hiç yorum yok:

Yorum Gönder