How to call Oracle function or stored procedure using spring persistence framework?

后端 未结 4 1493
忘掉有多难
忘掉有多难 2020-12-13 15:57

I am using Spring persistence framework for my project. I want to call oracle function or stored procedure from this framework.

Can anybody suggest how can I achieve

4条回答
  •  伪装坚强ぢ
    2020-12-13 16:57

    Simpler way of calling a Oracle function in Spring is subclassing StoredProcedure like below

    public class MyStoredProcedure extends StoredProcedure{
        private static final String SQL = "package.function";
    
        public MyStoredProcedure(DataSource ds){
            super(ds,SQL);
            declareParameter(new SqlOutParameter("param_out",Types.NUMERIC));
            declareParameter(new SqlParameter("param_in",Types.NUMERIC));
            setFunction(true);//you must set this as it distinguishes it from a sproc
            compile();
        }
    
        public String execute(Long rdsId){
            Map in = new HashMap();
            in.put("param_in",rdsId);
            Map out = execute(in);
            if(!out.isEmpty())
                return out.get("param_out").toString();
            else
                return null;
        }
    }
    

    And call it like this

    @Autowired DataSource ds;
    MyStoredProcedure sp = new MyStoredProcedure(ds);
    String i = sp.execute(1l);
    

    The Oracle function used here just takes in a numeric parameter and returns a numeric paramter.

提交回复
热议问题