1. 程式人生 > >spring的jdbctemplate與 namedjdbctemplate的區別

spring的jdbctemplate與 namedjdbctemplate的區別

1、呼叫儲存過程不一樣
2、後者能夠用具名引數

        sql="{ call \"sp_account_member_fetch\"(?)}"; -- jdbctemplate的呼叫方式
        sql=" select * from \"sp_account_member_fetch\"(:id)";--namedjdbctemplate的使用方式

引數設定不一樣:
1、
比如說,這個是jdbctemplate的呼叫儲存過程方式:

 public PagerResult<Member> getPager(
            int pageindex,
            int pagesize,
            Integer cnd_id_eq,
            Integer cnd_region_id_eq,
            String cnd_name_like,
            String cnd_email_like,
            String cnd_mobile_eq,
            Timestamp cnd_regist_time_min,
            Timestamp cnd_regist_time_max,
            String cnd_wxopenid_eq,
            String cnd_unionid_eq,
            String sort_column,
            Boolean sort_by_asc

    ){
        int theParamIndex=0;
        PagerResult<Member> pagerResult=new PagerResult<>();
        pagerResult=jdbcTemplate.execute(new CallableStatementCreator() {
            @Override
            public CallableStatement createCallableStatement(Connection con) throws SQLException {

                String sql="";
                sql="{ call \"sp_account_member_getPager\"(?,?,?,?,?,?,?,?,?,?,?,?,?)}";
                CallableStatement st=con.prepareCall(sql);
                int paraIndex=1;
                st.setInt(paraIndex,pageindex);

                paraIndex++;
                st.setInt(paraIndex,pagesize);

                paraIndex++;
                st.setInt(paraIndex,cnd_id_eq);

                paraIndex++;
                st.setInt(paraIndex,cnd_region_id_eq);

                paraIndex++;
                st.setString(paraIndex,cnd_name_like);

                paraIndex++;
                st.setString(paraIndex,cnd_email_like);

                paraIndex++;
                st.setString(paraIndex,cnd_mobile_eq);

                paraIndex++;
                st.setTimestamp(paraIndex,cnd_regist_time_min);

                paraIndex++;
                st.setTimestamp(paraIndex,cnd_regist_time_max);

                paraIndex++;
                st.setString(paraIndex,cnd_wxopenid_eq);

                paraIndex++;
                st.setString(paraIndex,cnd_unionid_eq);


                paraIndex++;
                st.setString(paraIndex,sort_column);

                paraIndex++;
                st.setBoolean(paraIndex,sort_by_asc);

                return st;
            }
        },new CallableStatementCallback<PagerResult<Member>>(){
            @Override
            public PagerResult<Member> doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException {
                PagerResult<Member> pager=new PagerResult<>();
                List<Member> list=new ArrayList<>();
                cs.execute();
                ResultSet rs=(ResultSet)cs.getResultSet();
                if (rs.next()){
                    String json_str=rs.getString(1);
                    if(ValidateUtils.isEmpty(json_str)){
                        return pager;
                    }
                    pager=JSONObject.parseObject(json_str,PagerResult.class);
                }
                if (rs.next()){
                    String json_str=rs.getString(1);
                    if(ValidateUtils.isEmpty(json_str)){

                    }
                    else{
                        list=JSONObject.parseArray(json_str,Member.class);
                    }
                    pager.setData(new ArrayList<>());
                    for(Member item:list){
                        pager.getData().add(item);
                    }
                }
                rs.close();
                cs.getConnection().setAutoCommit(true);
                return pager;
            }
        });
        return pagerResult;
    }

而具名jdbctemplate則可以這樣呼叫:

 @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public PagerResult<Member> getPager(
            int pageindex,
            int pagesize,
            Integer cnd_id_eq,
            Integer cnd_region_id_eq,
            String cnd_name_like,
            String cnd_email_like,
            String cnd_mobile_eq,
            Timestamp cnd_regist_time_min,
            Timestamp cnd_regist_time_max,
            String cnd_wxopenid_eq,
            String cnd_unionid_eq,
            String sort_column,
            Boolean sort_by_asc

    ){
        int theParamIndex=0;
        PagerResult<Member> pagerResult=new PagerResult<>();


        String sql="";
        sql="{ call \"sp_account_member_getPager\"(?,?,?,?,?,?,?,?,?,?,?,?,?)}";
        sql="select * from \"sp_account_member_getPager\"(:pageindex,\n" +
                "            :pagesize,\n" +
                "            :cnd_id_eq,\n" +
                "            :cnd_region_id_eq,\n" +
                "            :cnd_name_like,\n" +
                "            :cnd_email_like,\n" +
                "            :cnd_mobile_eq,\n" +
                "            :cnd_regist_time_min,\n" +
                "            :cnd_regist_time_max,\n" +
                "            :cnd_wxopenid_eq,\n" +
                "            :cnd_unionid_eq,\n" +
                "            :sort_column,\n" +
                "            :sort_by_asc)";
        MapSqlParameterSource parameterSource=new MapSqlParameterSource();
        parameterSource.addValue("pageindex",pageindex);
        parameterSource.addValue("pagesize",pagesize);
        parameterSource.addValue("cnd_id_eq",cnd_id_eq);
        parameterSource.addValue("cnd_region_id_eq",cnd_region_id_eq);
        parameterSource.addValue("cnd_name_like",cnd_name_like);
        parameterSource.addValue("cnd_email_like",cnd_email_like);
        parameterSource.addValue("cnd_mobile_eq",cnd_mobile_eq);
        parameterSource.addValue("cnd_regist_time_min",cnd_regist_time_min);
        parameterSource.addValue("cnd_regist_time_max",cnd_regist_time_max);
        parameterSource.addValue("cnd_wxopenid_eq",cnd_wxopenid_eq);
        parameterSource.addValue("cnd_unionid_eq",cnd_unionid_eq);
        parameterSource.addValue("sort_column",sort_column);
        parameterSource.addValue("sort_by_asc",sort_by_asc);
        
        
        pagerResult=namedParameterJdbcTemplate.execute(sql, parameterSource, new PreparedStatementCallback<PagerResult<Member>>() {
            @Override
            public PagerResult<Member> doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
                PagerResult<Member> pager=new PagerResult<>();
                List<Member> list=new ArrayList<>();
                ps.execute();
                ResultSet rs=(ResultSet)ps.getResultSet();
                if (rs.next()){
                    String json_str=rs.getString(1);
                    if(ValidateUtils.isEmpty(json_str)){
                        return pager;
                    }
                    pager=JSONObject.parseObject(json_str,PagerResult.class);
                }
                if (rs.next()){
                    String json_str=rs.getString(1);
                    if(ValidateUtils.isEmpty(json_str)){

                    }
                    else{
                        list=JSONObject.parseArray(json_str,Member.class);
                    }
                    pager.setData(new ArrayList<>());
                    for(Member item:list){
                        pager.getData().add(item);
                    }
                }
                rs.close();
                ps.getConnection().setAutoCommit(true);
                return pager;
            }
        });

        return pagerResult;
    }