JdbcTemplate query will automatically insinuate the list of queries into the entity list

Source: Internet
Author: User

The Spring API doc is described below:

RowMapper implementation that converts a row into a new instance of the specified mapped target class. The mapped target class must be a top-level class and it must has a default or No-arg constructor.

Column values is mapped based on matching the column name as obtained from result set metadata to public setters for the corresponding properties. The names is matched either directly or by transforming a name separating the parts with underscores to the same name Usi ng "Camel" case.

Mapping is provided for fields in the target class for many common types, e.g.: String, Boolean, Boolean, Byte, Byte, Shor T, short, int, Integer, long, long, float, float, double, double, BigDecimal, java.util.Date, etc.

To facilitate mapping between columns and fields that don ' t has matching names, try using column aliases in the SQL state ment like "Select FName as First_Name from customer".

Designed to provide convenience rather than high performance. For best performance consider using a custom RowMapper.


It also says that it can automatically map the fields of ResultSet and entity classes.

A concrete example is as follows:

If there is such a table, the sql-server2000 script for the table is as follows:

Code

/* Administrator table */CREATE table admin (id int identity (primary) key, username varchar () Not NULL, password VA Rchar (+) not NULL,)

To do this, we write a corresponding entity class admin, which is a standard JavaBean with the following code:

Code

/**  *   */  package db.demo;      /**   *  @author  xq *   *  @version  8:11:57 pm  *    */  public class Admin {           private int id;          private  string username;          private string  password;            public int  GetId ()  {                   return id;          }             public void setid (Int id)  {                   this.id = id;           }             public string getusername ()  {                   return username;           }            public  void setusername (String username)  {                   this.username = username;           }             public string getpassword ()  {                   return password;          }             public void setpassword ( String password)  {                   this.password = password;           }  }

Previously, in the corresponding Admindao, we used to do this, it looks very troublesome, if a table of many fields, it will be human life, we have to keep the set, get:

Code

/**  *   */  package db.demo;    import  java.sql.resultset;  import java.sql.sqlexception;  import java.util.list;     import org.springframework.jdbc.core.RowMapper;  import  org.springframework.jdbc.core.support.jdbcdaosupport;    /**  *  @author  xq *   *  @version  10:05:37 PM  *   */   public class AdminDAO extends JdbcDaoSupport {             private final String ID =  "ID";           private final String USERNAME =  " Username ";          private final string  password =  "PASSWORD";           private final string table_name =  "Admin ";            /**           *  Query Total Records <br/>           */          public List<Admin>  Queryall ()  {                   final String sql =  "select * from "  + table_ name;                                      return getjdbctemplate (). Query (Sql, new rowmapper () {                             public  Object maprow (Resultset rs, int rownum)  throws SQLException {                                    admin admin =  new admin ();                                    admin.setid (Rs.getint (ID));                                    admin.setusername (rs.getstring (USERNAME));                                    admin.setpassword (rs.getstring (PASSWORD));                                    return admin;                            }                                              });           }  }

Visible, we have to manually map resultset and admin. And now, we just need this:

Code

/**  *   */  package db.demo;    import  java.util.list;    import org.springframework.jdbc.core.beanpropertyrowmapper;   import org.springframework.jdbc.core.support.JdbcDaoSupport;    /**   *  @author  xq *   *  @version  10:05:37 PM  *    */  public class AdminDAO extends JdbcDaoSupport {             private final string table_name =   "Admin";            /**           *  Query Total Records <br/>           */          public List<Admin>  queryall ()  {&nbsP;                 final  String sql =  "select * from "  + TABLE_NAME;                                      return  Getjdbctemplate (). Query (Sql, new beanpropertyrowmapper (admin.class));           }  }

Oh, just a sentence is completely done ... Sprin will automatically map for us ... Obviously it's more convenient than before. We can also use it for any other occasion that uses rowmapper ... After all, it inherits from RowMapper ...

It is important to note that Beanpropertyrowmapper is mapped based on the field name and the standard setter method in the entity class. That is, we need to make the name of the field in the table consistent with the member variable names of the entity classes.


JdbcTemplate query will automatically insinuate the list of queries into the entity list

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.