分类管理:查询所有分类
准备工作:
创建数据库表、javabean、dao接口和实现类、service接口和实现类
步骤一:创建分类表
步骤二:创建javabean Category
步骤三:编写dao接口、及其实现类
步骤四:编写service
代码实现
步骤一:完善IndexServlet,显示/jsp/index.jsp查询分类
public class IndexServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ProductService service = new ProductService(); //查询所有分类 //List<Category> categoryList = service.findAllCategory();
//将查询结果存放到request作用域
//request.setAttribute("categoryList", categoryList);request.getRequestDispatcher("/index.jsp").forward(request, response);}
步骤二:完善CategoryService,提供findAll()方法
实现类
public List<Category> findAllCategory() {
ProductDao dao = new ProductDao(); List<Category> categoryList = null; try { categoryList = dao.findAllCategory(); } catch (SQLException e) { e.printStackTrace(); } return categoryList; }
步骤三:完善CategoryDao,提供findAll()方法
public List<Category> findAllCategory() throws SQLException { QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource()); String sql = "select * from category"; return runner.query(sql, new BeanListHandler<Category>(Category.class)); }
步骤四:遍历数据
<ul class="nav navbar-nav" id="categoryUl"> <%-- <c:forEach items="${categoryList }" var="category"> <li><a href="#">${category.cname }</a></li> </c:forEach> --%> </ul>
优化:Ajax异步加载
1、修改header.jsp。添加js函数,页面加载发送ajax查询所有分类
2、编写CategoryServlet,提供findAll()方法
增强:缓存技术
代码实现
redis.properties