第八讲:8.2 spring jdbc其他操作

本文详细介绍了一种基于Spring框架的学生信息管理系统中的增删改查(CRUD)操作实现过程,包括更新、删除、查询所有学生的具体代码示例。

一,更新操作
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);
       }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值