详细:MVC开发模式(代码示例)
代码布局:
Student.java
1 package com.zzk.bean; 2 3 /** 4 * @Author: zzk 5 * @Description: 实体类包-主要存放和数据库对应的实体类---类名=表名;列名=属性名 6 * 实体类需要包含:属性,构造方法(无参,全参构造),setter/getter方法 7 * 属于Model 8 * @Date Created in 2021-03-25-23:44 9 * @Modified By: 10 */ 11 public class Student { 12 private Integer stuid; 13 private String stuname; 14 private Integer age; 15 private Integer sex; 16 17 public Student() {} 18 19 public Student(Integer stuid, String stuname, Integer age, Integer sex) { 20 this.stuid = stuid; 21 this.stuname = stuname; 22 this.age = age; 23 this.sex = sex; 24 } 25 26 public Integer getStuid() { 27 return stuid; 28 } 29 30 public void setStuid(Integer stuid) { 31 this.stuid = stuid; 32 } 33 34 public String getStuname() { 35 return stuname; 36 } 37 38 public void setStuname(String stuname) { 39 this.stuname = stuname; 40 } 41 42 public Integer getAge() { 43 return age; 44 } 45 46 public void setAge(Integer age) { 47 this.age = age; 48 } 49 50 public Integer getSex() { 51 return sex; 52 } 53 54 public void setSex(Integer sex) { 55 this.sex = sex; 56 } 57 }
StudentDao.java
1 package com.zzk.dao; 2 3 import com.zzk.bean.Student; 4 5 import java.util.List; 6 7 /** 8 * @Author: zzk 9 * @Description: 命名:实体类名+Dao 10 * 11 * @Date Created in 2021-03-25-23:51 12 * @Modified By: 13 */ 14 public interface StudentDao { 15 //定义操作数据库的方法 16 List<Student> getAll(); 17 }
StudentDaoImpl.java
1 package com.zzk.dao.impl; 2 3 import com.zzk.bean.Student; 4 import com.zzk.dao.StudentDao; 5 import com.zzk.util.DruidUtil; 6 7 import java.sql.Connection; 8 import java.sql.PreparedStatement; 9 import java.sql.ResultSet; 10 import java.sql.SQLException; 11 import java.util.ArrayList; 12 import java.util.List; 13 14 /** 15 * @Author: zzk 16 * @Description: 命名:接口名+Impl 17 * @Date Created in 2021-03-25-23:56 18 * @Modified By: 19 */ 20 public class StudentDaoImpl extends DruidUtil implements StudentDao { 21 @Override 22 public List<Student> getAll() { 23 List list = new ArrayList(); 24 Connection connection = null; 25 PreparedStatement preparedStatement = null; 26 ResultSet resultSet = null; 27 try { 28 connection = getConnection(); 29 preparedStatement = connection.prepareStatement("select * from student"); 30 resultSet = preparedStatement.executeQuery(); 31 while (resultSet.next()){ 32 Student student = new Student(); 33 student.setStuid(resultSet.getInt("stuid")); 34 student.setStuname(resultSet.getString("stuname")); 35 student.setAge(resultSet.getInt("age")); 36 student.setSex(resultSet.getInt("sex")); 37 list.add(student); 38 } 39 } catch (SQLException throwables) { 40 throwables.printStackTrace(); 41 } finally { 42 close(connection,preparedStatement,resultSet); 43 } 44 return list; 45 } 46 }
StudentService.java
1 package com.zzk.service; 2 3 import com.zzk.bean.Student; 4 5 import java.util.List; 6 7 /** 8 * @Author: zzk 9 * @Description: 命名:实体类名+Service 10 * Service层主要定义业务逻辑,现阶段主要实现调取dao层 11 * @Date Created in 2021-03-26-0:18 12 * @Modified By: 13 */ 14 public interface StudentService { 15 //查询全部 16 List<Student> getAll(); 17 }
StudentServlet.java
1 package com.zzk.servlet; 2 3 import com.zzk.bean.Student; 4 import com.zzk.service.StudentService; 5 import com.zzk.service.impl.StudentServiceImpl; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 import java.io.IOException; 13 import java.util.List; 14 15 /** 16 * @Author: zzk 17 * @Description: C-controller 控制层 18 * @Date Created in 2021-03-26-0:24 19 * @Modified By: 20 */ 21 @WebServlet(urlPatterns = "/getstus") 22 public class StudentServlet extends HttpServlet { 23 @Override 24 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 25 doPost(req,resp); 26 } 27 28 @Override 29 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 30 //1.接收请求参数 31 //2.调取service层方法 32 StudentService studentService = new StudentServiceImpl(); 33 List<Student> all = studentService.getAll(); 34 //3.跳转页面(这里后台传递数据给前台,所以用转发,不用重定向) 35 req.setAttribute("allStudent",all); 36 req.getRequestDispatcher("show.jsp").forward(req,resp); 37 } 38 }
DruidUtil.java
1 package com.zzk.util; 2 3 import com.alibaba.druid.pool.DruidDataSourceFactory; 4 5 import javax.sql.DataSource; 6 import javax.xml.transform.Result; 7 import java.io.IOException; 8 import java.sql.Connection; 9 import java.sql.ResultSet; 10 import java.sql.SQLException; 11 import java.sql.Statement; 12 import java.util.Properties; 13 14 public class DruidUtil { 15 16 private static DataSource ds; 17 static{ 18 try { 19 Properties ppt = new Properties(); 20 ppt.load(DruidUtil.class.getClassLoader().getResourceAsStream("druid.properties")); 21 ds = DruidDataSourceFactory.createDataSource(ppt); 22 } catch (Exception e) { 23 e.printStackTrace(); 24 } 25 } 26 27 28 /** 29 * 从连接池中取出一个连接给用户 30 * @return 31 */ 32 public static Connection getConnection(){ 33 try { 34 return ds.getConnection(); 35 } catch (SQLException throwables) { 36 throwables.printStackTrace(); 37 } 38 return null; 39 } 40 41 42 public static void close(Connection conn, Statement state, ResultSet rs){ 43 try { 44 rs.close(); 45 } catch (Exception throwables) { 46 throwables.printStackTrace(); 47 } 48 try { 49 state.close(); 50 } catch (Exception throwables) { 51 throwables.printStackTrace(); 52 } 53 try { 54 conn.close(); 55 } catch (Exception throwables) { 56 throwables.printStackTrace(); 57 } 58 } 59 }
druid.properties
1 url=jdbc:mysql://localhost:3306/mymvc?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false 2 username=root 3 password=123456 4 driverClassName=com.mysql.cj.jdbc.Driver 5 initialSize=5 6 maxActive=10 7 minIdle=5 8 maxWait=3000
index.jsp
1 <%-- 2 Created by IntelliJ IDEA. 3 User: lenovo 4 Date: 2021/3/25 5 Time: 23:30 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <html> 10 <head> 11 <title>$Title$</title> 12 </head> 13 <body> 14 <a href="getstus">查询学生列表</a> 15 </body> 16 </html>
show.jsp
1 <%-- 2 Created by IntelliJ IDEA. 3 User: lenovo 4 Date: 2021/3/26 5 Time: 0:36 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 10 <html> 11 <head> 12 <title>学生信息</title> 13 </head> 14 <body> 15 <div align="center"> 16 <h1>全体学生信息</h1> 17 </div> 18 <div align="center"> 19 <table border="1" width="500px" bgcolor="aqua"> 20 <tr> 21 <th>学生ID</th> 22 <th>学生姓名</th> 23 <th>学生年龄</th> 24 <th>学生性别</th> 25 </tr> 26 <c:forEach items="${allStudent}" var="stuInfo"> 27 <tr align="center"> 28 <td>${stuInfo.stuid}</td> 29 <td>${stuInfo.stuname}</td> 30 <td>${stuInfo.age}</td> 31 <td>${stuInfo.sex==1?"男":"女"}</td> 32 </tr> 33 </c:forEach> 34 </table> 35 </div> 36 </body> 37 </html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~