Spring JdbcTemplate study notes (2) - common methods of jdbcTemplate

Inquire

queryForObject

//About the queryForObject method of jdbcTemplate. Let's analyze them one by one:
After the spring 3.2.2 version, jdbcTemplate.queryForInt() and jdbcTemplate.queryForLong() were canceled, and they were all replaced by queryForObject, which will not be explained in detail here.

queryForObject(sql, requiredType)

Essentially the same as queryForInt, but can return different objects, such as returning a String object
with 2 parameters, 1, sql 2, the returned object class

String sql = "select count(*) from user";
Integer num = jdbcTemplate.queryForObject(sql, Integer.class); 

This cannot be directly mapped to an entity class, such as

User user = jdbcTemplate.queryForObject(sql, User.class);

An error will be reported when running.
If you want to map to a specific entity, you can use the ORM framework, or the methods described later.

queryForObject(sql, requiredType, args…)

The third parameter is a variable parameter

String sql = "select count(*) from user where ID<? AND ID>?";
jdbcTemplate.queryForObject(sql, Integer.class,4,2 );

queryForObject(sql, args[], requiredType)

The second parameter is an array of parameters

jdbcTemplate.queryForObject(sql,new Integer[]{5,1}, Integer.class)

Different types of parameters can also be used

String sql = "select count(*) from user where ID>? AND USER_NAME LIKE ?";

jdbcTemplate.queryForObject(sql, new Object[]{1,"%哈%"},Integer.class);

queryForObject(sql, rowMapper)

Note that the query here must ensure that only one piece of data can be queried, otherwise an error will be reported.

String sql = "select * from user WHERE ID = 1";
User user = jdbcTemplate.queryForObject(sql, new RowMapper<User>(){
@Override
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
    User user = new User();
    user.setId(rs.getInt("ID"));
    user.setUserName(rs.getString("USER_NAME"));
    return user;
}
});

The parameters are also the same as above, two methods.

queryForList

Returns a list with maps, each map is a record, and the key in the map is the field name

List<Map<String, Object>> rows = jdbcTemplate.queryForList("SELECT * FROM user");  

//get the list with map

for(int i=0;i<rows.size();i++){     //遍历    
Map userMap=rows.get(i);    
System.out.println(userMap.get("id"));      
System.out.println(userMap.get("name"));      
System.out.println(userMap.get("age"));    
}   

Ibid.

queryForMap

//这个查询只能是查询一条记录的查询,返回一个map,key的值是column的值    
Map<String, Object> map = jdbcTemplate.queryForMap("select * from user");    
map.get("name ");

queryForRowSet

//Return a result set and then call .getString or getInt to get the value

The query method is not explained in detail, it is not commonly used, and the queryForXXX method is basically sufficient.

renew

execute

Execute sql statement, no return execution, used for update operations (add, delete, modify)
jdbcTemplate.execute(sql);

update

Update operation, returns the number of rows affected
Int rowNum = jdbcTemplate.update(sql);

batchUpdate

Execute batch update, parameter is string array
String[] sql = new String[2];
sql[0] = "";
sql[1] = "";
jdbcTemplate.batchUpdate(sql);
//Update, args is object array
jdbcTemplate.update(sql, args[]);

/*
Description:
How to use JDBCTemplate:
define a jdbcTemplate node in ApplicationContext.xml, use POJO injection, you can perform operations after getting the injection,
no need to inherit any base class
*/

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326685240&siteId=291194637