💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# **一、本节任务:使用MVC模式完成框架搭建** 在现有项目的src目录下分别创建如下包。 包的命名规范为:com.姓名汉语拼音**加学号后三位**.功能模块英文名称 比如:com.baishenghua100.pojo **1.创建pojo包** **com.baishenghua100.pojo** **2.创建utils包** **com.baishenghua100.utils** **3.创建dao包** **com.baishenghua100.dao** **4.创建dao的实现包impl** **com.baishenghua100.dao.impl** **5.创建controller包** **com.baishenghua100.controller** **6.创建services包** **com.baishenghua100.services** **7.创建services包的实现包impl** **com.baishenghua100.services.impl** **8.创建测试包test** **com.baishenghua100.test** **9.创建过滤器包filter** **com.baishenghua100.filter** 然后分别在各个包下创建对应的类和接口 # **二、具体实现步骤:** **1.创建pojo包** **com.baishenghua100.pojo** >创建导航菜单类Menu100.java 三个属性:菜单名称、菜单地址和菜单序号。属性名上也加上学号后三位 重写hashCode()和equals()方法 重写toString()方法 实现可序列化接口 创建有参和无参构造方法 ![](http://h.yiniuedu.com/c1ddc9a35b244ca11c17e53291013bc1) ``` package com.baishenghua100.pojo; import java.io.Serializable; /** * Menu100.java(导航菜单类) * @desc 导航菜单 * @author 柏圣华 * @date 2021-10-21 * */ public class Menu100 implements Serializable{ private static final long serialVersionUID = 1L; private int menuNo100;//导航菜单序号 private String menuName100;//导航菜单名称 private String menuURL100;//导航菜单地址 public Menu100() { super(); } public Menu100(int menuNo100, String menuName100, String menuURL100) { super(); this.menuNo100 = menuNo100; this.menuName100 = menuName100; this.menuURL100 = menuURL100; } @Override public String toString() { return "Menu100 [menuNo100=" + menuNo100 + ", menuName100=" + menuName100 + ", menuURL100=" + menuURL100 + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((menuName100 == null) ? 0 : menuName100.hashCode()); result = prime * result + menuNo100; result = prime * result + ((menuURL100 == null) ? 0 : menuURL100.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Menu100 other = (Menu100) obj; if (menuName100 == null) { if (other.menuName100 != null) return false; } else if (!menuName100.equals(other.menuName100)) return false; if (menuNo100 != other.menuNo100) return false; if (menuURL100 == null) { if (other.menuURL100 != null) return false; } else if (!menuURL100.equals(other.menuURL100)) return false; return true; } public int getMenuNo100() { return menuNo100; } public void setMenuNo100(int menuNo100) { this.menuNo100 = menuNo100; } public String getMenuName100() { return menuName100; } public void setMenuName100(String menuName100) { this.menuName100 = menuName100; } public String getMenuURL100() { return menuURL100; } public void setMenuURL100(String menuURL100) { this.menuURL100 = menuURL100; } public static long getSerialversionuid() { return serialVersionUID; } } ``` **2.创建utils包** **com.baishenghua100.utils** >创建图片验证码类,首先这个类是一个servlet。命名为:CheckCode100.java 参见图片[验证码类](http://cms.yiniuedu.com/javawebstudy/2472193) ![](http://h.yiniuedu.com/30af3b8ad55aa9ed52cbe7361317e41a) **3.创建dao包** **com.baishenghua100.dao** >创建IMenuDao100接口,里面定义增删改查和一些复杂查询方法 查询所有菜单 根据条件查询部分菜单 查询指定的某个菜单 更新菜单 添加菜单 删除菜单 ![](http://h.yiniuedu.com/10fdc43cb49f62ae4259eab95cc6c686) ``` package com.baishenghua100.dao; import java.util.List; import com.baishenghua100.pojo.Menu100; /** * IMenuDao100.java(导航菜单数据处理接口) * @desc 主要定义一些方法:查询所有导航菜单、条件查询菜单、根据序号查询导航菜单以及添加、修改和删除菜单。 * @author 柏圣华 * @date 2021-10-21 * */ public interface IMenuDao100 { public List<Menu100> queryAll100();//查询所有导航菜单 public List<Menu100> queryByCond100(String condition100);//条件查询导航菜单 public Menu100 query100(Menu100 menu100);//根据序号查询导航菜单 public Menu100 add100(Menu100 menu100);//添加导航菜单 public Menu100 update100(Menu100 menu100);//修改导航菜单 public Menu100 delete100(Menu100 menu100);//删除导航菜单 } ``` **4.创建dao的实现包impl** **com.baishenghua100.dao.impl** >创建IMenuDao100接口的实现类MenuDaoImpl100类,并实现接口里面的所有方法 ![](http://h.yiniuedu.com/86e741a1a281164375e3a8e1d1cc799b) ``` package com.baishenghua100.dao.impl; import java.util.List; import com.baishenghua100.dao.IMenuDao100; import com.baishenghua100.pojo.Menu100; /** * MenuDaoImpl100.java(导航菜单数据处理具体实现类) * @desc 主要实现接口里面的增删改查的方法,比如说,查询所有导航菜单、条件查询菜单以及添加、修改和删除菜单。 * @author 柏圣华 * @date 2021-10-21 * */ public class MenuDaoImpl100 implements IMenuDao100 { @Override public List<Menu100> queryAll100() { // TODO Auto-generated method stub return null; } @Override public List<Menu100> queryByCond100(String condition100) { // TODO Auto-generated method stub return null; } @Override public Menu100 query100(Menu100 menu100) { // TODO Auto-generated method stub return null; } @Override public Menu100 add100(Menu100 menu100) { // TODO Auto-generated method stub return null; } @Override public Menu100 update100(Menu100 menu100) { // TODO Auto-generated method stub return null; } @Override public Menu100 delete100(Menu100 menu100) { // TODO Auto-generated method stub return null; } } ``` **5.创建controller包** **com.baishenghua100.controller** >创建MenuServlet100 ![](http://h.yiniuedu.com/deec7108152ac5a68a0a487716f7611f) ``` package com.baishenghua100.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; /** * MenuServlet100.java(导航菜单控制器类) * @desc 主要控制导航菜单模型数据和视图展示 * @author 柏圣华 * @date 2021-10-21 * */ @WebServlet("/MenuServlet100") public class MenuServlet100 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public MenuServlet100() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @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); } } ``` **6.创建services包** **com.baishenghua100.services** >创建IMenuService100接口 ![](http://h.yiniuedu.com/71b53fef868cc2cfafd0ddd836ab6a1c) ``` package com.baishenghua100.services; import java.util.List; import com.baishenghua100.pojo.Menu100; /** * IMenuService100.java(导航菜单数据处理服务接口) * @desc 主要定义一些方法,比如说,查询所有导航菜单、条件查询菜单以及添加、修改和删除菜单。 * @author 柏圣华 * @date 2021-10-21 * */ public interface IMenuService100 { public List<Menu100> queryAll100();//查询所有导航菜单 public List<Menu100> queryByCond100(String condition100);//条件查询导航菜单 public Menu100 query100(Menu100 menu100);//根据序号查询导航菜单 public Menu100 add100(Menu100 menu100);//添加导航菜单 public Menu100 update100(Menu100 menu100);//修改导航菜单 public Menu100 delete100(Menu100 menu100);//删除导航菜单 } ``` **7.创建services包的实现包impl** **com.baishenghua100.services.impl** >创建MenuServiceImpl100实现IMenuService100接口 ![](http://h.yiniuedu.com/3d243a9c0e4799b66a3a5c54a6f25665) ``` package com.baishenghua100.services.impl; import java.util.List; import com.baishenghua100.pojo.Menu100; import com.baishenghua100.services.IMenuService100; /** * MenuServiceImpl100.java(导航菜单数据处理服务具体实现类) * @desc 主要调用IMenDao100接口里面的方法,具体调用IMenDao100接口实现类MenuDaoImpl100类里面的方法 * @author 柏圣华 * @date 2021-10-21 * */ public class MenuServiceImpl100 implements IMenuService100 { IMenuDao100 iMenuDao100 = new MenuDaoImpl100(); @Override public List<Menu100> queryAll100() { return iMenuDao100.queryAll100(); } @Override public List<Menu100> queryByCond100(String condition100) { return iMenuDao100.queryByCond100(condition100); } @Override public Menu100 query100(Menu100 menu100) { return iMenuDao100.query100(menu100); } @Override public Menu100 add100(Menu100 menu100) { return iMenuDao100.add100(menu100); } @Override public Menu100 update100(Menu100 menu100) { return iMenuDao100.update100(menu100); } @Override public Menu100 delete100(Menu100 menu100) { return iMenuDao100.delete100(menu100); } } ``` **8.创建测试包test** **com.baishenghua100.test** >创建MenuServiceImpl100的测试类MenuServiceImplTest100 创建一个JUnit Test case,名字为MenuServiceImplTest100 在class under test下选择MenuServiceImpl100这个类,然后选择要测试哪些方法 ![](http://h.yiniuedu.com/b57157abb0ccef41a80c9bfa572f0f75) ``` package com.baishenghua100.test; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; /** * MenuServiceImplTest100.java(导航菜单数据处理服务具体实现类的测试类) * @desc 主要测试实现类MenuServiceImpl100类里面的方法,有没有实现 * @author 柏圣华 * @date 2021-10-21 * */ class MenuServiceImplTest100 { @BeforeEach void setUp() throws Exception { } @AfterEach void tearDown() throws Exception { } @Test void testQueryAll100() { fail("Not yet implemented"); } @Test void testQueryByCond100() { fail("Not yet implemented"); } @Test void testQuery100() { fail("Not yet implemented"); } @Test void testAdd100() { fail("Not yet implemented"); } @Test void testUpdate100() { fail("Not yet implemented"); } @Test void testDelete100() { fail("Not yet implemented"); } } ``` **9.创建过滤器包filter** **com.baishenghua100.filter** >创建字符编码过滤器类CharacterEncodingFilter100类 注意此处要创建一个Filter ![](http://h.yiniuedu.com/8e8187875d2e80475373d6801fdaec90) ``` package com.baishenghua100.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * CharacterEncodingFilter100.java(字符编码过滤器类) * @desc 主要统一字符编码 * @author 柏圣华 * @date 2021-10-21 * */ @WebFilter(urlPatterns="/*",initParams= {@WebInitParam(name="CharsetEncoding",value="utf-8")}) public class CharacterEncodingFilter100 implements Filter { private static String encoding;//定义变量接收初始化的值 /** * Default constructor. */ public CharacterEncodingFilter100() { // TODO Auto-generated constructor stub } /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)req; HttpServletResponse response = (HttpServletResponse)res; //设置字符编码链锁 request.setCharacterEncoding(encoding); response.setCharacterEncoding(encoding); chain.doFilter(request, response); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { //接收web.xml配置文件中或者注解中的初始参数 encoding = fConfig.getInitParameter("CharsetEncoding"); } } ```