0


mvc设计模式与三层架构

mvc与三层架构

1.什么是mvc设计模式

写Java Web项⽬时会发现,一个中型或者大型项目随着代码的增多,会发现:代码既可以写在src目录下,也可以写在WebContent目录下。

src下可以建很多包,WebContent下可以建很多文件夹。

所以问题就来了:一个新的类到底往哪个目录下的哪个文件夹里写?

此时解决办法就是:需要一个模式去规范,到底哪个类该往哪里写

M:(Model) 模型 : 应用程序的核心功能,管理这个模块中用的数据和值(bean,dao);

V(View )视图: 视图提供模型的展示,管理模型如何显示给用户,它是应用程序的外观(jsp/html)

C(Controller)控制器: 对用户的输入做出的反应,管理用户和视图的交互,也是连接模型和视图的枢纽(servlet/service)

MVC用于将web(UI)层进行职责解耦

说明:mvc设计模式(不属于23种设计模式)

2.三层架构

3.三层架构和MVC的区别与联系

准备工作

1.包结构

2.导入jar包

配置Tomcat

简化地址栏信息

工具类util:DruidUtil

  1. package util;
  2. import com.alibaba.druid.pool.DruidDataSourceFactory;
  3. import javax.sql.DataSource;
  4. import javax.xml.transform.Result;
  5. import java.io.IOException;
  6. import java.sql.Connection;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.Properties;
  11. public class DruidUtil {
  12. private static DataSource ds;
  13. static{
  14. try {
  15. Properties ppt = new Properties();
  16. ppt.load(DruidUtil.class.getClassLoader().getResourceAsStream("druid.properties"));
  17. ds = DruidDataSourceFactory.createDataSource(ppt);
  18. } catch (Exception e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. /**
  23. * 从连接池中取出一个连接给用户
  24. * @return
  25. */
  26. public static Connection getConnection(){
  27. try {
  28. return ds.getConnection();
  29. } catch (SQLException throwables) {
  30. throwables.printStackTrace();
  31. }
  32. return null;
  33. }
  34. public static void close(Connection conn, Statement state, ResultSet rs){
  35. try {
  36. rs.close();
  37. } catch (Exception throwables) {
  38. throwables.printStackTrace();
  39. }
  40. try {
  41. state.close();
  42. } catch (Exception throwables) {
  43. throwables.printStackTrace();
  44. }
  45. try {
  46. conn.close();
  47. } catch (Exception throwables) {
  48. throwables.printStackTrace();
  49. }
  50. }
  51. }

属性配置文件 druid.properties

  1. url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
  2. username=root
  3. password=123456
  4. driverClassName=com.mysql.jdbc.Driver
  5. initialSize=5
  6. maxActive=10
  7. minIdle=5
  8. maxWait=3000

一个请求的流程

一:MVC之Model开发

beam包:实体类Student

  1. package bean;
  2. //实体类包=主要存放数据库对应的实体类
  3. //类名=表名
  4. //列名=属性名
  5. //实体类包括:属性,构造方法(无参,全参),setter/getter
  6. //属于Model(M)
  7. public class Student {
  8. private Integer stuid;
  9. private String stuname;
  10. private Integer age;
  11. private String sex;
  12. //无参构造方法
  13. public Student() {
  14. }
  15. //全参构造方法
  16. public Student(Integer stuid, String stuname, Integer age, String sex) {
  17. this.stuid = stuid;
  18. this.stuname = stuname;
  19. this.age = age;
  20. this.sex = sex;
  21. }
  22. public Integer getStuid() {
  23. return stuid;
  24. }
  25. public void setStuid(Integer stuid) {
  26. this.stuid = stuid;
  27. }
  28. public String getStuname() {
  29. return stuname;
  30. }
  31. public void setStuname(String stuname) {
  32. this.stuname = stuname;
  33. }
  34. public Integer getAge() {
  35. return age;
  36. }
  37. public void setAge(Integer age) {
  38. this.age = age;
  39. }
  40. public String getSex() {
  41. return sex;
  42. }
  43. public void setSex(String sex) {
  44. this.sex = sex;
  45. }
  46. @Override
  47. public String toString() {
  48. return "Student{" +
  49. "stuid=" + stuid +
  50. ", stuname='" + stuname + '\'' +
  51. ", age=" + age +
  52. ", sex='" + sex + '\'' +
  53. '}';
  54. }
  55. }

dao包:StudentDao接口

  1. package dao;
  2. import bean.Student;
  3. import java.util.List;
  4. //实体类名+Dao=当前类名
  5. public interface StudentDao {
  6. //定义操作数据库的方法
  7. //查询全部方法
  8. public List<Student> getAll();
  9. }

dao包:impl子包: StudentDaoImpl接口实现类

  1. package dao.impl;
  2. import bean.Student;
  3. import dao.StudentDao;
  4. import util.DruidUtil;
  5. import java.sql.Connection;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. //接口名+impl=当前类名
  12. public class StudentDaoImpl extends DruidUtil implements StudentDao {
  13. @Override
  14. public List<Student> getAll() {
  15. Connection connection =null;
  16. PreparedStatement preparedStatement =null;
  17. ResultSet resultSet=null;
  18. List list=new ArrayList();
  19. try {
  20. connection = getConnection();
  21. preparedStatement = connection.prepareStatement("select * from student");
  22. resultSet=preparedStatement.executeQuery();
  23. while (resultSet.next()){
  24. Student student = new Student();
  25. student.setStuid(resultSet.getInt("stuid"));
  26. student.setStuname(resultSet.getString("stuname"));
  27. student.setAge(resultSet.getInt("age"));
  28. student.setSex(resultSet.getString("sex"));
  29. list.add(student);
  30. }
  31. } catch (SQLException e) {
  32. e.printStackTrace();
  33. }finally {
  34. close(connection,preparedStatement,resultSet);
  35. }
  36. return list;
  37. }
  38. }

二:MVC之Controlle开发

service包 :StudentService接口

  1. package service;
  2. import bean.Student;
  3. import java.util.List;
  4. //bean类名+Service=当前类名
  5. //Service层主要定义业务逻辑,现阶段主要实现调取dao层
  6. public interface StudentService {
  7. //查询全部方法
  8. public List<Student> getAll();
  9. }

service包 :impl子包:StudentServiceImpl接口实现类

  1. package service.impl;
  2. import bean.Student;
  3. import dao.StudentDao;
  4. import dao.impl.StudentDaoImpl;
  5. import service.StudentService;
  6. import java.util.List;
  7. public class StudentServiceImpl implements StudentService {
  8. private StudentDao studentDao=new StudentDaoImpl();
  9. @Override
  10. public List<Student> getAll() {
  11. return studentDao.getAll();
  12. }
  13. }

servlet包 :StudentServlet处理类处理请求

  1. package servlet;
  2. import bean.Student;
  3. import service.impl.StudentServiceImpl;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.annotation.WebServlet;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. import java.util.List;
  11. //C -controller 控制层
  12. @WebServlet(urlPatterns = "/getstus")
  13. public class StudentServlet extends HttpServlet {
  14. @Override
  15. protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  16. //接参数(接收请求参数)
  17. //写逻辑(调取service层方法)
  18. StudentServiceImpl studentService=new StudentServiceImpl();
  19. List<Student> getAll=studentService.getAll();
  20. //返结果(跳转页面)
  21. //后台传递数据给前台
  22. req.setAttribute("stulist",getAll);
  23. req.getRequestDispatcher("/show.jsp").forward(req,resp);
  24. }
  25. }

三:MVC之View开发

index.jsp页面

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <html>
  3. <head>
  4. <title>$Title$</title>
  5. </head>
  6. <body>
  7. <a href="getstus">查询学生列表</a>
  8. </body>
  9. </html>

show.jsp页面

  1. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  2. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  3. <html>
  4. <head>
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <h1>show.jsp</h1>
  9. <table border="" width="500px" bgcolor="aqua">
  10. <tr>
  11. <td>id</td>
  12. <td>name</td>
  13. <td>age</td>
  14. <td>sex</td>
  15. </tr>
  16. <c:forEach items="${stulist}" var="stu">
  17. <tr>
  18. <td>${stu.stuid}</td>
  19. <td>${stu.stuname}</td>
  20. <td>${stu.age}</td>
  21. <td>${stu.sex==1?"男":"女"}</td>
  22. </tr>
  23. </c:forEach>
  24. </table>
  25. </body>
  26. </html>

测试结果

前后端分离技术(了解)

标签: mvc linq java

本文转载自: https://blog.csdn.net/continueLR/article/details/122759149
版权归原作者 continueLR 所有, 如有侵权,请联系我们删除。

“mvc设计模式与三层架构”的评论:

还没有评论