* 在controller类中,应先定义一个ArrayList的集合即就是:
List<Product> defaultProductList = new ArrayList(); // 实体类未Product
其他的模块如下
·· dao、service都是定义的方法名
·· service的实现类返回dao的方法,即就是:
package com.jbjf.work.fourierbackstage.service.Impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.jbjf.work.fourierbackstage.dao.ProductDao; import com.jbjf.work.fourierbackstage.model.Product; import com.jbjf.work.fourierbackstage.service.ProductService; @Service("productService") public class ProductServiceImpl implements ProductService { @Autowired private ProductDao productDao; @Override public List<Product> defaultProductList() { return productDao.defaultProductList(); } }
··web层,就是具体的实现,其将数据库的操作交给对应的mapper
package com.jbjf.work.fourierbackstage.web; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.jbjf.work.fourierbackstage.model.Product; import com.jbjf.work.fourierbackstage.service.ProductService; @Controller public class ProductController { @Autowired private ProductService productService; @RequestMapping("productList") public String productList(HttpServletRequest request){ List<Product> defaultProductList = new ArrayList(); defaultProductList = productService.defaultProductList(); int productCount = productService.getProductCount(); request.setAttribute("defaultProductList", defaultProductList); return "productList"; } }
mapper:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.jbjf.work.fourierbackstage.dao.ProductDao"> <select id="defaultProductList" resultType="com.jbjf.work.fourierbackstage.model.Product"> select * from jbjf_product </select> </mapper>