• 2022-09-04 瞒春 学习笔记


    🚩前言

    🌻今天进行MVC架构的学习

    ✏️MVC架构

    一种软件架构模式,把整个软件分为三层:

    Model模型——获取数据,并且返回给Controller
    entity:——数据库的实体类
    service(业务):——业务控制层,【其余的活都交给service】
    dao层(数据模型层)——操作数据库
    View视图——看见的页面,渲染数据,渲染页面
    Controller控制器——Servlet,接请求,给响应
    耦合度:【代码之间的关联关系】。

    ✒️为什么要分层?

    MVC
    降耦合
    重用性高
    可维护性强
    设计理念——一张表,一个entity,一个service,一个dao,一个controller

    使用关系
    View层发起请求——Controller——Service——Dao层——Service——Controller——View

    Model模型层

    📌实体类(entity)

    实体类要求:

    类名和表名相同
    类中的属性和表中的字段名相同
    类中只能有对应的set,get方法和需要用到的构造器,如果有需要,可以写toString
    需要序列化,实现序列化接口

    ```csharp
    package com.gyc.morning.entity;
    
    import java.io.Serializable;
    
    public class Vip implements Serializable {
        private static final long serialVersionUID = -681512470754667710L;
        private Integer id;
        private String username;
        private String password;
        private String name;
        private String gender;
        private String profile;
        private String salt;
    
        public Vip() {
        }
    
        public Vip(String username, String password, String name, String gender) {
            this.username = username;
            this.password = password;
            this.name = name;
            this.gender = gender;
        }
    
        public Vip(Integer id, String username, String password, String name, String gender, String profile) {
            this.id = id;
            this.username = username;
            this.password = password;
            this.name = name;
            this.gender = gender;
            this.profile = profile;
        }
    
        public String getSalt() {
            return salt;
        }
    
        public void setSalt(String salt) {
            this.salt = salt;
        }
    
        public static long getSerialVersionUID() {
            return serialVersionUID;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getGender() {
            return gender;
        }
    
        public void setGender(String gender) {
            this.gender = gender;
        }
    
        public String getProfile() {
            return profile;
        }
    
        public void setProfile(String profile) {
            this.profile = profile;
        }
    
        @Override
        public String toString() {
            return "Vip{" +
                    "id=" + id +
                    ", username='" + username + '\'' +
                    ", password='" + password + '\'' +
                    ", name='" + name + '\'' +
                    ", gender='" + gender + '\'' +
                    ", profile='" + profile + '\'' +
                    '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    
    ### 业务层(service)
    业务:软件的功能——例如:登录,注册
    
    
    
    
    
    ```csharp
    先创建接口
    
    // 业务层
    public interface VipService {
        // 注册的方法
        int register(Vip vip);
        // 登录
        Long login(String username,String password);
        // 根据用户名查询--盐
        String select(String username);
    
    }
    实现接口的类
    
    public class VipServiceImpl implements VipService {
        private VipDao  vipDao=new VipDaoImpl();
        
        // 注册
        @Override
        public int register(Vip vip) {
            try {
                String salt = MD5Util.getSalt();
                vip.setPassword(MD5Util.stringToMD5(vip.getPassword()+salt));
                vip.setSalt(salt);
                return vipDao.save(vip);
            } catch (Exception e) {
                throw new RuntimeException();
            }
    
        }
    
        // 登录
        @Override
        public Long login(String username, String password) {
            try {
                password=MD5Util.stringToMD5(password+select(username));
                return vipDao.login(username,password);
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    
        // 查询--盐
        @Override
        public String select(String username) {
            try {
                return vipDao.select(username);
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    📌数据模型层(dao)

    执行SQL语句,获取数据
    先创建接口

    public interface VipDao {
        // 注册
        int save(Vip vip) throws Exception;
        
        // 登录
        Long login(String username,String password) throws Exception;
        
        // 添加
        String select(String username) throws Exception;
    }
    
    实现接口的类
    
    public class VipDaoImpl extends DAOImpl<Vip> implements VipDao {
        @Override
        public int save(Vip vip) throws Exception {
            // 添加用户名,密码,性别,真实姓名,盐
            String sql="insert into vip(username,password,gender,name,salt) values(?,?,?,?,?)";
            return update(sql,vip.getUsername(),vip.getPassword(),vip.getGender(),vip.getName(),vip.getSalt());
        }
    
        @Override
        public Long login(String username, String password) throws Exception {
            // 根据用户名和密码查询是否存在
            String sql="select count(*) from vip where username=? and password=?";
            return  getForValue(sql,username,password);
        }
    
        @Override
        public String select(String username) throws Exception {
            // 根据用户名查询---盐
            String sql="select salt from vip where username=? ";
            return getForValue(sql,username);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    📌View层

    前端页面,用来渲染实现页面的各种功能

    代码实例

    ```csharp
    <!--index.html-->
    <body>
    <a href="login.html">登录</a>
    <a href="register.html">注册</a>
    </body>
    <!--login.html-->
    <body>
    <form action="login.do" method="post">
        <p>账户: <input type="text" name="username"></p>
        <p>密码: <input type="password" name="password"></p>
        <input type="submit" value="登录">
    </form>
    </body>
    <!--register.html-->
    <body>
    <form action="addVip.do" method="post">
        <p>账户:<input type="text" name="username"></p>
        <p>密码:<input type="password" name="password"></p>
        <p>姓名:<input type="text" name="name"></p>
        <p>
            性别:<input type="radio" name="gender" value="男"><input type="radio" name="gender" value="女" checked></p>
        <p>
            <input type="submit" value="提交">
        </p>
    </form>
    </body>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    📌Controller层

    用来接受请求,发出响应

    // 以.do结尾的所有请求路径
    @WebServlet("*.do")
    public class VipController extends HttpServlet {
        private VipService vipService = new VipServiceImpl();
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            // 获取请求的根路径
            String servletPath = req.getServletPath();
            // 去掉请求根路径前边的/
            String substring = servletPath.substring(1);
    
            // 拿到.do前边的字符串
            substring = substring.substring(substring.lastIndexOf("/")+1, substring.length() - 3);
    
    
            try {
                // 拿到当前类要执行的方法的对象
                Method declaredMethod = getClass().getDeclaredMethod(substring, HttpServletRequest.class, HttpServletResponse.class);
                // 让方法去执行
                declaredMethod.invoke(this, req, resp);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
    
        }
    
        private void addVip(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            String name = req.getParameter("name");
            String gender = req.getParameter("gender");
            Vip vip = new Vip(username, password, name, gender);
            vipService.register(vip);
        }
    
        private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            Long login = vipService.login(username, password);
            // 如果为0,代表用户的账户密码不存在
            // 如果大于0,代表用户和密码存在
            if (login != 0) {
                System.out.println("Success");
            } else {
                System.out.println("Fail");
                resp.sendRedirect("login.html");
            }
    
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    📌工具类 加密类

    public class MD5Util {
        // MD5加密
        public static String stringToMD5(String str){
    
            return DigestUtils.md5Hex(str.getBytes());
        }
        
        // 创建盐
        public static String getSalt(){
            String words="qwertyuioplkjhgfdsazxcvb1234567890./;'[]-=`!@#$%";
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < 8; i++) {
                Random random = new Random();
                stringBuilder.append(words.charAt(random.nextInt(words.length()-1)));
            }
            return stringBuilder.toString();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    📌所需的jar包

    请添加图片描述

  • 相关阅读:
    【STM32】Cortex-M4 超详细的GPIO输出分析
    详情讲解canvas实现电子签名
    JVM基本常识了解
    Spring框架对redis的封装
    【毕业季】致毕业生的一句话:天高任鸟飞,海阔凭鱼跃
    聚类算法(K-means & AGNES & DBSCAN)
    使用Idea简单搭建springcloud项目
    让mybatis-plus支持NOT逻辑运算
    长期稳定的项目—steam搬砖
    从 Pulsar Client 的原理到它的监控面板
  • 原文地址:https://blog.csdn.net/weixin_49405762/article/details/126692278