Methods commonly used NamedParameterJdbcTemplate

Reprinted from: [] Transfer from http://blog.csdn.net/u011179993

 

Database structure 


1. Support class
SqlParameterSource Profile
can be used to achieve SqlParameterSource implemented as a set value for the parameter named default implementation are:

MapSqlParameterSource implementation is very simple, but encapsulates a java.util.Map;

BeanPropertySqlParameterSource encapsulates a JavaBean object named parameter value is determined by JavaBean object properties.

EmptySqlParameterSource an empty SqlParameterSource, used to use placeholder

RowMapper About
this interface in order to switch between sql query results and objects, you can achieve your own, you can also use the system to achieve the main achievement category are:

SingleColumnRowMapper, sql result is a single row of data, such as List <String>, List <Integer>, String, Integer, etc.

BeanPropertyRowMapper, sql result matches the object to List <XxxVO>, XxxVO

2. Insert / modify / delete data, using updateXXX method
using a Map as an argument
API: int update (String sql, Map <String,?> ParamMap)

Example:
 

Map<String, Object> paramMap = new HashMap<>();
paramMap.put("id", UUID.randomUUID().toString());
paramMap.put("name", "小明");
paramMap.put("age", 33);
paramMap.put("homeAddress", "乐山");
paramMap.put("birthday", new Date());
template.update( 
     "insert into student(id,name,age,home_address,birthday) values (:id,:name,:age,:homeAddress,:birthday)",
     paramMap
);

 

BeanPropertySqlParameterSource use as an argument

API: int update(String sql, SqlParameterSource paramSource)

BeanPropertySqlParameterSource use as an argument

public class StudentDTO{
    private String id;
    private String name;
    private String homeAddress;

     //getter,setter
}

 

StudentDTO dto=new StudentDTO();//这个DTO为传入数据
dto.setId(UUID.randomUUID().toString());
dto.setName("小红");
dto.setHomeAddress("成都");
//------------------------------
template.update("insert into student(id,name,home_address) values (:id,:name,:homeAddress)",
                new BeanPropertySqlParameterSource(dto));

 

Map<String, Object> paramMap = new HashMap<>();
paramMap.put("id", UUID.randomUUID().toString());
paramMap.put("name", "小明");
paramMap.put("homeAddress", "乐山");
//---------------
MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource(paramMap);

3. query
returns single separate data
API: public <T> T queryForObject (String sql, Map <String,?> ParamMap, Class <T> requiredType)

API: public < T > T queryForObject(String sql, SqlParameterSource paramSource, Class<T> requiredType)

Example (note the use EmptySqlParameterSource):
 

Integer count = template.queryForObject(
                "select count(*) from student", new HashMap<>(), Integer.class);


 

String name = template.queryForObject( "select name from student where home_address  limit 1 ", EmptySqlParameterSource.INSTANCE, String.class); 

 

Published 23 original articles · won praise 7 · views 20000 +

Guess you like

Origin blog.csdn.net/ghd602338792/article/details/95984995