• ADADADADAD

    ssm框架如何使用redis[ 建站问答 ]

    建站问答 时间:2024-12-01 19:16:50

    作者:文/会员上传

    简介:

    ssm框架使用redis的示例:1.导入Redis相关jar包,代码:<!--redis相关--><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.9.0</version>

    以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。

    ssm框架使用redis的示例:

    1.导入Redis相关jar包,代码:

    <!--redis相关-->

    <dependency>

    <groupId>redis.clients</groupId>

    <artifactId>jedis</artifactId>

    <version>2.9.0</version>

    </dependency>

    <dependency>

    <groupId>org.springframework.data</groupId>

    <artifactId>spring-data-redis</artifactId>

    <version>1.6.2.RELEASE</version>

    </dependency>

    2.在redis.properties文件中写入Redis基本配置属性,代码:

    #服务器ip

    redis.hostname=127.0.0.1

    #redis数据库端口

    redis.port=6379

    #使用的数据库(共有16个数据库0~15)

    redis.database=2

    #控制一个pool可分配多少个jedis示例

    redis.pool.maxActive=50

    #控制一个pool最多有多少个状态为idle的jedis实例

    redis.pool.maxIdle=300

    #最大等待连接时间(单位毫秒)

    redis.pool.maxTotal=600

    #redis密码(一般不设置密码,设了重启服务也会没有)

    redis.pass=

    3.在applicationContext.xml中添加相关bean,代码:

    <!--载入配置文件-->

    <context:property-placeholderlocation="classpath:redis.properties"ignore-unresolvable="true"/>

    <!--配置JedisPoolConfig示例-->

    <beanid="poolConfig"class="redis.clients.jedis.JedisPoolConfig">

    <propertyname="maxIdle"value="${redis.pool.maxIdle}"/>

    <propertyname="maxTotal"value="${redis.pool.maxTotal}"/>

    </bean>

    <!--配置JedisConnectionFactory-->

    <beanid="jedisConnectionFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">

    <propertyname="hostName"value="${redis.hostname}"/>

    <propertyname="port"value="${redis.port}"/>

    <propertyname="password"value="${redis.pass}"/>

    <propertyname="database"value="${redis.database}"/>

    <propertyname="poolConfig"ref="poolConfig"/>

    <propertyname="usePool"value="true"/>

    </bean>

    <!--配置RedisTemplate-->

    <beanid="redisTemplate"class="org.springframework.data.redis.core.RedisTemplate">

    <propertyname="connectionFactory"ref="jedisConnectionFactory"/>

    <propertyname="defaultSerializer">

    <beanclass="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>

    </property>

    <propertyname="keySerializer">

    <beanclass="org.springframework.data.redis.serializer.StringRedisSerializer"/>

    </property>

    <propertyname="valueSerializer">

    <beanclass="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>

    </property>

    <propertyname="hashKeySerializer">

    <beanclass="org.springframework.data.redis.serializer.StringRedisSerializer"/>

    </property>

    <propertyname="hashValueSerializer">

    <beanclass="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>

    </property>

    </bean>

    4.自动注入RedisTemplate并使用,代码:

    @Autowired

    privateRedisTemplateredisTemplate;

    @Override

    publicAccountgetAccountById(Integerid){

    if(redisTemplate.opsForHash().hasKey("Account",id.toString())){

    //redis缓存中包含数据,则从redis中获取

    System.out.println("从redis中获取");

    return(Account)redisTemplate.opsForHash().get("Account",id.toString());

    }else{

    //redis缓存中不含该数据,则从mysql中获取

    System.out.println("从mysql中获取");

    returnaccountDao.getAccountById(id);

    }

    }

    ssm框架如何使用redis.docx

    将本文的Word文档下载到电脑

    推荐度:

    下载
    热门标签: redisssm框架