SSM框架快速集成redis

原文地址:https://blog.csdn.net/DuShiWoDeCuo/article/details/78506579?locationNum=4&fps=1
SSM框架快速集成redis
1.添加maven依赖
<!-- config redis data and client jar-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${spring.redis.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>${commons.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>

注:org.springframework.data、org.apache.commons、redis.clients三者的版本必须统一,不然编译时会出现各种版本冲突。(即这种错误信息:java.lang.NoSuchMethodError)这里三者的版本分别为(亲测可用):

<spring.redis.version>1.6.0.RELEASE</spring.redis.version>
<jedis.version>2.7.2</jedis.version>
<commons.version>2.4.2</commons.version>

我这里使用的是:

    <!--redis start -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.7.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.8.1</version>
        </dependency>
        <!--redis end -->

2.编辑redis连接信息

redis.host=192.168.137.111
redis.port=6379
redis.password=

redis.maxIdle=300
redis.maxWaitMillis=1000
redis.maxTotal=600
redis.testOnBorrow=true
redis.testOnReturn=true

redis.projectNam=ssm_redis

3.配置redis-context.xml
`

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       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">

    <!-- 引入redis属性配置文件 -->
    <import resource="classpath:cache/redis-context.xml"/>

</beans>

5.单元测试redis

/**
 * @Author: CatalpaFlat
 * @Descrition:
 * @Date: Create in 10:08 2017/11/8
 * @Modified BY:
 */
@RunWith(SpringRunner.class)
@ContextConfiguration({"classpath:spring/*.xml"})
public class TestRedis {

    @Autowired
    private RedisTemplate redisTemplate;

    private static final Logger log  = Logger.getLogger(TestRedis.class.getName());

    @Test
    public void test(){
        redisTemplate.opsForValue().set("chen", "陈梓平");
        log.info("value:"+redisTemplate.opsForValue().get("chen"));
    }
}

或者这样:

@RequestMapping("/api/hotel/")
@Controller
public class Demo extends I_BaseController {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ResponseBody
    @RequestMapping("redis")
    public String redisDemo() {
        redisTemplate.opsForValue().set("hotel","hotelredisdemo");
        return "success";

    }

这里写图片描述

另附上redis启动图:
这里写图片描述

阅读更多

扫码向博主提问

ZZZZVSS

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • java
去开通我的Chat快问
换一批

没有更多推荐了,返回首页