饭前点心:
如果对三层架构有什么不理解的:
改进了那些地方:
1、加入接口(面向接口开发)
我们在实际开发中一般都是先有一个大致的框架,然后在后期实现各个模块的功能。
先接口 -- > 再实现类。
2、DBUtil
通用的数据库帮助类,可以简化Dao层的代码量
帮助类 一般建议写在 xxx.util包
接口与实现类的命名规范:
接口:interface,起名 I实体类ServiceIStudentService
IStudentDao
实现类:implements起名 实体类ServiceImplStudentServiceImpl
StudentDaoImpl
接口:I实体类层所在包名IStudentService、IStudentDao
接口所在的包: xxx.servicexx.dao
实现类:实体类层所在包名ImplStudentServiceImpl、StudentDaoImpl
实现类所在的包:xxx.service.implxx.dao.impl
以后使用接口/实现类时,推荐写法:
接口 x = new 实现类();
IStudentDao studentDao = new StudentDaoImpl();
效果如图:
Code:
本次代码是在上一个介绍中进行有优化,在此只展现修改的代码,其他的的部分可参考:
interface IStudentDao:
package org.dao;
import java.util.List;
import org.entity.Student;
// 接口:更加贴近设计要求,一般都先有一个大概,然后通过实现类实现各个方法
public interface IStudentDao {
// 判断该用户是否存在
public boolean isExist(int sno) ;
// 添加新用户
public boolean addStudent(Student student);
// 根据学生学号修改学生信息
public boolean updateStudentBySno(int sno,Student student) ;
// 删除用户
public boolean deleteStudentBySno(int sno) ;
// 通过学号查询学生信息(返回值是一个学生,如果学生不存在返回 Null)
public Student queryStudentBySno(int sno);
public List queryAllStudents();
}
interface IStudentService:
package org.service;
import java.util.List;
import org.entity.Student;
//接口:更加贴近设计要求,一般都先有一个大概,然后通过实现类实现各个方法
public interface IStudentService {
public Student queryStudentBySno(int sno);
// 查询所有用户信息
public List queryAllStudents();
// 更新用户(更新用户之前需要先判断用户是否已经存在)
public boolean updateStudent(int s