电商项目day09(网站前台之广告功能实现&优化策略)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangwei_620/article/details/85014444

今日目标:

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;

	}

猜你喜欢

转载自blog.csdn.net/wangwei_620/article/details/85014444