<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>2.8.1version>
dependency>
<dependency>
<groupId>org.springframework.datagroupId>
<artifactId>spring-data-redisartifactId>
<version>1.7.2.RELEASEversion>
dependency>
redis-config.properties
**注意 不要有空格 服务器地址要对上 **
# Redis settings
# server IP
redis.host=192.168.25.128
# server port
redis.port=6379
# server pass
redis.pass=
# use dbIndex
redis.database=0
# \u63A7\u5236\u4E00\u4E2Apool\u6700\u591A\u6709\u591A\u5C11\u4E2A\u72B6\u6001\u4E3Aidle(\u7A7A\u95F2\u7684)\u7684jedis\u5B9E\u4F8B
redis.maxIdle=300
# \u8868\u793A\u5F53borrow(\u5F15\u5165)\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u6700\u5927\u7684\u7B49\u5F85\u65F6\u95F4\uFF0C\u5982\u679C\u8D85\u8FC7\u7B49\u5F85\u65F6\u95F4(\u6BEB\u79D2)\uFF0C\u5219\u76F4\u63A5\u629B\u51FAJedisConnectionException\uFF1B
redis.maxWait=3000
# \u5728borrow\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u662F\u5426\u63D0\u524D\u8FDB\u884Cvalidate\u64CD\u4F5C\uFF1B\u5982\u679C\u4E3Atrue\uFF0C\u5219\u5F97\u5230\u7684jedis\u5B9E\u4F8B\u5747\u662F\u53EF\u7528\u7684
redis.testOnBorrow=true
applicationContext-redis.xml
此处 与spring整合 可以注入到需使用的地方 redisTemplate
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<context:property-placeholder location="classpath*:properties/*.properties" />
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
bean>
<bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="JedisConnectionFactory" />
bean>
beans>
逻辑最好在根处解决 处理
这里我们就在service中
package com.pinyougou.content.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.pinyougou.content.service.ContentService;
import com.pinyougou.entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.pinyougou.mapper.TbContentMapper;
import com.pinyougou.pojo.TbContent;
import com.pinyougou.pojo.TbContentExample;
import com.pinyougou.pojo.TbContentExample.Criteria;
import org.springframework.data.redis.core.RedisTemplate;
;
/**
* 服务实现层
*
* @author Administrator
*/
@Service
public class ContentServiceImpl implements ContentService {
@Autowired
private TbContentMapper contentMapper;
@Autowired
private RedisTemplate redisTemplate;
//----------------------------------------------------------------------------------------------------------------------------- 这里才是精髓
@Override
public List findContById(Long id) {
//前往redis中查找 存的为hash content 大组 id 为小组
List<TbContent> contentAll = (List<TbContent>) redisTemplate.boundHashOps("content").get(id);
if(contentAll==null){ //查不到就为null
TbContentExample example = new TbContentExample();
example.createCriteria().andCategoryIdEqualTo(id);
//对广告列表数据排序显示
example.setOrderByClause("sort_order desc"); //排序条件是字段名
contentAll = contentMapper.selectByExample(example);
redisTemplate.boundHashOps("content").put(id,contentAll);
System.out.println("-------从MySql获取数据-------");
}else{
System.out.println("-------从redis获取数据-------");
}
return contentAll;
}
//--------------------------------------------------------------------------------------------------------------------------------------下面为赠送 一起沾过来的
/**
* 查询全部
*/
@Override
public List<TbContent> findAll() {
return contentMapper.selectByExample(null);
}
/**
* 增加
*/
@Override
public void add(TbContent content) {
contentMapper.insert(content);
}
/**
* 修改
*/
@Override
public void update(TbContent content) {
contentMapper.updateByPrimaryKey(content);
}
/**
* 根据ID获取实体
*
* @param id
* @return
*/
@Override
public TbContent findOne(Long id) {
return contentMapper.selectByPrimaryKey(id);
}
/**
* 批量删除
*/
@Override
public void delete(Long[] ids) {
for (Long id : ids) {
contentMapper.deleteByPrimaryKey(id);
}
}
/**
* 分页查询+模糊搜索
*/
@Override
public PageResult findAll(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() + "%");
}
}
List<TbContent> lists = contentMapper.selectByExample(example);
PageInfo<TbContent> page = new PageInfo<>(lists);
return new PageResult(page.getTotal(), page.getList());
}
}