NamedParameterJdbcTemplate常用方法总结

时间:2023-01-06 17:21:34

数据库结构

NamedParameterJdbcTemplate常用方法总结

1.插入/修改/删除数据,使用update方法

插入数据1

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);

插入数据2

API: int update(String sql, SqlParameterSource paramSource)

使用 BeanPropertySqlParameterSource作为参数

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));

插入数据3

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);

2.查询

BeanPropertySqlParameterSource 这个类可以把参数包装,

BeanPropertyRowMapper 这个类可以把结果包装,

2.1返回单行单列数据

API: public < T > T queryForObject(String sql, Map< String, ?> paramMap, Class< T> requiredType)

示例:

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

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

示例:

String name = template.queryForObject( "select name from student where home_address like :addr limit 1 ", new MapSqlParameterSource("addr", "%成都%"), String.class); 

2.2 返回 (多行)单列 数据

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);

2.3 返回单行数据

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)

示例:

       StudentDTO studentDTO = template.queryForObject(
                "select name from student limit 1", new HashMap<>(), new BeanPropertyRowMapper<StudentDTO>(StudentDTO.class));
        //BeanPropertyRowMapper会把下划线转化为驼峰属性
        //结果对象可比实际返回字段多或者少
        //也可以手动编写RowMapper,但比较麻烦

2.4 返回map形式的单行数据

API: public Map< String, Object> queryForMap(String sql, Map< String, ?> paramMap) throws DataAccessException

API: public Map< String, Object> queryForMap(String sql, SqlParameterSource paramSource) throws DataAccessException

示例:

 Map< String, Object> studentMap = template.queryForMap("select * from student limit 1", new HashMap<>());

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) throws DataAccessException {

示例:

        List< StudentDTO> studentDTOList = template.query(
                "select * from student",  new BeanPropertyRowMapper< StudentDTO>(StudentDTO.class));
        System.out.println(studentDTOList);

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<>());