web流程示例(未使用mybatis)

it2025-12-24  8

说明:这个demo主要是总结JDBC的流程和细节,为了流程看得更清晰所以并未进行封装,另外我有一篇JDBC封装过的demo文章,那篇主要是注重封装的,mybatis框架就是做的数据访问层的封装

设计需求

将花的id,name,price,production四种数据,三种花以表的形式显示,并且有添加花卉种类功能(用另一页面填写信息提交表单)

按照开发的顺序进行编写 数据库设计、实体层、数据访问层、业务逻辑层、控制器层、视图

数据库表的设计

表名:flower

cn.wit.dao

FlowerDao

public interface FlowerDao { List<Flower> selAll(); int insert(Flower flower); }

cn.wit.daoImpl

FlowerDaoImpl

在daoImpl里面加上一个测试,用于测试从数据库到daoImpl数据是否成功流转

public class FlowerDaoImpl implements FlowerDao{ @Override public List<Flower> selAll() { Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; try { //类加载驱动 Class.forName("com.mysql.jdbc.Driver"); //创建连接对象 conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/login","root","bjxst"); //创建sql命令 String sql="select *from flower"; //占位符赋值 //创建sql命令对象 ps=conn.prepareStatement(sql); //执行sql命令 rs= ps.executeQuery(); //关闭资源 List<Flower> list=new ArrayList<>(); while(rs.next()){ Flower flower=new Flower(); flower.setId(rs.getInt("id")); flower.setName(rs.getString("name")); flower.setPrice(rs.getDouble("price")); flower.setProduction(rs.getString("production")); list.add(flower); //测试数据是否从数据库拿到 System.out.println(flower); } return list; } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { if(rs!=null){ rs.close(); } } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { if(ps!=null){ ps.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(conn!=null){ try { if(conn!=null){ conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return null; } @Override public int insert(Flower flower) { Connection conn=null; PreparedStatement ps=null; try { //类加载驱动 Class.forName("com.mysql.jdbc.Driver"); //创建连接对象 conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/login","root","bjxst"); //创建sql命令 String sql="insert into flower values(default,?,?,?)"; //创建sql命令对象 ps=conn.prepareStatement(sql); //占位符赋值 ps.setObject(1, flower.getName()); ps.setObject(2, flower.getPrice()); ps.setObject(3, flower.getProduction()); //执行sql命令 int i= ps.executeUpdate(); //关闭资源 return i; } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { if(ps!=null){ ps.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(conn!=null){ try { if(conn!=null){ conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return 0; } }

cn.wit.service

FlowerService

public interface FlowerService { List <Flower> show(); int add(Flower flower); }

cn.wit.serviceImpl

FlowerServiceImpl

public class FlowerServiceImpl implements FlowerService{ FlowerDao flowerDao=new FlowerDaoImpl(); @Override public List<Flower> show() { return flowerDao.selAll(); } @Override public int add(Flower flower) { return flowerDao.insert(flower); } }

cn.wit.servlet

ShowSerlvet

在servlet中创建service对象时,不要在service方法中创建,会造成栈堆数据堆积,GC不断回收

@WebServlet("/show") public class ShowServlet extends HttpServlet { /** * 此处创建类不要创建在service方法中 * 否则serice被调用一次就会创建一个对象 * 创建多个对象导致GC频繁回收 */ private FlowerService flowerService=new FlowerServiceImpl(); @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); List<Flower>list=flowerService.show(); //将list数据放在request中,请求转发进行数据流转 req.setAttribute("list", list); //测试dao、service数据是否流转到servlet中 if(list==null){ System.out.println("list为空"); }else{ System.out.println("list不为空"); } //在前端页面用getAttribute将list取出来,对list进行展示 req.getRequestDispatcher("index.jsp").forward(req, resp); } }

AddServlet

@WebServlet("/add") public class AddServlet extends HttpServlet { private FlowerService flowerService=new FlowerServiceImpl(); @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //设置请求、响应编码格式 req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); //拿到表单数据,传递flower给逻辑层 String name=req.getParameter("name"); double price=Double.parseDouble(req.getParameter("price")); String production=req.getParameter("production"); Flower flower=new Flower(); flower.setName(name); flower.setPrice(price); flower.setProduction(production); int i=flowerService.add(flower); if(i>0){ resp.sendRedirect("show"); }else{ resp.sendRedirect("add.jsp"); } } }

视图

index.jsp

pageEncoding 和contentType是转java文件时所需要的编码格式设置, 在jap转java文件的时候,先读pageEncoding,如果没有读到,会取contentType

EL表达式和JSTL标签库结合使用可以方便的拿取四大作用域对象中的数据,我的JavaEE分组里面有相关文章,eclipse使用jstl需要导入jstl和standard两个jar包

<!-- pageEncoding 和contentType是转java文件时所需要的编码格式设置, 在jap转java文件的时候,先读pageEncoding,如果没有读到,会取contentType 而req.setCharacterEncoding、resp.setContentType分别是设置浏览器请求和响应的编码格式 --> <%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%> <!--给资源前面添加项目路径 --> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <table border='1px'> <tr> <th>花卉编号</th> <th>花卉名称</th> <th>价格</th> <th>产地</th> </tr> <c:forEach items="${list}" var="flower"> <tr> <td>${flower.id}</td> <td>${flower.name}</td> <td>${flower.price}</td> <td>${flower.production}</td> </tr> </c:forEach> </table> </body> </html>

add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>添加花卉信息</title> <script type="text/javascript" src="js/jquery-1.7.2.js"></script> <script type="text/javascript"> $(function(){ $("form").submit(function(){ //表单选择器 :input标签type属性值 if($(":text:eq(0)").val()==""||$(":text:eq(1)").val()==""||$(":text:eq(2)").val()==""){ alert("信息不能为空"); return false; } }); }); </script> </head> <body> <form action="add" method="get" > <table border="1px" align="center"> <tr> <td colspan="2" style="text-align:center; font-size:30px;font-weight:bold"> 花卉信息 </td> </tr> <tr> <td>花卉名称</td> <td> <input type="text" name="name" value=""> </td> </tr> <tr> <td>花卉价格</td> <td> <input type="text" name="price" value=""> </td> </tr> <tr> <td>花卉产地</td> <td> <input type="text" name="production" value=""> </td> </tr> <tr> <td> <input type="submit" value="提交"><input type="reset" value="重置"> </td> </tr> </table> </form> </body> </html>
最新回复(0)