1. 程式人生 > >電商專案day09(網站前臺之廣告功能實現&優化策略)

電商專案day09(網站前臺之廣告功能實現&優化策略)

今日目標:

1、完成入口網站的搭建

2、完成運營商廣告後臺管理

3、輪播圖廣告展示

4、spring data redis 整合到專案

5、redis快取優化廣告業務

一、入口網站業務分析

1.首先廣告業務:

第一:吸引使用者   第二:運營商通過網站的流量賺錢

提高公司的收入,提升網站知名度,提升網站流量

2.設計到的表結構

tb_content

tb_content_category

廣告型別表和廣告表是一對多的關係

二、運營商後臺廣告型別和廣告管理

1.搭建廣告的服務和介面工程

2.完成需求:如圖所示

後臺程式碼已經寫好了,我們在前臺編寫一個方法來呼叫查詢所有的的廣告分類

前臺程式碼:

//查詢廣告分類列表
	$scope.selectContentCategoryList=function () {
		contentCategoryService.findAll().success(function (response) {
			$scope.contentCategoryList=response;
        })
    }
    

注意引入

contentCategoryService

頁面的改裝:

<td>內容類目ID</td>

                        <td><select ng-options="item.id as item.name for item in contentCategoryList" class="form-control" ng-model="entity.categoryId" placeholder="內容類目ID" ></select></td>

<td>狀態</td>

                        <td><input type="checkbox" ng-model="entity.status" ng-true-value="1" ng-false-value="0"></td>

3、完成如圖所示的效果

上面的功能我們在shop_web種就已經寫過了,所以我們直接複製即可

uploadController.java    配置檔案   aplicationContext.xml    fdfs_client.conf   

uploadService.js    以及複製Controller中的方法到content.html中

注意:一定要引入相關的service

最後修改頁面:

<td>圖片絕對路徑</td>

<td>
    <input type="file" id="file" />
    <button ng-click="uploadFile()" class="btn btn-primary" type="button" >
        上傳
    </button>
    <img  src="{{entity.pic}}" width="150px" height="150px">
</td>

三、網站首頁輪播圖片廣告展示

1.構建protal-web專案

新增jar包  以及配置檔案  還有 相關的靜態資源

2.完成後端程式碼

程式碼:

/**
	 * 通過id查詢分類的圖片
	 * @param categoryId
	 * @return
	 */
	@Override
	public List<TbContent> findCategoryId(Long categoryId) {
		TbContentExample example = new TbContentExample();
		Criteria criteria = example.createCriteria();
		criteria.andCategoryIdEqualTo(categoryId);
		criteria.andStatusEqualTo("1");
		List<TbContent> tbContents = contentMapper.selectByExample(example);
        for (TbContent tbContent : tbContents) {
            System.out.println("你好");
            System.out.println(tbContent);
        }
        return tbContents;
	}

前臺程式碼:

controller層
app.controller("indexController",function ($scope,$controller,contentService) {

    //控制器繼承程式碼
    $controller("baseController",{$scope:$scope});

    //根據廣告id查詢廣告的列表資料
    $scope.findCategoryId=function (categoryId) {
        contentService.findCategoryId(categoryId).success(function (response) {
            //定義廣告列表接受資料
            $scope.contentList = response;
        })
    }
    
})
servicer
//服務層
app.service('contentService',function($http){

    //根據id查詢廣告的分類
    this.findCategoryId=function(categoryId){
        return $http.get('content/findCategoryId.do?categoryId='+categoryId);
    }
});

四、redis在Linux系統安裝

1. 將redis資源包上傳到Linux中,redis-3.0.0.tar.gz資源包存在於“\資源\配套軟體\Redis”目錄中。
2. 執行tar -zxvf redis-3.0.0.tar.gz解壓,進入解壓的redis-3.0.0,執行make編譯命令
3. 在/usr/local下建立redis目錄,命令 mkdir redis  備註:將redis安裝到/usr/local/redis目錄中
4. 進入解壓的redis-3.0.0,安裝redis服務,執行命令:make install PREFIX=/usr/local/redis
5. 複製配置檔案將/redis-3.0.0/redis.conf 複製到redis 下的bin 目錄下  執行命令:
   cp redis.conf /usr/local/redis/bin
6. 啟動redis ./redis-server redis.conf 

五、springdataredis簡介以及demo

六、入口網站快取廣告資料

1、整合redis到專案中

首先新增jar包   

編寫配置檔案

在servicer層實現redis的新增

後臺程式碼:

/**
	 * 通過id查詢分類的圖片
	 * @param categoryId
	 * @return
	 */
	@Autowired
    private RedisTemplate redisTemplate;
	@Override
	public List<TbContent> findCategoryId(Long categoryId) {
	    //1.首先從從redis中查
        List<TbContent> contentList = (List<TbContent>) redisTemplate.boundHashOps("content").get(categoryId);
        //2.判斷是否有資料
        if (contentList==null){
            //如果沒有我們從資料中查詢
            TbContentExample example = new TbContentExample();
            Criteria criteria = example.createCriteria();
            criteria.andCategoryIdEqualTo(categoryId);
            criteria.andStatusEqualTo("1");
             contentList = contentMapper.selectByExample(example);
            System.out.println("資料庫總查的");
        }else{
            System.out.println("從redis中查的");
        }

        return contentList;

	}

2.優化,資料庫的資料與redis中資料一致性問題

專案中那些模組會用到快取?
        入口網站快取廣告資料
        秒殺
        使用者註冊驗證碼快取
        購物車模組

        
    如果廣告資料發生變化(增刪改),清空redis中快取的當前廣告分類廣告資料,下次展示廣告資料時,再重新從資料庫查詢最新廣告資料即可。
    
    注意:廣告修改時,需要分析是否修改了廣告分類值。

程式碼:

/**
	 * 增加
	 */
	@Override
	public void add(TbContent content) {
	    contentMapper.insert(content);
	    //清除新增廣告對應的快取資料
        redisTemplate.boundHashOps("content").delete(content.getCategoryId());
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(TbContent content){
		//修改後我們都要做跟新
        TbContent tbContent = contentMapper.selectByPrimaryKey(content.getId());
        redisTemplate.boundHashOps("content").delete(tbContent.getCategoryId());
        contentMapper.updateByPrimaryKey(content);
        //判斷id是否發生變化,如果分類發生變化,需要清除修改後的快取資料
        if (content.getCategoryId().longValue()!=tbContent.getCategoryId().longValue()){
            redisTemplate.boundHashOps("content").delete(content.getCategoryId());
        }
	}	
	
	/**
	 * 根據ID獲取實體
	 * @param id
	 * @return
	 */
	@Override
	public TbContent findOne(Long id){
		return contentMapper.selectByPrimaryKey(id);
	}

	/**
	 * 批量刪除
	 */
	@Override
	public void delete(Long[] ids) {
		for(Long id:ids){
		    //清除刪除廣告對應的快取資料
            TbContent tbContent = contentMapper.selectByPrimaryKey(id);
            contentMapper.deleteByPrimaryKey(id);
            redisTemplate.boundHashOps("content").delete(tbContent.getCategoryId());
		}		
	}
	
	
		@Override
	public PageResult findPage(TbContent content, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		TbContentExample example=new TbContentExample();
		Criteria criteria = example.createCriteria();
		
		if(content!=null){			
						if(content.getTitle()!=null && content.getTitle().length()>0){
				criteria.andTitleLike("%"+content.getTitle()+"%");
			}
			if(content.getUrl()!=null && content.getUrl().length()>0){
				criteria.andUrlLike("%"+content.getUrl()+"%");
			}
			if(content.getPic()!=null && content.getPic().length()>0){
				criteria.andPicLike("%"+content.getPic()+"%");
			}
			if(content.getStatus()!=null && content.getStatus().length()>0){
				criteria.andStatusLike("%"+content.getStatus()+"%");
			}
	
		}
		
		Page<TbContent> page= (Page<TbContent>)contentMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 通過id查詢分類的圖片
	 * @param categoryId
	 * @return
	 */
	@Autowired
    private RedisTemplate redisTemplate;
	@Override
	public List<TbContent> findCategoryId(Long categoryId) {
	    //1.首先從從redis中查
        List<TbContent> contentList = (List<TbContent>) redisTemplate.boundHashOps("content").get(categoryId);
        //2.判斷是否有資料
        if (contentList==null){
            //如果沒有我們從資料中查詢
            TbContentExample example = new TbContentExample();
            Criteria criteria = example.createCriteria();
            criteria.andCategoryIdEqualTo(categoryId);
            criteria.andStatusEqualTo("1");
             contentList = contentMapper.selectByExample(example);
            System.out.println("資料庫總查的");
        }else{
            System.out.println("從redis中查的");
        }

        return contentList;

	}