第9章 前端系統(tǒng)的展示

首頁后臺開發(fā)

Dao層的實(shí)現(xiàn)

  • 新建一個(gè)HeadLineDao接口
public interface HeadLineDao {
    /**
     * 根據(jù)傳入的條件查詢
     * @param headLineCondition
     * @return
     */
    List<HeadLine> queryHeadLine(@Param("headLineCondition") HeadLine headLineCondition);
}
  • 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.imooc.o2o.dao.HeadLineDao">

    <select id="queryHeadLine" resultType="com.imooc.o2o.entity.HeadLine">
        SELECT
        line_id,
        line_name,
        line_link,
        line_img,
        priority,
        enable_status,
        create_time,
        last_edit_time
        FROM
        tb_head_line
        <where>
           <if test="headLineCondition.enableStatus != null">
               AND enable_status = #{headLineCondition.enableStatus}
           </if>
        </where>
        ORDER BY
        priority DESC
    </select>
</mapper>
  • 測試
    發(fā)現(xiàn)數(shù)據(jù)哭著少了一列
ALTER TABLE tb_head_line ADD COLUMN line_img varchar(1000) DEFAULT NULL;

測試單元

    @Test
    public void testQueryArea() {
        HeadLine headLineCondition = new HeadLine();
        List<HeadLine> headLineList = headLineDao.queryHeadLine(headLineCondition);
        System.out.println(headLineList.size());
    }
image.png

ShopCategoryDao的改造

  • 支持 parent_Id為空的情況奔害,返回一級level
<mapper namespace="com.imooc.o2o.dao.ShopCategoryDao">
    <select id="queryShopCategory" resultType="com.imooc.o2o.entity.ShopCategory">
        SELECT
        shop_category_id,
        shop_category_name,
        shop_category_desc,
        shop_category_img,
        priority,
        create_time,
        last_edit_time,
        parent_id
        FROM
        tb_shop_category
        <where>
            <if test="shopCategoryCondition == null">
                and parent_id is null
            </if>
            <if test="shopCategoryCondition!= null">
                and parent_id is not null
            </if>
            <if test="shopCategoryCondition != null and shopCategoryCondition.parentId!=null">
                and parent_id = #{shopCategoryCondition.parentId}
            </if>
        </where>
        ORDER BY
        priority DESC
    </select>
  • 測試
    @Test
    public void testBQueryShopCategory() throws Exception {
        List<ShopCategory> shopCategoryList = shopCategoryDao.queryShopCategory(null);
        System.out.println(shopCategoryList.size());
    }
  • 測試結(jié)果


    image.png

Service層實(shí)現(xiàn)

  • 接口
public interface HeadLineService {
    /**
     * 根據(jù)傳入的條件返回指定的頭條列表
     * @param headLineCondition
     * @return
     * @throws IOException
     */
    List<HeadLine> getHeadLineList(HeadLine headLineCondition) throws IOException;
}
  • 實(shí)現(xiàn)類
@Service
public class getHeadLineList implements HeadLineService{
    @Autowired
    private HeadLineDao headLineDao;
    
    @Override
    public List<HeadLine> getHeadLineList(HeadLine headLineCondition) throws IOException {
        List<HeadLine>  headLineList = headLineDao.queryHeadLine(headLineCondition);
        return  headLineList;
    }
}

Controller層的實(shí)現(xiàn)

  • 新建在web下frontend包楷兽,新建MainPageController類
@Controller
@RequestMapping("/frontend")
public class MainPageController {
    @Autowired
    private ShopCategoryService shopCategoryService;

    @Autowired
    private HeadLineService headLineService;

    /**
     * 初始化前端展示的系統(tǒng)的主頁信息 包括獲取一級店鋪類別列表一級頭條
     * @return
     */
    @RequestMapping(value = "/listmainpageinfo", method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> listMainPageInfo() {
        Map<String, Object> modelMap = new HashMap<>();
        List<ShopCategory> shopCategoryList = new ArrayList<>();

        try {
            // 獲取一級店鋪列表(即parentId為空的ShopCategory)
            shopCategoryList = shopCategoryService.getShopCategoryList(null);
            modelMap.put("shopCategoryList", shopCategoryList);
        } catch (Exception e) {
            modelMap.put("success", false);
            modelMap.put("errMsg", e.getMessage());
            return modelMap;
        }

        List<HeadLine> headLineList = new ArrayList<>();

        try {
            // 獲取狀態(tài)為1(可用的頭條列表)
            HeadLine headLineCondition = new HeadLine();
            headLineCondition.setEnableStatus(1);
            headLineList = headLineService.getHeadLineList(headLineCondition);
            modelMap.put("headLineList", headLineList);

        } catch (Exception e) {
            modelMap.put("success", false);
            modelMap.put("errMsg", e.getMessage());
            return modelMap;
        }

        modelMap.put("success", true);
        return modelMap;
    }

}
  • 測試


    image.png

前端頁面展示

  • 新建FrontendAdminControlle控制類
@Controller
@RequestMapping("/frontend")
public class FrontendAdminController {

    @RequestMapping(value = "index", method = RequestMethod.GET)
    private String index() {
        return "frontend/index";
    }
}

店鋪列表

Dao層的實(shí)現(xiàn)

  • 增加通過parent_id查詢
    <select id="queryShopList" resultMap="shopMap">
        SELECT
        s.shop_id,
        s.shop_name,
        s.shop_desc,
        s.phone,
        s.shop_img,
        s.priority,
        s.create_time,
        s.last_edit_time,
        s.enable_status,
        s.advice,
        a.area_id,
        a.area_name,
        sc.shop_category_id,
        sc.shop_category_name
        FROM
        tb_shop s,
        tb_area a,
        tb_shop_category sc
        <where>
            <if test="shopCondition.shopCategory != null and shopCondition.shopCategory.shopCategoryId != null">
                AND s.shop_category_id = #{shopCondition.shopCategory.shopCategoryId}
            </if>
            新增加的
            <if test="shopCondition.shopCategory != null 
                     and shopCondition.shopCategory.parent != null
                     and shopCondition.shopCategory.parent.shopCategoryId != null">
               AND s.shop_category_id IN (
                SELECT 
                shop_category_id 
                FROM 
                tb_shop_category
                WHERE 
                parent_id = #{shopCondition.shopCategory.parent.shopCategoryId}
                )
            </if>
            <if test="shopCondition.area != null and shopCondition.area.areaId != null">
                AND s.shop_area_id = #{shopCondition.areaId}
            </if>
            <if test="shopCondition.shopName != null">
                AND s.shop_name LIKE "%${shopCondition.shopName}%"
            </if>
            <if test="shopCondition.enableStatus != null">
                AND s.enable_status = #{shopCondition.enableStatus}
            </if>
            <if test="shopCondition.owner != null and shopCondition.owner.userId != null">
                AND s.owner_id = #{shopCondition.owner.userId}
            </if>
            AND
            s.area_id = a.area_id
            AND
            s.shop_category_id = sc.shop_category_id
    </where>
        ORDER BY
        s.priority DESC
        LIMIT #{rowIndex}, #{pageSize}
    </select>

同理count中也要增加

            <if test="shopCondition.shopCategory != null
                     and shopCondition.shopCategory.parent != null
                     and shopCondition.shopCategory.parent.shopCategoryId != null">
               AND s.shop_category_id IN (
                SELECT
                shop_category_id
                FROM
                tb_shop_category
                WHERE
                parent_id = #{shopCondition.shopCategory.parent.shopCategoryId}
                )
            </if>

Controller層的實(shí)現(xiàn)

  • ShopListController
@Controller
@RequestMapping(value ="/frontend")
public class ShopListController {
    @Autowired
    ShopCategoryService shopCategoryService;

    @Autowired
    AreaService areaService;

    @Autowired
    ShopService shopService;

    @RequestMapping(value = "/listshoppageinfo", method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> listShopPageInfo(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<>();
        // 試著從前端獲取parentId
        long parentId = HttpServletRequestUtil.getLong(request, "parent");
        List<ShopCategory> shopCategoryList = null;

        if (parentId != -1) {
            try {
                ShopCategory shopCategoryCondition = new ShopCategory();
                ShopCategory parent = new ShopCategory();
                parent.setShopCategoryId(parentId);
                shopCategoryCondition.setParent(parent);
                shopCategoryList = shopCategoryService.getShopCategoryList(shopCategoryCondition);
            } catch (Exception e) {
                modelMap.put("success", false);
                modelMap.put("errMsg", e.getMessage());
            }
        } else {
            try {
                // 如果parentId不存在 則取出所有一級ShopCategory
                shopCategoryList = shopCategoryService.getShopCategoryList(null);
            } catch (Exception e) {
                modelMap.put("success", false);
                modelMap.put("errMsg", e.getMessage());
            }
        }

        modelMap.put("shopCategoryList", shopCategoryList);

        List<Area>  areaList = null;

        try {
            // 獲取區(qū)域列表信息
            areaList = areaService.getAreaList();
            modelMap.put("areaList", areaList);
            modelMap.put("success", true);
        } catch (Exception e) {
            modelMap.put("success", false);
            modelMap.put("errMsg", e.getMessage());
        }

        return modelMap;
    }

    @RequestMapping(value = "/listshops", method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> listShops(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        int pageIndex = HttpServletRequestUtil.getInt(request, "pageIndex");
        int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
        if ((pageIndex > -1) && (pageSize > -1)) {
            long parentId = HttpServletRequestUtil.getLong(request, "parentId");
            long shopCategoryId = HttpServletRequestUtil.getLong(request,
                    "shopCategoryId");
            int areaId = HttpServletRequestUtil.getInt(request, "areaId");
            String shopName = HttpServletRequestUtil.getString(request,
                    "shopName");
            Shop shopCondition = compactShopCondition4Search(parentId,
                    shopCategoryId, areaId, shopName);
            ShopExecution se = shopService.getShopList(shopCondition,
                    pageIndex, pageSize);
            modelMap.put("shopList", se.getShopList());
            modelMap.put("count", se.getCount());
            modelMap.put("success", true);
        } else {
            modelMap.put("success", false);
            modelMap.put("errMsg", "empty pageSize or pageIndex");
        }

        return modelMap;
    }

    private Shop compactShopCondition4Search(long parentId,
                                             long shopCategoryId, int areaId, String shopName) {
        Shop shopCondition = new Shop();
        if (parentId != -1L) {
            ShopCategory parentCategory = new ShopCategory();
            parentCategory.setShopCategoryId(parentId);
            // TODO
            // shopCondition.setParentCategory(parentCategory);
        }
        if (shopCategoryId != -1L) {
            ShopCategory shopCategory = new ShopCategory();
            shopCategory.setShopCategoryId(shopCategoryId);
            shopCondition.setShopCategory(shopCategory);
        }
        if (areaId != -1L) {
            Area area = new Area();
            area.setAreaId(areaId);
            shopCondition.setArea(area);
        }

        if (shopName != null) {
            shopCondition.setShopName(shopName);
        }
        shopCondition.setEnableStatus(1);
        return shopCondition;
    }
}

-測試


image.png

返回頁面

  • 在FrontendAdminController中添加一個(gè)返回的頁面
    @RequestMapping(value = "shoplist", method = RequestMethod.GET)
    private String shopList() {
    return "frontend/shoplist";
    }
  • 測試


    image.png

店鋪詳情頁面

Controller層

  • 返回shopdetail頁面
    @RequestMapping(value = "shopdetail", method = RequestMethod.GET)
    private String shopDetail() {
        return "frontend/shopdetail";
    }
  • 新建一個(gè)ShopDetailController類
@Controller
@RequestMapping("/frontend")
public class ShopDetailController {
    @Autowired
    private ShopService shopService;

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * 獲取店鋪信息以及該店鋪下的商品列別列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/listshopdetailpageinfo", method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> listShopDetailPageInfo(HttpServletRequest request) {

        Map<String, Object> modelMap = new HashMap<>();

        // 獲取前臺傳過來的shopId
        long shopId = HttpServletRequestUtil.getLong(request, "shopId");
        Shop shop = null;
        List<ProductCategory> productCategoryList = null;

        if (shopId != -1) {
            // 獲取店鋪Id為shopId的店鋪
            shop = shopService.getByShopId(shopId);
            // 獲取店鋪下面的商品類別列表
            productCategoryList = productCategoryService.getProductCategroyList(shopId);
            modelMap.put("shop", shop);
            modelMap.put("productCategoryList", productCategoryList);
            modelMap.put("success", true);
        } else {
            modelMap.put("success", false);
            modelMap.put("errMsg", "empty shopId");
        }
        return modelMap;
    }

    @RequestMapping(value = "/listproductsbyshop", method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> listProductByShop(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<>();

        // 獲取頁碼
        int pageIndex = HttpServletRequestUtil.getInt(request, "pageIndex");
        // 獲取一頁需要的顯示條數(shù)
        int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
        // 獲取店鋪Id
        long shopId = HttpServletRequestUtil.getLong(request, "shopId");
        // 空值判斷
        if ((pageSize > -1) && (pageIndex > -1) && (shopId > -1)) {
            long productCategoryId = HttpServletRequestUtil.getLong(request,
                    "productCategoryId");
            String productName = HttpServletRequestUtil.getString(request,
                    "productName");
            Product productCondition = compactProductCondition4Search(shopId,
                    productCategoryId, productName);
            ProductExecution pe = productService.getProductList(
                    productCondition, pageIndex, pageSize);
            modelMap.put("productList", pe.getProductList());
            modelMap.put("count", pe.getCount());
            modelMap.put("success", true);
        } else {
            modelMap.put("success", false);
            modelMap.put("errMsg", "empty pageSize or pageIndex or shopId");
        }
        return modelMap;
    }

    private Product compactProductCondition4Search(long shopId,
                                                   long productCategoryId, String productName) {
        Product productCondition = new Product();
        Shop shop = new Shop();
        shop.setShopId(shopId);
        productCondition.setShop(shop);
        if (productCategoryId != -1L) {
            ProductCategory productCategory = new ProductCategory();
            productCategory.setProductCategoryId(productCategoryId);
            productCondition.setProductCategory(productCategory);
        }
        if (productName != null) {
            productCondition.setProductName(productName);
        }
        productCondition.setEnableStatus(1);
        return productCondition;
    }    
}
image.png

image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末华临,一起剝皮案震驚了整個(gè)濱河市芯杀,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌雅潭,老刑警劉巖揭厚,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異扶供,居然都是意外死亡筛圆,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進(jìn)店門诚欠,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人漾岳,你說我怎么就攤上這事轰绵。” “怎么了尼荆?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵左腔,是天一觀的道長。 經(jīng)常有香客問我捅儒,道長液样,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任巧还,我火速辦了婚禮鞭莽,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘麸祷。我一直安慰自己澎怒,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布阶牍。 她就那樣靜靜地躺著喷面,像睡著了一般星瘾。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上惧辈,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天琳状,我揣著相機(jī)與錄音,去河邊找鬼盒齿。 笑死轿偎,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的味咳。 我是一名探鬼主播棍弄,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼倒彰!你這毒婦竟也來了审洞?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤待讳,失蹤者是張志新(化名)和其女友劉穎芒澜,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體创淡,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡痴晦,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了琳彩。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片誊酌。...
    茶點(diǎn)故事閱讀 40,133評論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖露乏,靈堂內(nèi)的尸體忽然破棺而出碧浊,到底是詐尸還是另有隱情,我是刑警寧澤瘟仿,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布箱锐,位于F島的核電站,受9級特大地震影響劳较,放射性物質(zhì)發(fā)生泄漏驹止。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一观蜗、第九天 我趴在偏房一處隱蔽的房頂上張望臊恋。 院中可真熱鬧,春花似錦墓捻、人聲如沸捞镰。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽岸售。三九已至践樱,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間凸丸,已是汗流浹背拷邢。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留屎慢,地道東北人瞭稼。 一個(gè)月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像腻惠,于是被迫代替她去往敵國和親环肘。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,077評論 2 355

推薦閱讀更多精彩內(nèi)容