MVC设计模式-练习一

MVC 模式:

  • Model(模型) - 模型就是数据,模型代表了web应用中的核心功能,包括业务逻辑层和数据库访问层
  • View(视图) -  视图就是网页, JSP,用来展示模型中的数据.主要是用户看到并与之交互的界面,即Java web应用程序的外观
  • Controller(控制器) - 控制器用来把不同的数据,显示在不同的视图上。控制器主要负责交互和将用户输入的数据导入模型.

一.操作

  1. 新建数据库和表MVC设计模式-练习一_第1张图片
  2. 导入以下文件到eclipse中的lib里MVC设计模式-练习一_第2张图片MVC设计模式-练习一_第3张图片
  3. 导入servlet-api.jar到项目中MVC设计模式-练习一_第4张图片
  4. 新建实体类Hero
    package bean;
     
    public class Hero {
     
        public int id;
        public String name;
        public float hp;
        public int damage;
    	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 float getHp() {
    		return hp;
    	}
    	public void setHp(float hp) {
    		this.hp = hp;
    	}
    	public int getDamage() {
    		return damage;
    	}
    	public void setDamage(int damage) {
    		this.damage = damage;
    	}
         
    }
    

  5. 创建HeroDAO.java,用于从数据库查询数据
    package dao;
      
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.List;
     
    import bean.Hero;
      
    public class HeroDAO {
      
        public HeroDAO() {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
      
        public Connection getConnection() throws SQLException {
            return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8", "root",
                    "admin");
        }
      
        public int getTotal() {
            int total = 0;
            try (Connection c = getConnection(); Statement s = c.createStatement();) {
      
                String sql = "select count(*) from hero";
      
                ResultSet rs = s.executeQuery(sql);
                while (rs.next()) {
                    total = rs.getInt(1);
                }
      
                System.out.println("total:" + total);
      
            } catch (SQLException e) {
      
                e.printStackTrace();
            }
            return total;
        }
      
        public void add(Hero hero) {
      
            String sql = "insert into hero values(null,?,?,?)";
            try (Connection c = getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
      
                ps.setString(1, hero.name);
                ps.setFloat(2, hero.hp);
                ps.setInt(3, hero.damage);
      
                ps.execute();
      
                ResultSet rs = ps.getGeneratedKeys();
                if (rs.next()) {
                    int id = rs.getInt(1);
                    hero.id = id;
                }
            } catch (SQLException e) {
      
                e.printStackTrace();
            }
        }
      
        public void update(Hero hero) {
      
            String sql = "update hero set name= ?, hp = ? , damage = ? where id = ?";
            try (Connection c = getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
      
                ps.setString(1, hero.name);
                ps.setFloat(2, hero.hp);
                ps.setInt(3, hero.damage);
                ps.setInt(4, hero.id);
      
                ps.execute();
      
            } catch (SQLException e) {
      
                e.printStackTrace();
            }
      
        }
      
        public void delete(int id) {
      
            try (Connection c = getConnection(); Statement s = c.createStatement();) {
      
                String sql = "delete from hero where id = " + id;
      
                s.execute(sql);
      
            } catch (SQLException e) {
      
                e.printStackTrace();
            }
        }
      
        public Hero get(int id) {
            Hero hero = null;
      
            try (Connection c = getConnection(); Statement s = c.createStatement();) {
      
                String sql = "select * from hero where id = " + id;
      
                ResultSet rs = s.executeQuery(sql);
      
                if (rs.next()) {
                    hero = new Hero();
                    String name = rs.getString(2);
                    float hp = rs.getFloat("hp");
                    int damage = rs.getInt(4);
                    hero.name = name;
                    hero.hp = hp;
                    hero.damage = damage;
                    hero.id = id;
                }
      
            } catch (SQLException e) {
      
                e.printStackTrace();
            }
            return hero;
        }
      
        public List list() {
            return list(0, Short.MAX_VALUE);
        }
      
        public List list(int start, int count) {
            List heros = new ArrayList();
      
            String sql = "select * from hero order by id desc limit ?,? ";
      
            try (Connection c = getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
      
                ps.setInt(1, start);
                ps.setInt(2, count);
      
                ResultSet rs = ps.executeQuery();
      
                while (rs.next()) {
                    Hero hero = new Hero();
                    int id = rs.getInt(1);
                    String name = rs.getString(2);
                    float hp = rs.getFloat("hp");
                    int damage = rs.getInt(4);
                    hero.id = id;
                    hero.name = name;
                    hero.hp = hp;
                    hero.damage = damage;
                    heros.add(hero);
                }
            } catch (SQLException e) {
      
                e.printStackTrace();
            }
            return heros;
        }
      
    }
    

  6. 创建listHero.jsp
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    	pageEncoding="UTF-8" import="java.util.*"%>
    
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    
    
    		
    id name hp damage edit delete
    ${hero.id} ${hero.name} ${hero.hp} ${hero.damage} edit delete

  7. 新建servlet
    package servlet;
    
    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 bean.Hero;
    import dao.HeroDAO;
    
    /**
     * Servlet implementation class HeroListServlet
     */
    @WebServlet("/HeroListServlet")
    public class HeroListServlet extends HttpServlet {
    	protected void service(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
        List heros = new HeroDAO().list();
            request.setAttribute("heros", heros);
            request.getRequestDispatcher("listHero.jsp").forward(request, response);
     
        }
    }
    

 按照老师的方法创建servlet,然后运行即可

最后运行成功图如下:

MVC设计模式-练习一_第5张图片

 二.总结

        看着how2j和老师的教程把流程做了一遍,虽然中间没有遇到太大的问题,但是并没有彻底理解清楚其中的每一步,只是对MVC设计模式有了一点印象,但是自己讲不清楚为什么.后面要多看一些教程,然后自己重新做一遍,更好地体会MVC的内涵.

你可能感兴趣的:(mvc,设计模式,java,web,servlet)