1. 创建配置文件resource.properties
#首页大广告位展示
CONTENT_INDEX_REDIS_KEY=CONTENT_INDEX_AD
2. 修改配置文件applicationContext-dao.xml
3. ContentServiceImpl.java
package com.taotao.portal.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.taotao.content.dao.JedisClient;
import com.taotao.mapper.ContentMapper;
import com.taotao.pojo.TbContent;
import com.taotao.portal.service.ContentService;
import com.taotao.utils.HttpClientUtil;
import com.taotao.utils.JsonUtils;
import com.taotao.utils.TaotaoResult;
@Service
public class ContentServiceImpl implements ContentService {
@Autowired
private JedisClient jedisClient;
@Autowired
private ContentMapper contentMapper;
@Value("${CONTENT_INDEX_REDIS_KEY}")
private String CONTENT_INDEX_REDIS_KEY;//taotao-content-service中的resouce.properties
@Value("${REST_BASIC_URL}")//与taotao-portal中的resources.properties对应上
private String REST_BASIC_URL;
@Value("${INDEX_CONTENT_URL}")
private String INDEX_CONTENT_URL;
@Override
public List<TbContent> getContentByCategoryId(long categoryId) {
//1.去redis数据库查询数据
String jedisJson = jedisClient.hget(CONTENT_INDEX_REDIS_KEY, categoryId+"");
//2.判断
//3.若数据存在,则返回数据
if (StringUtils.isNotEmpty(jedisJson)) {//redis不为空
//将数据转换为list
List<TbContent> list = JsonUtils.jsonToList(jedisJson, TbContent.class);
return list;
}
//4.若数据不存在,则从MySQL数据库中查询数据,并将数据写入到redis中
List<TbContent> contentlist = contentMapper.getContentByCategoryId(categoryId);
//往缓存中添加数据,缓存中存储的value是字符串类型的
jedisClient.hset(CONTENT_INDEX_REDIS_KEY, categoryId+"", JsonUtils.objectToJson(contentlist));
return contentlist;
}
}
4. IndexController.java
package com.taotao.portal.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.taotao.pojo.TbContent;
import com.taotao.portal.service.ContentService;
import com.taotao.utils.HttpClientUtil;
import com.taotao.utils.JsonUtils;
import com.taotao.utils.TaotaoResult;
@Controller
public class IndexController {
@Autowired
private ContentService contentService;
@RequestMapping("/index")
public String indexPage(Model model){
List<TbContent> content = contentService.getContentByCategoryId(89);
List list = new ArrayList<>();
//遍历
for (TbContent tbContent : content) {
//用map拼接数据
//{"srcB":"http://image.taotao.com/images/2015/03/03/2015030304360302109345.jpg","height":240,"alt":"","width":670,"src":"http://image.taotao.com/images/2015/03/03/2015030304360302109345.jpg","widthB":550,"href":"http://sale.jd.com/act/e0FMkuDhJz35CNt.html?cpdad=1DLSUE","heightB":240}
Map map = new HashMap<>();
map.put("srcB", tbContent.getPic());
map.put("height",240 );
map.put("alt", tbContent.getSubTitle());
map.put("width",670 );
map.put("src", tbContent.getPic2());
map.put("widthB", 550);
map.put("href", tbContent.getUrl());
map.put("heightB", 240);
//将map数据添加到list中
list.add(map);
}
//将list转换为json
String jsonData = JsonUtils.objectToJson(list);
model.addAttribute("ad1", jsonData);//与index.jsp中的数据同步
return "index";
}
}