How to call a custom Oracle function returning a value from JPA

后端 未结 5 488
暗喜
暗喜 2021-01-14 01:23

I have a custom function in oracle returning a number after deleting records. I could get a value in sql_plus such as

call my_function(4) into :out_number;
<         


        
5条回答
  •  一整个雨季
    2021-01-14 02:06

    When using Spring Data JPA / Spring JDBC you can use SimpleJdbcCall for calling functions and procedures.

    For such function header:

    FUNCTION get_text (p_param IN VARCHAR2) RETURN VARCHAR2
    

    Use this call:

    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
    import org.springframework.jdbc.core.namedparam.SqlParameterSource;
    import org.springframework.jdbc.core.simple.SimpleJdbcCall;
    import org.springframework.stereotype.Repository;
    
    @Repository
    public class MyRepository {
    
        @Autowired
        private JdbcTemplate jdbcTemplate;
    
        @Override
        public String callGetTextFunction(String param) {
            SimpleJdbcCall jdbcCall = new SimpleJdbcCall(jdbcTemplate)
                    .withCatalogName("PKG_PACKAGE") //package name
                    .withFunctionName("GET_TEXT"); //function name
            SqlParameterSource paramMap = new MapSqlParameterSource()
                    .addValue("p_param", param));
            //First parameter is function output parameter type.
            return jdbcCall.executeFunction(String.class, paramMap));
        }
    
    }
    

提交回复
热议问题