9、mvc设计模式

mvc在开发分为以下几个

 

 

 首先控制层进行对前端的表面传值,得到值之后访问业务逻辑层处理业务再调用数据访问层通过工具进行一系列的连接。注意捕获异常在控制层也就是最顶层。

1)控制层编写

package com.cqkj.controller;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.cqkj.bean.User;
import com.cqkj.services.UserService;
import com.cqkj.utils.DruidUtil;
import com.cqkj.utils.ResultMap;



@WebServlet("/user")
public class UserServlet extends BaseServlet{
    //servlet是什么?     哪个类继承了HttpServlet,那么这个类就不再是类而是servlet
    ResultMap resultMap = new ResultMap();
    UserService userService = new UserService();
    
    public String login(HttpServletRequest req, HttpServletResponse resp) {
        try {
            
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            //调用userService业务逻辑层
            User user = userService.login(username,password);
            HttpSession session = req.getSession();
            session.setAttribute("user",user);
            resultMap.setStatus(true);
        } catch (Exception e) {
            resultMap.setStatus(false);
            resultMap.setMessage(e.getMessage());
        }
        
        String jsonStr = JSON.toJSONString(resultMap);
        return jsonStr;
    }
    
    public String selectAllUser(HttpServletRequest req, HttpServletResponse resp) {
        return "";
    }
}

2)业务逻辑层,注意使用抛出异常的方式是最节约资源的。

package com.cqkj.services;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.cqkj.bean.User;
import com.cqkj.dao.UserDao;
import com.cqkj.utils.DruidUtil;

public class UserService {//业务逻辑层

    UserDao userDao = new UserDao();
    
    public User login(String username,String password) throws SQLException {
        if(username.equals("")) {
            throw new RuntimeException("用户名不能为空!");
        }
        if(password.equals("")) {
            throw new RuntimeException("密码不能为空!");
        }
     //调用数据处理层 User user
= userDao.login(username, password); if(user == null) { throw new RuntimeException("用户名或密码输入有误!"); } return user; } }

3)数据处理层调用工具类的方法,同前面。

import com.cqkj.utils.DruidUtil;

public class UserDao {//数据访问层
    
    
    public User login(String username,String password) throws SQLException {
        String sql = "select * from user where username = ? and password = ?";
        Object[] obj = {username,password};
        ResultSet rs = DruidUtil.query(sql, obj);
        User user = null;
        if(rs.next()) {
            user = new User();
            user.setUsername(rs.getString(1));
            user.setPassword(rs.getString(2));
            user.setName(rs.getString(3));
            user.setTel(rs.getString(4));
            user.setHeadImg(rs.getString(5));
            user.setType(rs.getInt(6));
        }
        return user;
    }

}

 

posted @ 2021-06-09 18:48  深夜起来玩游戏  阅读(64)  评论(0编辑  收藏  举报