JAVA 3层架构及其实例文件/代码规范

目录
架构图
为什么要使用3层架构
各个部分应有的文件
表现层
业务逻辑层
数据访问层
各层示例代码
表现层
jsp
业务逻辑层
接口
逻辑
【JAVA 3层架构及其实例文件/代码规范】数据访问层
dao 接口
dao 操作
一图胜万言

JAVA 3层架构及其实例文件/代码规范

文章插图
架构图
为什么要使用3层架构
1 方便团队分工 利于维护
2 规范代码 , 在开发软件时对每个层的代码进行规范 , 固定开发语言的风格 。
3 降低更新难度 , 当软件系统要换数据库时 , 只要将数据访问层的代码修改就好了 。
4 实现"高内聚、低耦合" 。易于分配资源 。
5 是使得代码逻辑清晰 。
各个部分应有的文件 表现层
应该有的文件(jsp+)
业务逻辑层
应该有的文件(的接口 , 及其实现的逻辑代码)
具体的逻辑代码应该包括 , 多次对Dao层的操作(多次查询 , 修改 , 登陆的检验 , 用户在线状态的检验等等)也包括对数据库的连接 , 关闭等等
数据访问层
应该有的
DAO(全称Data  , 意为数据访问接口/对象)
DAO中也应该定义接口 , 及其实现(具体指增删改查的代码)
(意为实体类是数据库表的映射)
utils是工具集的意思 , 其中连接数据库 , 关闭数据库/数据源/对象的一些操作应该写在这里的某个类里面
各层示例代码 表现层 jsp
Title
序号品牌名称企业名称排序品牌介绍状态操作${brand.id}${brand.brandName}${brand.companyName}${brand.ordered}${brand.description} ${brand.status}修改 删除

package com.teaching.jsp.servlet;import com.teaching.jsp.entity.Brand;import com.teaching.jsp.service.BrandService;import com.teaching.jsp.service.impl.BrandServiceImpl;import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.annotation.*;import java.io.IOException;@WebServlet(name = "AddBrandServlet", value = "http://www.kingceram.com/addBrand")public class AddBrandServlet extends HttpServlet {BrandService brandService=new BrandServiceImpl();@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding("utf8");//接收参数String brandName = request.getParameter("brandName");String companyName = request.getParameter("companyName");String ordered = request.getParameter("ordered");String description = request.getParameter("description");String status = request.getParameter("status");//封装BrandBrand brand = new Brand();brand.setBrandName(brandName);brand.setDescription(description);brand.setCompanyName(companyName);brand.setOrdered(Integer.valueOf(ordered));brand.setStatus(Integer.valueOf(status));//调用Service去处理业务brandService.insertBrand(brand);//转发到selectAllresponse.sendRedirect("/teachingJsp/selectBrand");}}
业务逻辑层接口
package com.teaching.jsp.service;import com.teaching.jsp.entity.Brand;import java.util.List;public interface BrandService {List selectAllBrand();boolean insertBrand(Brand brand);Brand selectBrandById(String id);boolean updateBrand(Brand brand);}
逻辑
package com.web.serivce.impl;import com.web.dao.Impl.UserDaoImpl;import com.web.dao.UserDao;import com.web.entity.User;import com.web.serivce.UserServlet;import com.web.utils.DbUtils;import java.sql.Connection;import java.sql.SQLException;public class UserService implements UserServlet {private UserDao userDao=new UserDaoImpl();//使用Dao查询当前的用户@Overridepublic User getUserByUserEntity(User user) {//service是有多个dao方法组成//和数据库交互相关的方法调用//其中获取数据库连接 , 关闭连接 应该写在本层User u= null;Connection connection =null;try {connection = DbUtils.getConnection();connection.setAutoCommit(false);u = userDao.selectUserByEntity(user,connection);connection.commit();} catch (SQLException e) {try {connection.rollback();} catch (SQLException ex) {throw new RuntimeException(ex);}throw new RuntimeException(e);} finally {if (connection!=null){try {DbUtils.close(connection);} catch (SQLException e) {throw new RuntimeException(e);}}}return u;}//判断用户是否注册@Overridepublic boolean registryUser(User user) {User u= null;Connection connection =null;int row=0;try {connection = DbUtils.getConnection();connection.setAutoCommit(false);int i=userDao.selectUserByUsername(user.getUsername(),connection);if (i>0){throw new RuntimeException("username已经存在");}row+=userDao.insertUser(user,connection);connection.commit();} catch (SQLException e) {try {connection.rollback();} catch (SQLException ex) {throw new RuntimeException(ex);}throw new RuntimeException(e);} finally {if (connection!=null){try {DbUtils.close(connection);} catch (SQLException e) {throw new RuntimeException(e);}}}return row>0;}}