我一直在使用 Spring JDBC 的 SimpleJdbcCall。我觉得很有帮助,但对我来说仍然不够简单和流畅。理想情况下,我认为 JDBC 调用与普通的 Java 方法调用没有区别。
看到最近Java出现了很多轻量级的ORM框架(比如Ebean),想问一下存储过程方向有没有类似的发展?
我一直在使用 Spring JDBC 的 SimpleJdbcCall。我觉得很有帮助,但对我来说仍然不够简单和流畅。理想情况下,我认为 JDBC 调用与普通的 Java 方法调用没有区别。
看到最近Java出现了很多轻量级的ORM框架(比如Ebean),想问一下存储过程方向有没有类似的发展?
Calling stored procedures from Java through JDBC or JPA can be very tedious and verbose. If you look a the examples provided by EclipseLink, things haven't gotten that much simpler with JPA 2.1:
@NamedStoredProcedureQuery(
name="ReadUsingMultipleResultSetMappings",
procedureName="Read_Multiple_Result_Sets",
resultSetMappings = {
"EmployeeResultSetMapping",
"AddressResultSetMapping",
"ProjectResultSetMapping",
"EmployeeConstructorResultSetMapping"
}
)
@SqlResultSetMappings({
@SqlResultSetMapping(
name = "EmployeeResultSetMapping",
entities = {
@EntityResult(entityClass = Employee.class)
}
),
@SqlResultSetMapping(
name = "EmployeeConstructorResultSetMapping",
classes = {
@ConstructorResult(
targetClass = EmployeeDetails.class,
columns = {
@ColumnResult(name="EMP_ID", type=Integer.class),
@ColumnResult(name="F_NAME", type=String.class),
@ColumnResult(name="L_NAME", type=String.class),
@ColumnResult(name="R_COUNT", type=Integer.class)
}
)
}
)
})
A good option is to leverage a code generator, such as the one by jOOQ (I work for the jOOQ vendor, so this answer is biased). Assume this procedure:
-- Check whether there is an author in AUTHOR by that name and get his ID
CREATE OR REPLACE PROCEDURE author_exists (
author_name VARCHAR2,
result OUT NUMBER,
id OUT NUMBER
);
Using generated code, you can simply call it as such:
AuthorExists result = Routines.authorExists(configuration, "Paulo");
assertEquals(new BigDecimal("1"), result.getResult());
assertEquals(new BigDecimal("2"), result.getId();
See also the jOOQ manual for details:
Other code generators exist and can be leveraged to generate the tedious JDBC or JPA code.
可以使用 @NamedStoredProcedureQuery 和其他方法从 JPA 调用存储过程。例如,您可以使用 eclipselink。http://wiki.eclipse.org/EclipseLink/Examples/JPA/StoredProcedures
希望能帮助到你。