Results 1 to 3 of 3

Thread: Spring 3 Dao - Calling Stored Procedure

  1. #1

    Default Spring 3 Dao - Calling Stored Procedure

    Hi,

    I am new to Spring mvc 3. Just a toddler.
    I would like to have pointers on how to call a stored procedure in Spring MVC 3.
    Examples would be really appreciated.
    Please forgive my ignorance.

  2. #2
    Join Date
    Jan 2009
    Location
    Huntington Beach, CA
    Posts
    718

    Default

    Well you wouldn't want to call a backend database stored procedure from Spring MVC.

    I'll explain. Spring MVC is the module of Spring Framework for building Web application layer. Meaning the layer that is only responsible for receive an HttpRequest getting the info out of the request, then delegating to other layers to do the work. So typically calling down to a Service/business logic layer, which then calls down to the DAO layer. The service and DAO layer can exist in any type of application using the SpringFramework and not even use or touch Spring MVC.

    So I think your first confusion is that Spring MVC is the Spring Framework instead a module of the Spring Framework.

    Now, to have a Repository/DAO call a Stored Procedure that is more a likely question.

    In Spring there is a class called StoredProcedure that you extend and set up the IN/OUT parameters in the constructor and then overwrite a method execute to execute the stored procedure.

    Here's an example I found online

    Code:
    class ActStartled extends StoredProcedure {
      private static final String SQL = "p_proc";
    
      ActStartled(DataSource dataSource) {
          super(dataSource, SQL);
          declareParameter(new SqlParameter("n", Types.INTEGER));
      }
    
      void execute(int value) {
          Map<String, Object> parameters = new HashMap<String, Object>(1);
          parameters.put("n", value);
          execute(parameters);
      }
    }
    Now that I showed how to do it. I will also recommend, that unless this is a stored procedure that has existed for a really long time and you are forced to use it. Do not use stored procedures in your application as you will be tying logic into the database, which tends to be too much coupling and locks you into that database vendor.

    Good Luck

    Mark

  3. #3

    Default

    Thank you Mark.
    It helped. Now I have a better understanding.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •