jdbi @SqlCall OutParameters 简单例子

oyhk 学习笔记

jdbi 调用存储过程时返回值的用法。在注解的用法时默认是没有返回值的,我们可以通过添加这个注解,从而达到。

首先,我们逃添加以下的一个注解类

@SqlStatementCustomizingAnnotation(OutParameter.Factory.class)
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface OutParameter {

	String name();

	int sqlType();

	static class Factory implements SqlStatementCustomizerFactory {
		@Override
		public SqlStatementCustomizer createForType(Annotation annotation, @SuppressWarnings("rawtypes") Class sqlObjectType) {
			throw new UnsupportedOperationException("Not allowed on Type"); //$NON-NLS-1$
		}

		@Override
		public SqlStatementCustomizer createForMethod(Annotation annotation, @SuppressWarnings("rawtypes") Class sqlObjectType, Method method) {
			final OutParameter outParam = (OutParameter) annotation;
			return new SqlStatementCustomizer() {
				@Override
				public void apply(@SuppressWarnings("rawtypes") SQLStatement q) throws SQLException {
					assert q instanceof Call;
					((Call) q).registerOutParameter(outParam.name(), outParam.sqlType());
				}
			};
		}

		@Override
		public SqlStatementCustomizer createForParameter(Annotation annotation, @SuppressWarnings("rawtypes") Class sqlObjectType, Method method, final Object arg) {
			throw new UnsupportedOperationException("Not defined for parameter"); //$NON-NLS-1$
		}
	}

}

jdbi 注解接口的代码


    @SqlCall(":index = call p_hh_GetVchNumber (2,0,8,:date,'','','')")
    @OutParameter(name="inxex",sqlType = Types.NUMERIC)
    OutParameters createNumberIndex(@Bind("date") String date);
简单的测试用例



        OutParameters outParameter1 = billDao.createNumberIndex("2015-08-22");
        System.out.println("-----------------------------------");
        System.out.println(outParameter1.getShort("index"));
        System.out.println("-------------------------------------");
这样就可以达到想要的效果了,谢谢