Revised code examples for stored procedure type declarations
Issue: SPR-16811
This commit is contained in:
@@ -2982,7 +2982,6 @@ in the primitive wrapper classes explicitly or using auto-boxing.
|
|||||||
[subs="verbatim,quotes"]
|
[subs="verbatim,quotes"]
|
||||||
----
|
----
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
|
||||||
public class ExecuteAnUpdate {
|
public class ExecuteAnUpdate {
|
||||||
@@ -4023,7 +4022,7 @@ data from the `t_actor` relation to an instance of the `Actor` class.
|
|||||||
|
|
||||||
public ActorMappingQuery(DataSource ds) {
|
public ActorMappingQuery(DataSource ds) {
|
||||||
super(ds, "select id, first_name, last_name from t_actor where id = ?");
|
super(ds, "select id, first_name, last_name from t_actor where id = ?");
|
||||||
super.declareParameter(new SqlParameter("id", Types.INTEGER));
|
declareParameter(new SqlParameter("id", Types.INTEGER));
|
||||||
compile();
|
compile();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4044,7 +4043,7 @@ for this customer query takes the `DataSource` as the only parameter. In this
|
|||||||
constructor you call the constructor on the superclass with the `DataSource` and the SQL
|
constructor you call the constructor on the superclass with the `DataSource` and the SQL
|
||||||
that should be executed to retrieve the rows for this query. This SQL will be used to
|
that should be executed to retrieve the rows for this query. This SQL will be used to
|
||||||
create a `PreparedStatement` so it may contain place holders for any parameters to be
|
create a `PreparedStatement` so it may contain place holders for any parameters to be
|
||||||
passed in during execution.You must declare each parameter using the `declareParameter`
|
passed in during execution. You must declare each parameter using the `declareParameter`
|
||||||
method passing in an `SqlParameter`. The `SqlParameter` takes a name and the JDBC type
|
method passing in an `SqlParameter`. The `SqlParameter` takes a name and the JDBC type
|
||||||
as defined in `java.sql.Types`. After you define all parameters, you call the
|
as defined in `java.sql.Types`. After you define all parameters, you call the
|
||||||
`compile()` method so the statement can be prepared and later executed. This class is
|
`compile()` method so the statement can be prepared and later executed. This class is
|
||||||
@@ -4097,9 +4096,7 @@ class since it can easily be parameterized by setting SQL and declaring paramete
|
|||||||
[subs="verbatim"]
|
[subs="verbatim"]
|
||||||
----
|
----
|
||||||
import java.sql.Types;
|
import java.sql.Types;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.springframework.jdbc.core.SqlParameter;
|
import org.springframework.jdbc.core.SqlParameter;
|
||||||
import org.springframework.jdbc.object.SqlUpdate;
|
import org.springframework.jdbc.object.SqlUpdate;
|
||||||
|
|
||||||
@@ -4178,9 +4175,7 @@ output parameter, in this case only one, using the parameter name as the key.
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.jdbc.core.SqlOutParameter;
|
import org.springframework.jdbc.core.SqlOutParameter;
|
||||||
import org.springframework.jdbc.object.StoredProcedure;
|
import org.springframework.jdbc.object.StoredProcedure;
|
||||||
@@ -4227,14 +4222,13 @@ Oracle REF cursors).
|
|||||||
[source,java,indent=0]
|
[source,java,indent=0]
|
||||||
[subs="verbatim,quotes"]
|
[subs="verbatim,quotes"]
|
||||||
----
|
----
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import javax.sql.DataSource;
|
||||||
import oracle.jdbc.OracleTypes;
|
import oracle.jdbc.OracleTypes;
|
||||||
import org.springframework.jdbc.core.SqlOutParameter;
|
import org.springframework.jdbc.core.SqlOutParameter;
|
||||||
import org.springframework.jdbc.object.StoredProcedure;
|
import org.springframework.jdbc.object.StoredProcedure;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class TitlesAndGenresStoredProcedure extends StoredProcedure {
|
public class TitlesAndGenresStoredProcedure extends StoredProcedure {
|
||||||
|
|
||||||
private static final String SPROC_NAME = "AllTitlesAndGenres";
|
private static final String SPROC_NAME = "AllTitlesAndGenres";
|
||||||
@@ -4264,12 +4258,10 @@ the supplied `ResultSet`:
|
|||||||
[source,java,indent=0]
|
[source,java,indent=0]
|
||||||
[subs="verbatim,quotes"]
|
[subs="verbatim,quotes"]
|
||||||
----
|
----
|
||||||
import org.springframework.jdbc.core.RowMapper;
|
|
||||||
|
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
import com.foo.domain.Title;
|
import com.foo.domain.Title;
|
||||||
|
import org.springframework.jdbc.core.RowMapper;
|
||||||
|
|
||||||
public final class TitleMapper implements RowMapper<Title> {
|
public final class TitleMapper implements RowMapper<Title> {
|
||||||
|
|
||||||
@@ -4288,12 +4280,10 @@ the supplied `ResultSet`.
|
|||||||
[source,java,indent=0]
|
[source,java,indent=0]
|
||||||
[subs="verbatim,quotes"]
|
[subs="verbatim,quotes"]
|
||||||
----
|
----
|
||||||
import org.springframework.jdbc.core.RowMapper;
|
|
||||||
|
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
import com.foo.domain.Genre;
|
import com.foo.domain.Genre;
|
||||||
|
import org.springframework.jdbc.core.RowMapper;
|
||||||
|
|
||||||
public final class GenreMapper implements RowMapper<Genre> {
|
public final class GenreMapper implements RowMapper<Genre> {
|
||||||
|
|
||||||
@@ -4311,17 +4301,15 @@ delegate to the superclass' untyped `execute(Map parameters)` method (which has
|
|||||||
[source,java,indent=0]
|
[source,java,indent=0]
|
||||||
[subs="verbatim,quotes"]
|
[subs="verbatim,quotes"]
|
||||||
----
|
----
|
||||||
import oracle.jdbc.OracleTypes;
|
|
||||||
import org.springframework.jdbc.core.SqlOutParameter;
|
|
||||||
import org.springframework.jdbc.core.SqlParameter;
|
|
||||||
import org.springframework.jdbc.object.StoredProcedure;
|
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
|
|
||||||
import java.sql.Types;
|
import java.sql.Types;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import oracle.jdbc.OracleTypes;
|
||||||
|
import org.springframework.jdbc.core.SqlOutParameter;
|
||||||
|
import org.springframework.jdbc.core.SqlParameter;
|
||||||
|
import org.springframework.jdbc.object.StoredProcedure;
|
||||||
|
|
||||||
public class TitlesAfterDateStoredProcedure extends StoredProcedure {
|
public class TitlesAfterDateStoredProcedure extends StoredProcedure {
|
||||||
|
|
||||||
@@ -4416,6 +4404,7 @@ dependency injection.
|
|||||||
final File clobIn = new File("large.txt");
|
final File clobIn = new File("large.txt");
|
||||||
final InputStream clobIs = new FileInputStream(clobIn);
|
final InputStream clobIs = new FileInputStream(clobIn);
|
||||||
final InputStreamReader clobReader = new InputStreamReader(clobIs);
|
final InputStreamReader clobReader = new InputStreamReader(clobIs);
|
||||||
|
|
||||||
jdbcTemplate.execute(
|
jdbcTemplate.execute(
|
||||||
"INSERT INTO lob_table (id, a_clob, a_blob) VALUES (?, ?, ?)",
|
"INSERT INTO lob_table (id, a_clob, a_blob) VALUES (?, ?, ?)",
|
||||||
new AbstractLobCreatingPreparedStatementCallback(lobHandler) { # <1>
|
new AbstractLobCreatingPreparedStatementCallback(lobHandler) { # <1>
|
||||||
@@ -4426,6 +4415,7 @@ dependency injection.
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
blobIs.close();
|
blobIs.close();
|
||||||
clobReader.close();
|
clobReader.close();
|
||||||
----
|
----
|
||||||
@@ -4512,21 +4502,24 @@ declaration of an `SqlOutParameter`.
|
|||||||
[source,java,indent=0]
|
[source,java,indent=0]
|
||||||
[subs="verbatim,quotes"]
|
[subs="verbatim,quotes"]
|
||||||
----
|
----
|
||||||
final TestItem = new TestItem(123L, "A test item",
|
public class TestItemStoredProcedure extends StoredProcedure {
|
||||||
new SimpleDateFormat("yyyy-M-d").parse("2010-12-31"));
|
|
||||||
|
|
||||||
declareParameter(new SqlOutParameter("item", OracleTypes.STRUCT, "ITEM_TYPE",
|
public TestItemStoredProcedure(DataSource dataSource) {
|
||||||
new SqlReturnType() {
|
...
|
||||||
public Object getTypeValue(CallableStatement cs, int colIndx, int sqlType, String typeName) throws SQLException {
|
declareParameter(new SqlOutParameter("item", OracleTypes.STRUCT, "ITEM_TYPE",
|
||||||
STRUCT struct = (STRUCT) cs.getObject(colIndx);
|
new SqlReturnType() {
|
||||||
Object[] attr = struct.getAttributes();
|
public Object getTypeValue(CallableStatement cs, int colIndx, int sqlType, String typeName) throws SQLException {
|
||||||
TestItem item = new TestItem();
|
STRUCT struct = (STRUCT) cs.getObject(colIndx);
|
||||||
item.setId(((Number) attr[0]).longValue());
|
Object[] attr = struct.getAttributes();
|
||||||
item.setDescription((String) attr[1]);
|
TestItem item = new TestItem();
|
||||||
item.setExpirationDate((java.util.Date) attr[2]);
|
item.setId(((Number) attr[0]).longValue());
|
||||||
return item;
|
item.setDescription((String) attr[1]);
|
||||||
}
|
item.setExpirationDate((java.util.Date) attr[2]);
|
||||||
}));
|
return item;
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
...
|
||||||
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
You use the `SqlTypeValue` to pass in the value of a Java object like `TestItem` into a
|
You use the `SqlTypeValue` to pass in the value of a Java object like `TestItem` into a
|
||||||
@@ -4538,7 +4531,7 @@ the following example, or ``ArrayDescriptor``s.
|
|||||||
[source,java,indent=0]
|
[source,java,indent=0]
|
||||||
[subs="verbatim,quotes"]
|
[subs="verbatim,quotes"]
|
||||||
----
|
----
|
||||||
final TestItem = new TestItem(123L, "A test item",
|
final TestItem testItem = new TestItem(123L, "A test item",
|
||||||
new SimpleDateFormat("yyyy-M-d").parse("2010-12-31"));
|
new SimpleDateFormat("yyyy-M-d").parse("2010-12-31"));
|
||||||
|
|
||||||
SqlTypeValue value = new AbstractSqlTypeValue() {
|
SqlTypeValue value = new AbstractSqlTypeValue() {
|
||||||
@@ -6257,7 +6250,6 @@ constructs a Spring application context, and calls these two methods.
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import javax.xml.transform.stream.StreamResult;
|
import javax.xml.transform.stream.StreamResult;
|
||||||
import javax.xml.transform.stream.StreamSource;
|
import javax.xml.transform.stream.StreamSource;
|
||||||
|
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
import org.springframework.oxm.Marshaller;
|
import org.springframework.oxm.Marshaller;
|
||||||
|
|||||||
Reference in New Issue
Block a user