MVC框架基础练习

mvc框架:应用于桌面程序

MVC框架基础练习_第1张图片

Model模型:是应用程序的主体部分,主要包括业务逻辑模块(web项目中的service和dao类)和数据模块(vo类)模型与数据格式无关,这样一个模型能为多个视图提供数据,由于应用于模型的代码只需要写一次就可以被多个视图重用,所以减少了代码的重复行
view视图:用户与之交互的界面,在web项目中一般有jsp,html组成
controller控制器:接收来自界面的请求并交给模型,进行处理,在这个过程中控制器不做
任何处理,只是起到了一个连接的作用
优点:

    1.低耦合性:视图层和业务层分离,很容易改变应用程序的数据层和业务规则
    2.高重用性和可适用性,允许各种不同样式的视图来访问一个服务段的代码
    3.较低的生命周期成本成本,mvc可降低开发和维护用户接口的技术含量
    4.部署方便-
    由MVC架构衍生出的三层架构
    1.表现层:接收请求,封装数据,调用业务逻辑层,响应数据
    2.业务逻辑层:对业务逻辑进行封装,组合数据持久层中基本功能,形成复杂的业务逻辑。
    例如:注册业务,先查询是否存在,再进行添加
    3.数据持久层:对数据库进行增删改查等基本操作

mvc基础模式练习

//register注册界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


用户注册





<%if(request.getAttribute("mag")!=null){ %> <%=request.getAttribute("mag") %> <%} %> //success.jsp文件,注册成功页面 <%@page import="demo.vo.Customer"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> Insert title here <%=request.getAttribute("mag") %> <%List list=(List)request.getAttribute("list"); %> <%if(list!=null&&list.size()!=0){ %> <%for(Customer c:list){ %> <%} %>
id name age account
> <%=c.getId() %> <%=c.getName() %> <%=c.getAge() %> <%=c.getAccount() %>
<%} %> //update修改界面 <%@page import="demo.vo.Customer"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> Insert title here

修改顾客信息

<%Customer c=(Customer)request.getAttribute("customer"); %>
姓名
年龄
账号
密码
//servlet表现层 //RegisterServlet package demo.controller; import java.io.IOException; import java.util.List; 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 demo.service.CustomerService; import demo.vo.Customer; /** * Servlet implementation class RegisterServlet */ @WebServlet("/RegisterServlet") public class RegisterServlet extends HttpServlet { private static final long serialVersionUID = 1L; CustomerService service=new CustomerService(); /** * @see HttpServlet#HttpServlet() */ public RegisterServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name=request.getParameter("name"); int age=Integer.parseInt(request.getParameter("age")); String account= request.getParameter("account"); String pwd=request.getParameter("pwd"); Customer customer=new Customer(1,name,age,account,pwd); if(service.register(customer)) { // 注册成功,跳转到成功页面 request.setAttribute("mag","注册成功"); request.getRequestDispatcher("success.jsp").forward(request, response); }else { // 注册失败,跳回到注册页面,并显示账号以存在 request.setAttribute("mag", "账号已存在"); request.getRequestDispatcher("register.jsp").forward(request, response); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } } //ShowAllServlet package demo.controller; import java.io.IOException; import java.util.List; 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 demo.dao.CustomerDao; import demo.service.CustomerService; import demo.vo.Customer; /** * Servlet implementation class ShowAllServlet */ @WebServlet("/ShowAllServlet") public class ShowAllServlet extends HttpServlet { private static final long serialVersionUID = 1L; CustomerService service=new CustomerService(); /** * @see HttpServlet#HttpServlet() */ public ShowAllServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List list=service.getAllCustomer(); request.setAttribute("list", list); if(request.getAttribute("mag")!=null) { request.setAttribute("mag", request.getAttribute("mag")); } request.getRequestDispatcher("success.jsp").forward(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } } //UpdateServlet package demo.controller; import java.io.IOException; 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 demo.service.CustomerService; import demo.vo.Customer; /** * Servlet implementation class UpdateServlet */ @WebServlet("/UpdateServlet") public class UpdateServlet extends HttpServlet { private static final long serialVersionUID = 1L; CustomerService service=new CustomerService(); /** * @see HttpServlet#HttpServlet() */ public UpdateServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取要修改的顾客id int id=Integer.parseInt(request.getParameter("id")); // 现将顾客的信息获取,传递到页面中,作为默认数据进行修改 // 根据id查询顾客对象业务 Customer customer=service.getCustomerById(id); request.setAttribute("customer", customer); request.getRequestDispatcher("update.jsp").forward(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } } //Update01Servlet package demo.controller; import java.io.IOException; 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 demo.service.CustomerService; import demo.vo.Customer; /** * Servlet implementation class Update01Servlet */ @WebServlet("/Update01Servlet") public class Update01Servlet extends HttpServlet { private static final long serialVersionUID = 1L; CustomerService service=new CustomerService(); /** * @see HttpServlet#HttpServlet() */ public Update01Servlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int id=0; int age=0; // 先判断id和age是否能够进行数字转换 if(request.getParameter("id")!=null&&request.getParameter("age")!=null && !"".equals(request.getParameter("age"))) { id=Integer.parseInt(request.getParameter("id")); age=Integer.parseInt(request.getParameter("age")); } String name=request.getParameter("name"); String account=request.getParameter("account"); String pwd=request.getParameter("pwd"); // 再判断字符串属性不是空串 if(!"".equals(name)&&!"".equals(pwd)&&!"".equals(account)) { Customer c=new Customer(id,name,age,account,pwd); // 调用修改业务 if(service.updateCustomer(c)) { request.setAttribute("mag", "修改成功"); }else { request.setAttribute("mag", "修改失败"); } }else { request.setAttribute("mag", "修改失败"); } request.getRequestDispatcher("ShowAllServlet").forward(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } } //DeleteServlet package demo.controller; import java.io.IOException; 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 demo.service.CustomerService; /** * Servlet implementation class DeleteServlet */ @WebServlet("/DeleteServlet") public class DeleteServlet extends HttpServlet { private static final long serialVersionUID = 1L; CustomerService service=new CustomerService(); /** * @see HttpServlet#HttpServlet() */ public DeleteServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int id=Integer.parseInt(request.getParameter("id")); if(service.deleteCustomerById(id)) { request.setAttribute("mag", "删除成功"); }else { request.setAttribute("mag", "删除失败"); } request.getRequestDispatcher("ShowAllServlet").forward(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } } //DeleteServlet01 package demo.controller; import java.io.IOException; import java.util.List; 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 demo.service.CustomerService; /** * Servlet implementation class DeleteServlet01 */ @WebServlet("/DeleteServlet01") public class DeleteServlet01 extends HttpServlet { private static final long serialVersionUID = 1L; CustomerService service=new CustomerService(); /** * @see HttpServlet#HttpServlet() */ public DeleteServlet01() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String[] str=request.getParameterValues("san"); int[] in=new int[str.length]; if(str.length!=0) { for(int i=0;i getAllCustomer(){ return dao.getCustomer(); } // 修改业务 public boolean updateCustomer(Customer customer) { return dao.updateCustomer(customer); } // 删除业务 public boolean deleteCustomerById(int id) { return dao.deleteCustomerById(id); } } //数据持久层dao package demo.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import demo.common.DBUtils; import demo.vo.Customer; public class CustomerDao { //查询注册的账号是否重复 public boolean selectCustomerByAccount(String account) { // 1加载驱动2.获得连接3.执行sql语句处理4.关闭资源 Connection conn=DBUtils.getConn(); String sql="select account from customer where account=?"; PreparedStatement ps=null; ResultSet rs=null; try { ps=conn.prepareStatement(sql); ps.setString(1, account); rs=ps.executeQuery(); if(rs.next()) { return true; } return false; }catch(Exception e) { e.printStackTrace(); return false; }finally { DBUtils.close(conn, ps, rs); } } // 插入注册的数据 public boolean insertCustomer(Customer customer) { Connection conn =DBUtils.getConn(); PreparedStatement ps=null; String sql="insert into customer values(null,?,?,?,?)"; try { ps=conn.prepareStatement(sql); ps.setString(1,customer.getName()); ps.setInt(2, customer.getAge()); ps.setString(3,customer.getAccount()); ps.setString(4,customer.getPwd()); if(ps.executeUpdate()>0) { return true; } return false; }catch(Exception e) { e.printStackTrace(); return false; }finally { DBUtils.close(conn, ps, null); } } // 一览数据 public List getCustomer() { Connection conn =DBUtils.getConn(); PreparedStatement ps=null; ResultSet rs=null; List list=new ArrayList(); String sql="select* from customer"; try { ps=conn.prepareStatement(sql); rs=ps.executeQuery(); while(rs.next()) { int id=rs.getInt(1); String name=rs.getString(2); int age=rs.getInt(3); String account=rs.getString(4); String pwd=rs.getString(5); Customer customer=new Customer(id,name,age,account,pwd); list.add(customer); } return list; }catch(Exception e) { e.printStackTrace(); return null; }finally { DBUtils.close(conn, ps, rs); } } // 根据id查询Customer public Customer getCustomerById(int id) { Connection conn=DBUtils.getConn(); PreparedStatement ps=null; ResultSet rs=null; String sql="select *from customer where id=?"; try { ps=conn.prepareStatement(sql); ps.setInt(1, id); rs=ps.executeQuery(); if(rs.next()) { Customer customer=new Customer(rs.getInt("id"), rs.getString("name"),rs.getInt("age"),rs.getString(4),rs.getString(5)); return customer; } return null; } catch (Exception e) { // TODO: handle exception return null; }finally { DBUtils.close(conn, ps, rs); } } public boolean updateCustomer(Customer customer) { Connection conn=DBUtils.getConn(); PreparedStatement ps=null; String sql="update customer set name=?,age=?,account=?,pwd=? where id=? "; try { ps=conn.prepareStatement(sql); ps.setString(1, customer.getName()); ps.setInt(2, customer.getAge()); ps.setString(3, customer.getAccount()); ps.setString(4, customer.getPwd()); ps.setInt(5, customer.getId()); if(ps.executeUpdate()>0) { return true; } return false; }catch (Exception e) { e.printStackTrace(); return false; } finally { DBUtils.close(conn, ps, null); } } // 删除 public boolean deleteCustomerById(int id) { Connection conn=DBUtils.getConn(); PreparedStatement ps=null; String sql="delete from customer where id=?"; try { ps=conn.prepareStatement(sql); ps.setInt(1, id); if(ps.executeUpdate()>0) { return true; } return false; }catch (Exception e) { e.printStackTrace(); return false; } finally { DBUtils.close(conn, ps, null); } } } //数据库连接封装 package demo.common; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class DBUtils { // 获取Connection对象(数据库连接相关数据可通过参数控制) public static Connection getConn(String driver,String url,String user,String pwd) { try { Class.forName(driver); Connection conn = DriverManager.getConnection(url,user,pwd); return conn; }catch(Exception e) { return null; } } // 固定数据库连接参数获取Connection对象 public static Connection getConn() { try { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/mysql2209?useUnicode=true&characterEncoding=utf8", "root", "root" ); return conn; }catch(Exception e) { return null; } } // 关流 public static void close(Connection conn,PreparedStatement ps,ResultSet rs) { try { if(rs!=null)rs.close(); if(ps!=null)ps.close(); if(conn!=null)conn.close(); }catch(Exception e) { } } } //.vo类对象 package demo.vo; public class Customer { private int id; private String name; private int age; private String account; private String pwd; public Customer() {} public Customer(int id, String name, int age, String account, String pwd) { super(); this.id = id; this.name = name; this.age = age; this.account = account; this.pwd = pwd; } @Override public String toString() { return "Customer [id=" + id + ", name=" + name + ", age=" + age + ", account=" + account + ", pwd=" + pwd + "]"; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }

你可能感兴趣的:(mvc,java,数据库)