Spring's Namedparameterjdbctemplate

Source: Internet
Author: User

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
  1. @Test
  2. Public void TestNamedParameterJdbcTemplate1 () {
  3. Namedparameterjdbctemplate namedparameterjdbctemplate = null;
  4. Namedparameterjdbctemplate =
  5. New Namedparameterjdbctemplate (DataSource);
  6. Namedparameterjdbctemplate =
  7. New Namedparameterjdbctemplate (JdbcTemplate);
  8. String insertsql = "INSERT into test (name) VALUES (: Name)";
  9. String selectsql = "SELECT * from Test where name=:name";
  10. String deletesql = "Delete from Test where name=:name";
  11. map<string, object> parammap = new hashmap<string, object> ();
  12. Parammap.put ("name", "Name5");
  13. Namedparameterjdbctemplate.update (Insertsql, Parammap);
  14. final list<integer> result = new arraylist<integer> ();
  15. Namedparameterjdbctemplate.query (Selectsql, Parammap,
  16. New RowCallbackHandler () {
  17. @Override
  18. public void Processrow (ResultSet rs) throws SQLException {
  19. Result.add (Rs.getint ("id"));
  20. }
  21. });
  22. Assert.assertequals (1, result.size ());
  23. Sqlparametersource Paramsource = new Mapsqlparametersource (PARAMMAP);
  24. Namedparameterjdbctemplate.update (Deletesql, Paramsource);
  25. }

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
    1. Package cn.javass.spring.chapter7;
    2. Public class Usermodel {
    3. private int id;
    4. private String MyName;
    5. //Omit getter and setter
    6. }

Java code:Java Code
  1. @Test
  2. Public void TestNamedParameterJdbcTemplate2 () {
  3. Namedparameterjdbctemplate namedparameterjdbctemplate = null;
  4. Namedparameterjdbctemplate = new Namedparameterjdbctemplate (JdbcTemplate);
  5. Usermodel model = new Usermodel ();
  6. Model.setmyname ("Name5");
  7. String insertsql = "INSERT into test (name) VALUES (: MyName)";
  8. Sqlparametersource Paramsource = new Beanpropertysqlparametersource (model);
  9. Namedparameterjdbctemplate.update (Insertsql, Paramsource);
  10. }

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.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.