一,更新操作
1,修改StudentDaoImpl,添加 更新方法,
@Override
public int updateStudent(Student student) {
String sql ="update t_student set name=? ,age=? where id=?";
Object[] params = new Object[]{student.getName(),student.getAge(),student.getId()};
int i = jdbcTemplate.update(sql,params);
return i;
}
2,修改 StudentServiceImpl类,调用,
@Override
public int updateStudent(Student student) {
return studentDao.updateStudent(student);
}
3,添加Student构造方法,全参构造,包括id,因为在修改方法中,我们需要new 一个带有id的Student
public Student(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
4,测试-运行
package com.cruise.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.cruise.model.Student;
import com.cruise.service.StudentService;
public class T {
public static void main(String[] args) {
ClassPathXmlApplicationContext CPXAC=new ClassPathXmlApplicationContext("beans.xml");
StudentService studentService = (StudentService) CPXAC.getBean("studentService");
studentService.updateStudent(new Student(1,"张三2", 46));
}
}
二,删除操作:
1,修改StudentDaoImpl,添加 删除方法,
@Override
public int deleteStudent(Integer id) {
String sql ="delete from t_student where id=?";
Object[] params = new Object[]{id};
int i = jdbcTemplate.update(sql,params);
return i;
}
2,修改 StudentServiceImpl类,调用,
@Override
public int deleteStudent(Integer id) {
return studentDao.deleteStudent(id);
}
3,测试-运行
package com.cruise.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.cruise.service.StudentService;
public class T {
public static void main(String[] args) {
ClassPathXmlApplicationContext CPXAC=newClassPathXmlApplicationContext("beans.xml");
StudentService studentService = (StudentService) CPXAC.getBean("studentService");
int i = studentService.deleteStudent(1);
if (i==1) {
System.out.println("删除成功");
}
}
}
三,查询所有List操作
1,修改StudentDaoImpl,添加 List方法,涉及到内部类,和final声明
@Override
public List<Student> findStudents() {
String sql ="select * from t_student";
final ArrayList<Student> studentList = new ArrayList<Student>();
jdbcTemplate.query(sql, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
studentList.add(student);
}
});
return studentList;
}
}
2,修改 StudentServiceImpl类,调用,
@Override
public List<Student> findStudents() {
return studentDao.findStudents();
}
3,Student添加无参构造和toString方法,数据库手动添加一些测试数据
4,运行测试,
package com.cruise.test;
import java.util.List;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.cruise.model.Student;
import com.cruise.service.StudentService;
public class T {
public static void main(String[] args) {
ClassPathXmlApplicationContext CPXAC=newClassPathXmlApplicationContext("beans.xml");
StudentService studentService = (StudentService) CPXAC.getBean("studentService");
List<Student> students = studentService.findStudents();
for (Student student : students) {
System.out.println(student);
}
}
}
第八讲:8.2 spring jdbc其他操作
