日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

实战SSM_O2O商铺_13【商铺注册】View层之初始化页面数据

發布時間:2025/3/21 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实战SSM_O2O商铺_13【商铺注册】View层之初始化页面数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 請求過程分析
  • DAO層的實現
    • AreaDao接口,Mapper映射文件及單元測試
    • ShopCategoryDao接口
    • ShopCategoryDao.xml Mapper映射文件
    • 單元測試
  • Service層的實現
    • AreaService接口,AreaServiceImpl接口實現類及單元測試
    • ShopCategoryService接口
    • ShopCategoryServiceImpl 接口實現類
    • 單元測試
  • Controller層
  • 部署調測
  • Github地址

請求過程分析

頁面畫完之后,我們的下拉框是沒有數據的

<!-- 商鋪分類 下拉列表 --><li><div class="item-content"><div class="item-inner"><div class="item-title label">商鋪分類</div><div class="item-input"><!-- 增加id,便于js中操作,需要從后臺讀取數據 --><select id="shop-category"></select></div></div></div></li><!-- 所屬區域 下拉列表 - --><li><div class="item-content"><div class="item-inner"><div class="item-title label">所屬區域</div><div class="item-input"><select id="shop-area"></select></div></div></div></li>

在初始換頁面加載js的時候調用

// 調用函數,加載數據 getShopInitInfo();

函數定義如下:

/*** 從后臺加載獲取下拉菜單的值*/function getShopInitInfo() {$.getJSON(initUrl, function(data) {if (data.success) {var tempShopCategoryHtml = '';var tempShopAreaHtml = '';data.shopCategoryList.map(function(item, index) {tempShopCategoryHtml += '<option data-id="'+ item.shopCategoryId + '">' + item.shopCategoryName+ '</option>';});data.areaList.map(function(item, index) {tempShopAreaHtml += '<option data-id="' + item.areaId+ '">' + item.areaName + '</option>';});// 獲取html中對應標簽的id 賦值$('#shop-category').html(tempShopCategoryHtml);$('#shop-area').html(tempShopAreaHtml)}else{$.toast(data.errMsg);}});};

請求 initUrl , 我們設置的值為/o2o/shopadmin/getshopinitinfo ,根據web.xml中配置攔截所有請求可知在經過DispatcherServlet分發到Controller層,接收到請求后繼續處理。


DAO層的實現

需要獲取商鋪分類列表和區域列表,DAO層我們還沒做完,來完善下


AreaDao接口,Mapper映射文件及單元測試

我們在實戰SSM_O2O商鋪_05集成SSM后驗證DAO層、Service層、Controller層的配置中已經開發了queryArea接口以及配置了mapper映射文件,我們這里直接復用這個接口即可。


剩下的就是ShopCategory的了。

ShopCategoryDao接口

/o2o/src/main/java/com/artisan/o2o/dao/ShopCategoryDao.java

package com.artisan.o2o.dao;import java.util.List;import org.apache.ibatis.annotations.Param;import com.artisan.o2o.entity.ShopCategory;public interface ShopCategoryDao {/*** * * @Title: queryShopCategoryList* * @Description: 按照需求* * 1.首頁展示一級目錄(即parent_id 為 null的商鋪類別)* * 2.點進去某個一級目錄加載對應目錄下的子目錄* * 所以這里需要加個入參ShopCategory,并通過MyBatis提供的注解@Param與Mapper映射文件中的SQL關聯起來* ,在SQL中進行判斷* * @return* * @return: List<ShopCategory>*/List<ShopCategory> queryShopCategoryList(@Param("shopCategoryCondition") ShopCategory shopCategory);}

ShopCategoryDao.xml Mapper映射文件

/o2o/src/main/resources/mapper/ShopCategoryDao.xml

<?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.artisan.o2o.dao.ShopCategoryDao"><select id="queryShopCategoryList" resultType="com.artisan.o2o.entity.ShopCategory">SELECTshop_category_id ,shop_category_name,shop_category_desc,shop_category_img,priority,create_time,last_edit_time,parent_idFROMtb_shop_category<where><!-- 控制層getshopinitinfo的方法 shopCategoryService.getShopCategoryList(new ShopCategory());只能選擇二級商鋪類別,不能掛載一級商鋪類別大類目錄下--><if test="shopCategoryCondition != null">and parent_id is not null</if><!-- 如果傳遞了父類的id,則查詢對應父類下的目錄 --><if test="shopCategoryCondition.parent != null">and parent_id = #{shopCategoryCondition.parent.shopCategoryId}</if></where> ORDER BY priority DESC </select> </mapper>

單元測試

tb_shop_category中的數據

為了方便測試,我們添加幾條測試數據

-- ---------------------------- -- Records of tb_shop_category -- ---------------------------- INSERT INTO `tb_shop_category` VALUES ('1', '咖啡奶茶', '咖啡奶茶大類', '/xxxx/xxxx', '0', '2018-05-18 02:13:56', '2018-05-18 02:13:58', null); INSERT INTO `tb_shop_category` VALUES ('2', '咖啡', '咖啡小類', '/yyyy/yyyy', '2', '2018-05-18 03:38:17', '2018-05-18 03:38:20', '1'); INSERT INTO `tb_shop_category` VALUES ('3', '奶茶', '奶茶小類', '/aaa/bbb', '0', '2018-05-29 14:36:55', '2018-05-29 14:36:58', '1');

單元測試類

package com.artisan.o2o.dao;import java.util.List;import org.junit.Assert; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired;import com.artisan.o2o.BaseTest; import com.artisan.o2o.entity.ShopCategory;public class ShopCategoryDaoTest extends BaseTest {@AutowiredShopCategoryDao shopCategoryDao;@Testpublic void testQueryShopCategoryList() {// shopCategoryCondition 不為null的情況,查詢parent_id is not null 的數據ShopCategory shopCategory = new ShopCategory();List<ShopCategory> categoryList = shopCategoryDao.queryShopCategoryList(shopCategory);Assert.assertEquals(2, categoryList.size());for (ShopCategory shopCategory2 : categoryList) {System.out.println(shopCategory2);}// shopCategoryCondition.parent 不為null的情況// 查詢parent=1的店鋪目錄ShopCategory child = new ShopCategory();ShopCategory parent = new ShopCategory();parent.setShopCategoryId(1L);child.setParent(parent);categoryList = shopCategoryDao.queryShopCategoryList(child);Assert.assertEquals(2, categoryList.size());for (ShopCategory shopCategory2 : categoryList) {System.out.println(shopCategory2);}}}

檢查日志信息是否符合預期

五月 29, 2018 2:44:57 下午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager 信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 2, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 10000, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1br1ebw9v1c27wf2172af4p|74f0ea28, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1br1ebw9v1c27wf2172af4p|74f0ea28, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://localhost:3306/o2o?useUnicode=true&characterEncoding=utf8, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 30, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 10, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ] JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@38234a38] will not be managed by Spring ==> Preparing: 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 parent_id is not null ORDER BY priority DESC ==> Parameters: <== Columns: shop_category_id, shop_category_name, shop_category_desc, shop_category_img, priority, create_time, last_edit_time, parent_id <== Row: 2, 咖啡, 咖啡小類, /yyyy/yyyy, 2, 2018-05-18 03:38:17.0, 2018-05-18 03:38:20.0, 1 <== Row: 3, 奶茶, 奶茶小類, /aaa/bbb, 0, 2018-05-29 14:36:55.0, 2018-05-29 14:36:58.0, 1 <== Total: 2 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@36cda2c2] ShopCategory [shopCategoryId=2, shopCategoryName=咖啡, shopCategoryDesc=咖啡小類, shopCategoryImg=/yyyy/yyyy, priority=2, createTime=Fri May 18 03:38:17 BOT 2018, lastEditTime=Fri May 18 03:38:20 BOT 2018, parent=null] ShopCategory [shopCategoryId=3, shopCategoryName=奶茶, shopCategoryDesc=奶茶小類, shopCategoryImg=/aaa/bbb, priority=0, createTime=Tue May 29 14:36:55 BOT 2018, lastEditTime=Tue May 29 14:36:58 BOT 2018, parent=null] Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@29ca3d04] was not registered for synchronization because synchronization is not active JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@52c3cb31] will not be managed by Spring ==> Preparing: 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 parent_id is not null and parent_id = ? ORDER BY priority DESC ==> Parameters: 1(Long) <== Columns: shop_category_id, shop_category_name, shop_category_desc, shop_category_img, priority, create_time, last_edit_time, parent_id <== Row: 2, 咖啡, 咖啡小類, /yyyy/yyyy, 2, 2018-05-18 03:38:17.0, 2018-05-18 03:38:20.0, 1 <== Row: 3, 奶茶, 奶茶小類, /aaa/bbb, 0, 2018-05-29 14:36:55.0, 2018-05-29 14:36:58.0, 1 <== Total: 2 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@29ca3d04] ShopCategory [shopCategoryId=2, shopCategoryName=咖啡, shopCategoryDesc=咖啡小類, shopCategoryImg=/yyyy/yyyy, priority=2, createTime=Fri May 18 03:38:17 BOT 2018, lastEditTime=Fri May 18 03:38:20 BOT 2018, parent=null] ShopCategory [shopCategoryId=3, shopCategoryName=奶茶, shopCategoryDesc=奶茶小類, shopCategoryImg=/aaa/bbb, priority=0, createTime=Tue May 29 14:36:55 BOT 2018, lastEditTime=Tue May 29 14:36:58 BOT 2018, parent=null] 五月 29, 2018 2:44:58 下午 org.springframework.context.support.GenericApplicationContext doClose 信息: Closing org.springframework.context.support.GenericApplicationContext@ae45eb6: startup date [Tue May 29 14:44:53 BOT 2018]; root of context hierarchy

單元測試通過


Service層的實現


AreaService接口,AreaServiceImpl接口實現類及單元測試

我們在實戰SSM_O2O商鋪_05集成SSM后驗證DAO層、Service層、Controller層的配置中已經開發了AreaService接口,AreaServiceImpl接口實現類及單元測試,我們這里直接復用即可。


ShopCategoryService接口

/o2o/src/main/java/com/artisan/o2o/service/ShopCategoryService.java

package com.artisan.o2o.service;import java.util.List;import com.artisan.o2o.entity.ShopCategory;public interface ShopCategoryService {List<ShopCategory> getShopCategoryList(ShopCategory shopCategory); }

ShopCategoryServiceImpl 接口實現類

/o2o/src/main/java/com/artisan/o2o/service/impl/ShopCategoryServiceImpl.java

package com.artisan.o2o.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import com.artisan.o2o.dao.ShopCategoryDao; import com.artisan.o2o.entity.ShopCategory; import com.artisan.o2o.service.ShopCategoryService;@Service public class ShopCategoryServiceImpl implements ShopCategoryService {@Autowiredprivate ShopCategoryDao shopCategoryDao;@Overridepublic List<ShopCategory> getShopCategoryList(ShopCategory shopCategory) {return shopCategoryDao.queryShopCategoryList(shopCategory);}}

單元測試

package com.artisan.o2o.service;import java.util.List;import org.junit.Assert; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired;import com.artisan.o2o.BaseTest; import com.artisan.o2o.entity.ShopCategory;public class ShopServiceCategoryTest extends BaseTest {@AutowiredShopCategoryService shopCategoryService;@Testpublic void testQueryShopCategory() {ShopCategory shopCategory = new ShopCategory();List<ShopCategory> shopCategories = shopCategoryService.getShopCategoryList(shopCategory);Assert.assertEquals(2, shopCategories.size());for (ShopCategory shopCategory2 : shopCategories) {System.out.println(shopCategory2);}} }

驗證符合預期,單元測試OK。


Controller層

前端js中的請求路徑

// 獲取基本信息的URL var initUrl = '/o2o/shopadmin/getshopinitinfo';

/o2o/src/main/java/com/artisan/o2o/web/shopadmin/ShopController.java#getshopinitinfo 中使用@RequestMapping匹配前端請求的URL,返回前端的數據為@ResponseBody將返回Map<String, Object>類型轉為的JSON串

/*** * * @Title: getshopinitinfo* * @Description: 初始化區域信息 和 ShopCategory信息,返回給前臺表單頁面* * @return: Map<String,Object>*/@RequestMapping(value = "/getshopinitinfo", method = RequestMethod.GET)@ResponseBodypublic Map<String, Object> getshopinitinfo() {Map<String, Object> modelMap = new HashMap<String, Object>();List<ShopCategory> shopCategoryList = null;List<Area> areaList = null;try {shopCategoryList = shopCategoryService.getShopCategoryList(new ShopCategory());areaList = areaservice.getAreaList();// 返回success shopCategoryList areaList,前端通過 data.success來判斷從而展示shopCategoryList和areaList的數據modelMap.put("success", true);modelMap.put("shopCategoryList", shopCategoryList);modelMap.put("areaList", areaList);} catch (Exception e) {modelMap.put("success", false);modelMap.put("errMsg", e.getMessage());}return modelMap;}

當然了別忘了注入

@Autowiredprivate ShopCategoryService shopCategoryService;@Autowiredprivate AreaService areaservice;

部署調測

啟動tomcat ,調測階段可以通過debug的方式,更加詳細了解請求過程。

可以看到已經加載了后端的數據,并且符合需求。 店鋪分類加載二級目錄,區域加載全部區域.


Github地址

代碼地址: https://github.com/yangshangwei/o2o

總結

以上是生活随笔為你收集整理的实战SSM_O2O商铺_13【商铺注册】View层之初始化页面数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。