The Namedparameterjdbctemplate class is based on the JdbcTemplate class and encapsulates it to support named parameter attributes.
Namedparameterjdbctemplate provides the following three main methods: Execute method, Query and Queryforxxx method, update and BatchUpdate method.
First, let's look at an example:
Java code:Java Code
- @Test
- Public void TestNamedParameterJdbcTemplate1 () {
- Namedparameterjdbctemplate namedparameterjdbctemplate = null;
- Namedparameterjdbctemplate =
- New Namedparameterjdbctemplate (DataSource);
- Namedparameterjdbctemplate =
- New Namedparameterjdbctemplate (JdbcTemplate);
- String insertsql = "INSERT into test (name) VALUES (: Name)";
- String selectsql = "SELECT * from Test where name=:name";
- String deletesql = "Delete from Test where name=:name";
- map<string, object> parammap = new hashmap<string, object> ();
- Parammap.put ("name", "Name5");
- Namedparameterjdbctemplate.update (Insertsql, Parammap);
- final list<integer> result = new arraylist<integer> ();
- Namedparameterjdbctemplate.query (Selectsql, Parammap,
- New RowCallbackHandler () {
- @Override
- public void Processrow (ResultSet rs) throws SQLException {
- Result.add (Rs.getint ("id"));
- }
- });
- Assert.assertequals (1, result.size ());
- Sqlparametersource Paramsource = new Mapsqlparametersource (PARAMMAP);
- Namedparameterjdbctemplate.update (Deletesql, Paramsource);
- }
Now let's analyze the code:
1) namedparameterjdbctemplate initialization : can use DataSource or JdbcTemplate object as constructor parameter initialization;
2) insert into test (name) values: where ": Name" is the named parameter;
3) Update (Insertsql, Parammap): where Parammap is a map type that contains key-value pairs with the key "name" and a value of "Name5", which is the data that sets the value for the named parameter;
4) query (Selectsql, Parammap, New RowCallbackHandler () ...) : similar to the one described in JdbcTemplate, the only difference is the need to pass in Parammap to set a value for a named parameter;
5) Update (Deletesql, Paramsource): similar to "Update (Insertsql, Parammap)", but use the Sqlparametersource parameter to set a value for the named parameter, This is implemented here using the Mapsqlparametersource, which is the simple encapsulation java.util.Map.
The Namedparameterjdbctemplate class has two ways of setting values for named parameters: Java.util.Map and Sqlparametersource:
1) Java.util.Map: Use the map key data for named parameters, while map value data is used to set values;
2) Sqlparametersource: You can use the Sqlparametersource implementation as a parameter to set values for named parameters, The default is Mapsqlparametersource and Beanpropertysqlparametersource implementations; the Mapsqlparametersource implementation is very simple, Just encapsulates the JAVA.UTIL.MAP; Beanpropertysqlparametersource encapsulates a JavaBean object that determines the value of a named parameter by JavaBean object properties.
Java code:Java Code
- Package cn.javass.spring.chapter7;
- Public class Usermodel {
- private int id;
- private String MyName;
- //Omit getter and setter
- }
Java code:Java Code
- @Test
- Public void TestNamedParameterJdbcTemplate2 () {
- Namedparameterjdbctemplate namedparameterjdbctemplate = null;
- Namedparameterjdbctemplate = new Namedparameterjdbctemplate (JdbcTemplate);
- Usermodel model = new Usermodel ();
- Model.setmyname ("Name5");
- String insertsql = "INSERT into test (name) VALUES (: MyName)";
- Sqlparametersource Paramsource = new Beanpropertysqlparametersource (model);
- Namedparameterjdbctemplate.update (Insertsql, Paramsource);
- }
You can see that beanpropertysqlparametersource use can reduce a lot of effort, but the named parameter must correspond to the JavaBean property name.
Spring namedparameterjdbctemplate (ext.)