src/main/java/db
DBConn.java
package db; import java.sql.*; public class DBConn { public Connection getConnection() { String URL = "jdbc:mysql://localhost:3306/bigproject?characterEncoding=utf-8"; String USER = "root"; String PASSWORD = "111111"; try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection conn = DriverManager.getConnection(URL, USER, PASSWORD); return conn; } catch (ClassNotFoundException e) { e.printStackTrace(); }catch (SQLException e) { e.printStackTrace(); } return null; } }
src/main/java/service
GoodsService.java
package service; import java.sql.SQLException; import java.util.List; import bean.*; /** * 这是商品的业务处理规范 * */ public interface GoodsService { PageBean findGoodsByPage(int currentPage) throws SQLException ; List<Goods> findAll() throws SQLException ; /** * 根据ID查询单个商品对象 */ Goods findGoodsById(int id) throws SQLException ; /** * 模糊查询, 根据姓名或者根据性别,或者两者兼有。 */ List<Goods> searchGoods(String gname , double price) throws SQLException ; /** * 添加商品 */ void insert(Goods goods) throws SQLException ; /** * 根据id删除商品 */ void delete(int id) throws SQLException ; }
src/main/java/service/impl
GoodsServiceImpl.java
package service.impl; import java.sql.SQLException; import java.util.List; import dao.*; import dao.impl.*; import bean.*; import service.*; /** * 这是商品业务实现 * */ public class GoodsServiceImpl implements GoodsService{ @Override public List<Goods> findAll() throws SQLException { GoodsDao dao = new GoodsDaoImpl(); return dao.findAll(); } @Override public void insert(Goods goods) throws SQLException { GoodsDao dao = new GoodsDaoImpl(); dao.insert(goods); } @Override public void delete(int id) throws SQLException { GoodsDao dao = new GoodsDaoImpl(); dao.delete(id); } @Override public Goods findGoodsById(int id) throws SQLException { GoodsDao dao = new GoodsDaoImpl(); return dao.findGoodsById(id); } @Override public List<Goods> searchGoods(String gname, double price) throws SQLException { return new GoodsDaoImpl().searchGoods(gname, price); } @Override public PageBean<Goods> findGoodsByPage(int currentPage) throws SQLException { //封装分页的该页数据 PageBean<Goods> pageBean = new PageBean<Goods>(); int pageSize = GoodsDao.PAGE_SIZE ; pageBean.setCurrentPage(currentPage); //设置当前页 pageBean.setPageSize(pageSize); //设置每页显示多少记录 GoodsDao dao = new GoodsDaoImpl() ; List<Goods> list =dao.findGoodsByPage(currentPage); pageBean.setList(list); //设置这一页的商品数据 //总的记录数, 总的页数。 int count = dao.findCount(); pageBean.setTotalSize(count); //设置总的记录数 //200 , 10 ==20 201 , 10 = 21 201 % 10 == 0 ?201 / 10 :201 % 10 + 1 pageBean.setTotalPage(count % pageSize==0 ? count / pageSize : (count / pageSize) + 1); //总页数 return pageBean; } }
src/main/java/servlet
AddServlet.java
package servlet; import java.io.IOException; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import bean.Goods; import service.GoodsService; import service.impl.GoodsServiceImpl; /** * 用于处理商品的添加请求 * */ public class AddServlet extends HttpServlet { @Override public void init() throws ServletException{ super.init(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); try { //1. 获取客户端提交上来的数据 String gname = request.getParameter("gname"); //sname:zhangsan String sprice = request.getParameter("price"); double price=Double.parseDouble(sprice); Goods goods = new Goods(gname,price); GoodsService service = new GoodsServiceImpl(); service.insert(goods); request.getRequestDispatcher("GoodsListServlet").forward(request, response); } catch (Exception e) { e.printStackTrace(); } } /** * @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.java
package servlet; import java.io.IOException; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import service.GoodsService; import service.impl.GoodsServiceImpl; /** * 用于处理删除 * */ public class DeleteServlet extends HttpServlet { @Override public void init() throws ServletException{ super.init(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { int id = Integer.parseInt(request.getParameter("id")); //2. 执行删除 GoodsService service = new GoodsServiceImpl(); service.delete(id); //3. 跳转到列表页。 request.getRequestDispatcher("GoodsListServlet").forward(request, response); } catch (SQLException e) { e.printStackTrace(); } } /** * @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); } }
GoodsListPageServlet.java
package servlet; import java.io.IOException; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.catalina.valves.StuckThreadDetectionValve; import bean.*; import service.GoodsService; import service.impl.GoodsServiceImpl; /** * 这是用于分页显示商品列表的servlet * */ public class GoodsListPageServlet extends HttpServlet { @Override public void init() throws ServletException{ super.init(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { int currentPage =Integer.parseInt( request.getParameter("currentPage")); //2. 根据指定的页数,去获取该页的数据回来 //List<Goods> --- list.jsp GoodsService service = new GoodsServiceImpl(); PageBean<Goods> pageBean= service.findGoodsByPage(currentPage); request.setAttribute("pageBean", pageBean); //3. 跳转界面。 request.getRequestDispatcher("list_page.jsp").forward(request, response); } catch (SQLException e) { e.printStackTrace(); } } /** * @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); } }
GoodsListServlet.java
package servlet; import java.io.IOException; import java.sql.SQLException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import bean.Goods; import service.GoodsService; import service.impl.GoodsServiceImpl; /** * 负责查询所有的商品信息。 然后呈现到list.jsp页面上。 */ public class GoodsListServlet extends HttpServlet { @Override public void init() throws ServletException{ super.init(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { //1. 查询出来所有的商品 GoodsService service = new GoodsServiceImpl(); List<Goods> list = service.findAll(); //2. 先把数据存储到作用域中 request.setAttribute("list", list); //3. 跳转页面 request.getRequestDispatcher("list.jsp").forward(request, response); } catch (SQLException e) { e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
SearchGoodsServlet.java
package servlet; import java.io.IOException; import java.sql.SQLException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.catalina.valves.StuckThreadDetectionValve; import bean.Goods; import service.GoodsService; import service.impl.GoodsServiceImpl; /** * Servlet implementation class SearchStudentServlet */ @SuppressWarnings("serial") public class SearchGoodsServlet extends HttpServlet { @Override public void init() throws ServletException{ super.init(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); try { //1. 取到了要查询的关键数据 姓名 , 性别。 String gname= request.getParameter("gname"); String sprice = request.getParameter("price"); double price=Double.parseDouble(sprice); //2. 找service去查询 GoodsService service = new GoodsServiceImpl(); List<Goods> list = service.searchGoods(gname, price); System.out.println("list的大小是:"+list.size()); for (Goods goods : list) { System.out.println("goods="+goods); } request.setAttribute("list", list); //3. 跳转界面。列表界面 request.getRequestDispatcher("list.jsp").forward(request, response); } catch (SQLException e) { e.printStackTrace(); } } /** * @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); } }