spring jdbc IncorrectResultSizeDataAccessException 简单处理,,java spring
分享于 点击 20300 次 点评:114
spring jdbc IncorrectResultSizeDataAccessException 简单处理,,java spring
java spring jdbc
1.添加一个接口
public interface JdbcTemplateCallBack<T> { public T querys(JdbcTemplate jdbcTemplate);}
2 编写一个公用的查询方法拦截异常
public <T> T queryNullAble(JdbcTemplateCallBack<T> jdbcTemplateCallBack) throws DaoException { try { return jdbcTemplateCallBack.querys(getJdbcTemplate()); } catch (Exception e) { if(e instanceof IncorrectResultSizeDataAccessException){ if(((IncorrectResultSizeDataAccessException)e).getActualSize()==0) return null; } //其他的异常正常抛出 throw new DaoException(e); } }调用实例:return queryNullAble(new JdbcTemplateCallBack<User>() { public User querys(JdbcTemplate jdbcTemplate) { return jdbcTemplate.queryForObject("select * from suser where id=?", new BeanPropertyRowMapper(User.class),value); } });
用户点评