版權聲明:本文為博主原創文章,未經博主允許不得轉載!如需轉載請聲明:【轉自 http://blog.csdn.net/u011179993 】 https://blog.csdn.net/u011179993/article/details/74791304
NamedParameterJdbcTemplate類拓展了JdbcTemplate類,對JdbcTemplate類進行了封裝從而支持命名參數特性。
NamedParameterJdbcTemplate主要提供以下三類方法:execute方法、query及queryForXXX方法、update及batchUpdate方法。
數據庫結構
1.支持類
SqlParameterSource 簡介
可以使用SqlParameterSource實現作為來實現為命名參數設值,默認實現有 :
MapSqlParameterSource實現非常簡單,只是封裝了java.util.Map;
BeanPropertySqlParameterSource封裝了一個JavaBean對象,通過JavaBean對象屬性來決定命名參數的值。
EmptySqlParameterSource 一個空的SqlParameterSource ,常用來占位使用
RowMapper簡介
這個接口為了實現sql查詢結果和對象間的轉換,可以自己實現,也可以使用系統實現,主要實現類有:
SingleColumnRowMapper ,sql結果為一個單列的數據,如List<String> , List<Integer>,String,Integer等
BeanPropertyRowMapper, sql結果匹配到對象 List< XxxVO> , XxxVO
2.插入/修改/刪除數據,使用updateXXX方法
使用Map作為參數
API: int update(String sql, Map<String, ?> paramMap)
示例:
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
);
1
2
3
4
5
6
7
8
9
10
使用BeanPropertySqlParameterSource作為參數
API: int update(String sql, SqlParameterSource paramSource)
使用 BeanPropertySqlParameterSource作為參數
public class StudentDTO{
private String id;
private String name;
private String homeAddress;
//getter,setter
}
1
2
3
4
5
6
7
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));
1
2
3
4
5
6
7
使用MapSqlParameterSource 作為參數
API: int update(String sql, SqlParameterSource paramSource)
//使用 MapSqlParameterSource 作為參數
MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource()
.addValue("id", UUID.randomUUID().toString())
.addValue("name", "小王")
.addValue("homeAddress", "美國");
template.update("insert into student(id,name,home_address) values
(:id,:name,:homeAddress)",mapSqlParameterSource);
1
2
3
4
5
6
或者
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("id", UUID.randomUUID().toString());
paramMap.put("name", "小明");
paramMap.put("homeAddress", "樂山");
//---------------
MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource(paramMap);
1
2
3
4
5
6
3.查詢
返回單行單列數據
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)
示例(注意EmptySqlParameterSource的使用):
Integer count = template.queryForObject(
"select count(*) from student", new HashMap<>(), Integer.class);
1
2
String name = template.queryForObject( "select name from student where home_address limit 1 ", EmptySqlParameterSource.INSTANCE, String.class);
1
返回 (多行)單列 數據
API: public < T> List< T> queryForList(String sql, Map<String, ?> paramMap, Class< T > elementType)
API: public < T> List< T> queryForList(String sql, SqlParameterSource paramSource, Class< T> elementType)
示例:
List< String> namelist = template.queryForList("select name from student", new HashMap<>(), String.class);
1
返回單行數據
API: public < T> T queryForObject(String sql, Map< String, ?> paramMap, RowMapper< T>rowMapper)
API: public < T> T queryForObject(String sql, SqlParameterSource paramSource, RowMapper< T> rowMapper)
示例:
Student stu = template.queryForObject(
"select * from student limit 1", new HashMap<>(), new BeanPropertyRowMapper<Student>(Student.class));
//BeanPropertyRowMapper會把下划線轉化為駝峰屬性
//結果對象可比實際返回字段多或者少
1
2
3
4
注意:這兩個API也可以使用SingleColumnRowMapper返回單行單列數據
String name = template.queryForObject(
"select name from student limit 1", EmptySqlParameterSource.INSTANCE, new SingleColumnRowMapper<>(String.class));
1
2
2.4 返回Map形式的單行數據
API: public Map< String, Object> queryForMap(String sql, Map< String, ?> paramMap)
API: public Map< String, Object> queryForMap(String sql, SqlParameterSource paramSource)
示例:
Map< String, Object> studentMap = template.queryForMap("select * from student limit 1", new HashMap<>());
1
2.5 返回多行數據
API: public < T> List< T> query(String sql, Map< String, ?> paramMap, RowMapper< T> rowMapper)
API: public < T> List< T> query(String sql, SqlParameterSource paramSource, RowMapper< T> rowMapper)
API: public < T> List< T> query(String sql, RowMapper< T> rowMapper)
示例:
List< Student> studentList = template.query(
"select * from student",
new BeanPropertyRowMapper<>(Student.class)
);
1
2
3
4
同理,也可以使用SingleColumnRowMapper返回單行列表List< String>,List< Integer>等
2.6 返回多行數據(Map)
API: public List< Map< String, Object>> queryForList(String sql, Map< String, ?> paramMap)
API: public List< Map< String, Object>> queryForList(String sql, SqlParameterSource paramSource)
示例:
List<Map<String, Object>> mapList = template.queryForList(
"select * from student", new HashMap<>());
1
2
總結
開發中盡量使用NamedParameterJdbcTemplate代替JdbcTemplate,如果想使用JdbcTemplate,也可以通過NamedParameterJdbcTemplate#getJdbcOperations()獲取
不建議使用查詢結構為Map的API
---------------------
作者:chenjazz
來源:CSDN
原文:https://blog.csdn.net/u011179993/article/details/74791304
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!
