java web 接口优化_JavaWeb学习(17):优化三层(加入接口和 DBUtil)

本文介绍了在JavaWeb开发中如何优化三层架构,主要涉及面向接口开发和DBUtil通用数据库帮助类的使用。通过接口定义业务逻辑,实现了IStudentDao和IStudentService,降低了耦合度。DBUtil类提供了连接数据库、预处理SQL等方法,减少了Dao层的代码重复,提高了代码复用性。
摘要由CSDN通过智能技术生成

饭前点心:

如果对三层架构有什么不理解的:

改进了那些地方:

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();

效果如图:

9e3e68f93b5c726b2533857f70928889.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值