关于增删改查参数和返回值类型选择问题

增加和更新:比如增加或更新一个学生(Student),参数类型是Student,返回值是可有可无(void 或object类型)。 删除:比如删除一个学生,参数类型是定义类型(Student)的属性(sid 或sname),返回值可有可无((void 或object类型) 查询: 1.查询单个学生:比如通过用户名查询单个学生信息,参数类型是定义类型(Student)的属性(sid 或sname),返回值是定义的类型(Student) 2.查询所有学生:比如通过用户名查询所有学生信息,参数类型是定义类型(Student)的属性(sname),返回值是定义类型的集合(List<Student>) 项目实例:

//  查询所有学生信息
    List<Student> findAll() throws SQLException;
    //  增加学生信息
    void addStudent(Student student)throws SQLException;
    //  删除学生信息
    void deleteStudent(int sid)throws SQLException;
    //  更新学生信息
    void updateStudent(Student student)throws SQLException;
    //  通过sid查询学生信息
    Student findById(int sid)throws SQLException;
    //  根据姓名模糊查询或性别,或者两者都有
    List<Student> searchStudent(String sname,String gender)throws SQLException;
    //  分页查询
    List<Student> findPage(int currentPage) throws SQLException;
    //  查询总记录数
    int findCount()throws SQLException;

总结:

有无参数:其实我们就可以通过SQL语句来判断,没有条件就无须传递参数(SELECT * from Studnet,SELECT count( *) from Studnet) 返回值类型: 第一种:可有可无类型的,我们根据我们要实现的功能来判断是否需要返回值。 第二种:必须有返回值类型的,可以根据我们传递的参数来判断,如果我们传递的参数,只能取出一条结果,就用对象类型(student),可以取出多条的,就用集合类型(List<student>)

经验分享 程序员 微信小程序 职场和发展