Application:
Use spring's jdbctemplate to query the database, get a list of lists of results, database table fields and entity classes automatically correspond, you can use beanpropertyrowmapper.
Note:
Automatic binding requires the same column name as the Java entity class name, such as: The property name "UserName" can match the column field "UserName" or "user_name" in the database. In this way, we do not need a manual binding, greatly improving the development efficiency.
Org.springframework.jdbc.core.JdbcTemplate's Query method:
Org.springframework.jdbc.core.JdbcTemplate.query (String sql, object[) args,
rowmapper<UserEntity> RowMapper) throws DataAccessException
public class
Beanpropertyrowmapper<T> implements
RowMapper<T> Note: beanpropertyrowmapper Implements the RowMapper interface
Reprint Please specify: http://blog.csdn.net/limenghua9112/article/details/45096437
Query Code:
@Overridepublic list<userentity> finduser (userentity user) {logger.info ("query statement:" + sel_by_username_pwd);/* * Query given SQL to create a prepared statement from SQL and a list of * arguments to bind to the query, mapping each row t o a Java object via * a RowMapper. *//* * In Spring 2.5, comes with a handy rowmapper implementation (implementation) called * ' Beanpropertyrowmapper ', which can maps a r OW ' s column value to a * property by matching their names. Just Make sure both the property * and column have the same name, e.g Property ' custId ' would match to * column name ' CustId ' or with underscores (bottom line) ' cust_id '. */list<userentity> userlist = Jdbctemplate.query (sel_by_username_pwd,new object[] {user.getUserName (), User.getpwd ()},new beanpropertyrowmapper<userentity> (Userentity.class)); return userlist;}
Sql:
private static final String sel_by_username_pwd = "SELECT * from" + Constantlist.t_shuju_admin_user + "as SP WHERE sp.us Ername =? and sp.pwd =? ";
Spring jdbctemplate query, using Beanpropertyrowmapper