这是项目的架构
dao层下面有一个平级的包 impl //dao层 访问数据库。
GradeDAOImpl 他继承了BaseDAO 实现了IGradeDAO接口
public class GradeDAOImpl extends BaseDAO implements IGradeDAO { @Override public List<Grade> findAllGrades() throws Exception { List<Grade> list = new ArrayList<Grade>(); String sql = "select * from grade"; ResultSet rs = executeQuery(sql); if (rs != null) { while (rs.next()) { Grade grade = new Grade(); grade.setGid(rs.getInt("gid")); grade.setGname(rs.getString("gname")); list.add(grade); } } return list; } @Override public boolean addGrade(Grade grade) throws Exception { boolean flag = false;// 添加失败 String sql = "insert into grade(gname) values(?)"; int count = executeUpdate(sql, grade.getGname()); if (count > 0) { flag = true; } return flag; } }
BaseDAO类 //访问数据库的底层类
/** * 数据库访问的工具类 * * @author Happy * */ public class BaseDAO { // 定义四个静态常量,保存数据连接信息 private static final String driver = "com.mysql.jdbc.Driver"; private static final String url = "jdbc:mysql:///s2230myschool"; // localhost // // // 3306 private static final String username = "root"; private static final String password = ""; // 三个接口 private Connection con; private PreparedStatement ps; private ResultSet rs; // 01.写一个 返回连接的方法 public Connection getConnection() throws Exception { Class.forName(driver); if (con == null || con.isClosed()) { con = DriverManager.getConnection(url, username, password); } return con; } // 02.写一个执行增 删 改操作的方法 public int executeUpdate(String sql, Object... objs) throws Exception { getConnection(); ps = con.prepareStatement(sql); for (int i = 0; i < objs.length; i++) { ps.setObject(i + 1, objs[i]); } int count = ps.executeUpdate(); return count; } // 03.写一个执行查询操作的方法 public ResultSet executeQuery(String sql, Object... objs) throws Exception { getConnection(); ps = con.prepareStatement(sql); for (int i = 0; i < objs.length; i++) { ps.setObject(i + 1, objs[i]); } rs = ps.executeQuery(); return rs; } // 04.写一个回收资源的方法 public void closeResource() { try { if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (con != null) { con.close(); } } catch (Exception e) { e.printStackTrace(); } } }
IGradeDAO接口
public interface IGradeDAO { // 01.书写一个用于查询所有年级名称和编号的方法 public List<Grade> findAllGrades() throws Exception; // 02.写一个用户添加年级的方法 public boolean addGrade(Grade grade) throws Exception; }
entity层 //实体类
public class Grade { private int gid; private String gname; public int getGid() { return gid; } public void setGid(int gid) { this.gid = gid; } public String getGname() { return gname; } public void setGname(String gname) { this.gname = gname; } }
service层 有一个平级的 impl层 // GradeServiceImpl 实现了 IGadeService 接口
GradeServiceImpl :
public class GradeServiceImpi implements IGradeService { // 需要向下依赖DAO的实例 以及他提供的方法 IGradeDAO dao = new GradeDAOImpl(); @Override public List<Grade> findAllGrades() throws Exception { return dao.findAllGrades(); } @Override public boolean addGrade(Grade grade) throws Exception { return dao.addGrade(grade); } }
IGadeService 接口 :
public interface IGradeService { // 01.书写一个用于查询所有年级名称和编号的方法 public List<Grade> findAllGrades() throws Exception; // 02.写一个用户添加年级的方法 public boolean addGrade(Grade grade) throws Exception; }
test层 //UI层
//测试类:
public class JDBCTest { public static void main(String[] args) throws Exception { /*GradeDAOImpl gradeDAO = new GradeDAOImpl(); Grade grade = new Grade(); grade.setGname("精英班"); boolean flag = gradeDAO.addGrade(grade); System.out.println(flag);*/ //UI层 依赖于service IGradeService service =new GradeServiceImpi(); List<Grade> list =service.findAllGrades(); for(Grade grade: list){ System.out.println(grade.getGname()); } } }
原文地址:https://www.cnblogs.com/SFHa/p/9194781.html
时间: 2024-11-04 03:39:34