0
点赞
收藏
分享

微信扫一扫

JavaWeb三层架构项目集成

爱写作的小土豆 2022-04-14 阅读 75

目录

课前回顾

一、JavaWeb环境搭建

二、基本语法和表单提交方式

三、页面跳转方式和jdbc

四、服务器与客户端储存

 五、新闻数据分页

 六、文件上传

七、三层架构

三层模式

三层模式的划分

层与层之间的关系

分层实现用户登录

创建实体类

编写数据访问层

数据访问接口

数据访问接口的实现

编写业务逻辑层

编写表示层


课前回顾

一、JavaWeb环境搭建

二、基本语法和表单提交方式

三、页面跳转方式和jdbc

四、服务器与客户端储存

 五、新闻数据分页

 六、文件上传

七、三层架构


三层模式

三层模式的划分

  • 表示层
  • 业务逻辑层
  • 数据库访问层

层与层之间的关系


分层实现用户登录

创建实体类

package com.zking.pojo;
/**
 * 用户实体类
 * @author zjjt
 *
 */

public class User {

    private Integer userId;
    private String userName;
    private String userPwd;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPwd() {
        return userPwd;
    }

    public void setUserPwd(String userPwd) {
        this.userPwd = userPwd;
    }

    public User() {
    }

    public User(Integer userId, String userName, String userPwd) {
        this.userId = userId;
        this.userName = userName;
        this.userPwd = userPwd;
    }

}

编写数据访问层

  • 数据访问接口

package com.zking.dao;

import com.zking.pojo.User;
/**
 * 用户的数据访问接口
 * @author zjjt
 *
 */
public interface IUserDao {
    User login(User user);
   
}
  • 数据访问接口的实现

package com.zking.dao.impl;

import com.zking.dao.IUserDao;
import com.zking.pojo.User;
import com.zking.util.DBHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
 * 数据库访问接口类
 * @author zjjt
 *
 */

public class UserDaoImpl implements IUserDao {

    private Connection con;
    private PreparedStatement ps;
    private ResultSet rs;

    public User login(User user) {
        try {
            con=DBHelper.getCon();
            String sql="select * from t_user where user_name=?";
            ps=con.prepareStatement(sql);
            ps.setString(1, user.getUserName());
            rs=ps.executeQuery();
            if(rs.next()){
                //将信息赋值给User对象
                User u=new User();
                u.setUserName(rs.getString(2));
                u.setUserPwd(rs.getString(3));
                return u;
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBHelper.close(con,ps,rs);
        }
        return null;
    }

}

编写业务逻辑层

  • 编写业务逻辑层接口
public interface IUserBiz {
  
	String login(User user);   
}
  • 编写业务逻辑层接口实现
package com.zking.biz.impl;

import com.zking.biz.IUserBiz;
import com.zking.dao.IUserDao;
import com.zking.dao.impl.UserDaoImpl;
import com.zking.pojo.User;

import java.util.List;

public class UserBizImpl implements IUserBiz {

    //需要去调用dao层的方法

    //一个厨师对应多个送菜的人 里式替换原则
    private IUserDao userDao=new UserDaoImpl();

    @Override
    public String login(User user) {
        User u = userDao.login(user);
        // user 表示层给我的
        // u 数据库来的
        if(u==null){
            return "account not found";
        }
        if(!u.getUserPwd().equals(user.getUserPwd())){
            return "password error";
        }
        return "login success";
    }
}

编写表示层

JSP页面调用业务逻辑层

面向接口编程
    1、先编写接口
    2、实现该接口
    3、使用里氏替换原则来声明接口对象
    4、调用接口的方法类

<%@page import="com.zking.pojo.User"%>
<%@page import="com.zking.biz.impl.UserBizImpl"%>
<%@page import="com.zking.biz.IUserBiz"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
     //表示层
     //表示层-业务逻辑层(biz)-数据库操作层(dao)
     String username=request.getParameter("username");
     String password=request.getParameter("password");
    //将数据封装到实体类中
     User user= new User(0,username,password);
     //判断用户是否登录
     
     //面向接口编程
     //1、先编写接口
     //2、实现该接口
     //3、使用里氏替换原则来声明接口对象
     //4、调用接口的方法类
      IUserBiz userBiz=new UserBizImpl();
     //调用业务逻辑层的方法
     String massage=userBiz.login(user);
     //输出结果 
     
%>
<h2><%=massage%></h2>

登陆界面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="doLogin.jsp" method="post">
<p><input type="text" name="username"></p>
    <p><input type="text" name="password"></p>
    <p><button>登录</button></p></form>
</body>
</html>

效果:

 

 

举报

相关推荐

0 条评论