有勇气的牛排博客

java Dao层抽象化设计 更简单

有勇气的牛排 1109 Java 2021-08-26 21:57:34

介绍

本机主要都上期文章Dao进行升级,分为了下面三类文件:

  • BaseDao:通用的dao层方法

  • IStudentDao:接口编程

  • StudentDao:实际开发Dao层

上期文章:https://www.920vip.net/article/84

1 BaseDao

package net920vip.Dao; import net920vip.util.DBUtil; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; /** * 通用的dao层方法 * */ public class BaseDao { DBUtil db; /** * 通用的增删改 * */ // public int update(String sql, Object[] o) throws Exception { public int update(String sql, Object... o) throws Exception { db = new DBUtil(); Connection con = db.getCon(); PreparedStatement pstm = con.prepareStatement(sql); if(o!=null){ for (int i=0;i<o.length;i++){ pstm.setObject((i+1),o[i]); } } int i = pstm.executeUpdate(); db.closeAll(); return i; } /** * 通用的查询 * */ public ResultSet query(String sql, Object... o)throws Exception{ db = new DBUtil(); Connection con = db.getCon(); PreparedStatement pstm = con.prepareStatement(sql); if(o!=null){ for (int i=0;i<o.length;i++){ pstm.setObject((i+1),o[i]); } } ResultSet rs = pstm.executeQuery(); // 此处不能关数据库 return rs; } }

2 IStudentDao

package net920vip.Dao;

import net920vip.Bean.Student;

import java.util.List;

public interface IStudentDao {
    public int insert(Student s) throws Exception;
    // 删除
    public int del(int id) throws Exception;
    // 修改
    public int update(Student s) throws Exception;

    // 查询所有学生
    public List<Student> queryAll() throws Exception;
    // 根据姓名模糊查询
    public List<Student> queryByName(String inputName) throws Exception;
}

3 StudentDao

package net920vip.Dao; import net920vip.Bean.Student; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; public class StudentDao extends BaseDao implements IStudentDao { @Override public int insert(Student s) throws Exception { // String sql = "insert into Student(username,password) values(?,?)"; // // // 新建一个数组 // Object[] o = new Object[2]; // o[0] = s.getUsername(); // o[1] = s.getPassoword(); // // // 调用父类方法 // int i = super.update(sql, o); // return i; // 方法2 // 方法3 return super.update("insert into Student(username,password) values(?,?)", s.getUsername(), s.getPassoword()); } @Override public int del(int id) throws Exception { return super.update("delete from Student where id = ?", id); } @Override public int update(Student s) throws Exception { return super.update("update Student set username = ?, password = ? where id = ?", s.getUsername(), s.getPassoword(), s.getId()); } @Override public List<Student> queryAll() throws Exception { ArrayList<Student> list = new ArrayList<Student>(); ResultSet rs = super.query("select * from Student", null); while (rs.next()) { // 取出每一行中的每一列的数据 int id = rs.getInt(1); String username = rs.getString(2); String password = rs.getString(3); // 将数据封装到Studnet对象里 Student s = new Student(id, username, password); // 分别将每一个studnet对象,添加到list集合中 list.add(s); } db.closeAll(); return list; } @Override public List<Student> queryByName(String inputName) throws Exception { return null; } }

留言

专栏
文章
加入群聊