jdbctemplate調用 mysql存儲過程

@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public Member getMember(final String type,final String mobile) {
    final Member member = new Member();
    Member result = (Member) jdbcTemplate.execute(
            new CallableStatementCreator() {
                public CallableStatement createCallableStatement(java.sql.Connection con) throws SQLException {
                    String storedProc = "{call checkvip(?,?,?,?,?,?,?,?,?)}";// 調用的sql
                    CallableStatement cs = con.prepareCall(storedProc);
                    cs.setInt(1,Integer.valueOf(type));// 設置輸入參數的值
                    cs.setString(2, mobile);// 設置輸入參數的值
                    cs.registerOutParameter(3, Types.INTEGER);// 註冊輸出參數的類型
                    cs.registerOutParameter(4, Types.VARCHAR);
                    cs.registerOutParameter(5, Types.VARCHAR);
                    cs.registerOutParameter(6, Types.VARCHAR);
                    cs.registerOutParameter(7, Types.BIT);
                    cs.registerOutParameter(8, Types.VARCHAR);
                    cs.registerOutParameter(9, Types.VARCHAR);
                    return cs;
                }
            }, new CallableStatementCallback() {
                public Object doInCallableStatement(CallableStatement cs) throws SQLException,DataAccessException {
                    cs.execute();
                    String csString = cs.getString(3);
                    if (csString.equals("0")) {
                        return null;
                    }else{
                        member.setCode(cs.getString(5));
                        member.setUsername(cs.getString(6));
                        String s = cs.getString(7);
                        if (s.equals("0")) {
                            member.setGender(Member.Gender.male);
                        }else{
                            member.setGender(Member.Gender.female);
                        }
                        member.setMobile(cs.getString(8));
                        member.setEmail(cs.getString(9));
                        return member;
                    }
                }
            });
    return result;
}
相關文章
相關標籤/搜索