The previous sections speak of the use of JdbcTemplate, this section explains How to use Namedparameterjdbctemplate:
The Namedparameterjdbctemplate class is based on the JdbcTemplate class, and the Namedparameterjdbctemplate contains a jdbctemplate inside, So jdbctemplate can do things namedparameterjdbctemplate are able to do, namedparameterjdbctemplate with respect to jdbctemplate mainly increased the parameters can be named function.
Namedparameterjdbctemplate, mainly provides the following three kinds of methods: Execute method, Query and Queryforxxx method, update and BatchUpdate method.
Spring Configuration JdbcTemplate:
(1) The first step, Spring provides the class org.springframework.jdbc.core.JdbcTemplate, so the bean is injected with the data source object:
<bean id= "Logjdbctemplate" class= "Org.springframework.jdbc.core.JdbcTemplate" >
<property name= "DataSource" ref= "Logdatasource"/>
</bean>
(2) The second step, the DAO class implementation class uses annotations to inject the Bean object logjdbctemplate into the member variable:
@Autowired
protected JdbcTemplate logjdbctemplate;
Spring Configuration namedparameterjdbctemplate:
(1) The first step, Spring provides class org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate, so the bean is used to inject the data source object, However, this is injected using a constructor:
<bean id= "Lognamedjdbctemplate" class= "Org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate" >
<constructor-arg ref= "Logdatasource" ></constructor-arg>
</bean>
(2) The second step, the DAO class implementation class uses annotations to inject the Bean object lognamedjdbctemplate into the member variable:
@Autowired
protected Namedparameterjdbctemplate lognamedjdbctemplate;
@Test Public voidtestNamedParameterJdbcTemplate1 () {namedparameterjdbctemplate namedparameterjdbctemplate=NULL; //namedparameterjdbctemplate =//new Namedparameterjdbctemplate (DataSource); Namedparameterjdbctemplate =Newnamedparameterjdbctemplate (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 =NewHashmap<string, object>(); Parammap.put ("name","Name5"); Namedparameterjdbctemplate.update (Insertsql, Parammap); Final List<Integer> result =NewArraylist<integer>(); Namedparameterjdbctemplate.query (Selectsql, Parammap,NewRowCallbackHandler () {@Override Public voidProcessrow (ResultSet rs) throws SQLException {Result.add (Rs.getint ("ID")); } }); Assert.assertequals (1, Result.size ()); Sqlparametersource Paramsource=NewMapsqlparametersource (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.
Examples of passing SQL parameters using Beanpropertysqlparametersource encapsulation JavaBean objects are as follows:
Package Cn.javass.spring.chapter7; Public class Usermodel { privateint ID; Private String myName; // omit getter and setter }
@Test Public voidTestNamedParameterJdbcTemplate2 () {namedparameterjdbctemplate namedparameterjdbctemplate=NULL; Namedparameterjdbctemplate=Newnamedparameterjdbctemplate (JdbcTemplate); Usermodel Model=NewUsermodel (); Model.setmyname ("Name5"); String Insertsql="INSERT into Test (name) VALUES (: MyName)"; Sqlparametersource Paramsource=NewBeanpropertysqlparametersource (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.
This article references from: http://blog.csdn.net/dyllove98/article/details/7772470
JdbcTemplate Study (iv)