修改操作
StudentDao接口
int upd(Student student);
StudentDaoImpl类
@Override
public int upd(Student student) {
String sql= "update students set name=:name where id=:id";
Map param = new HashMap<>();
param.put("id",student.getId());
param.put("name",student.getName());
return (int) jdbcTemplate.update(sql,param);
}
controller层
public String hello(){
Student s = new Student();
s.setId(103);
s.setName("小鬼");
studentDao.upd(s);
return "hello";
}
删除操作
StudentDao接口
int del(int id);
StudentDaoImpl类
@Override
public int del(int id) {
String sql= "delete from students where id=:id";
Map param = new HashMap<>();
param.put("id",id);
return jdbcTemplate.update(sql,param);
}
controller层
public String hello(){
studentDao.del(107);
return "hello";
}
运行步骤见上一篇文章
查询操作(无参)
StudentDao接口
List select();
StudentDaoImpl类
@Override
public List select() {
String sql = "select*from students";
List list = jdbcTemplate.query(sql, new RowMapper() {
@Override
public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
Student student = new Student();
//想要查询的字段
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getString("age"));
return student;
}
});
for(Student student:list){
System.out.println(student);
}
return null;
}
controller层
public String hello(){
studentDao.select();
return "hello";
}
查询操作(有参)
StudentDao接口
List select(int id);
StudentDaoImpl类
@Override
public List select(int id) {
String sql = "select * from students where id = :id";
Map param = new HashMap<>();
param.put("id",id);
List
controller层
public String hello(){
studentDao.select(109);
return "hello";
}
在浏览器运行之后控制台会显示查询结果



