windows上安装redis并永久修改密码,及ssm框架集成redis

首先是redis安装,这个在菜鸟教程上介绍的很详细,按照上面说的就可以做到。下面是他的网址:

redis安装

安装完成后,我们先启动服务器,进入redis目录,按住shift然后右键,打开控制台。然后输入

redis-server.exe redis.windows.conf

这样就打开服务器了,现在的redis是没有密码的,然后重复上面的步骤,输入:

redis-cli.exe -h 127.0.0.1 -p 6379 

现在是打开客户端了,可以操作了。

我们在修改密码前,可以看下客户端有没有设置密码:

config get requirepass

然后我们需要修改密码,这个有两种方式,第一种是直接代码修改,这个是只有当次生效,重新启动redis就没有了。第二种是修改redis配置文件,这样可以永久修改。第一种设置密码代码如下:

config set requirepass "yourpassword"//设置当前密码,服务重新启动后又会置为默认,即无密码;不建议此种方式

第二种就需要修改redis安装目录下的redis.windows.conf文件了,找到# requirepass foobared,然后在下面添加一行

requirepass root

保存,现在root就是你的密码了。

接下来我们需要关闭redis,重新启动。服务器端启动命令没有变,客户端命令需要在登录时添加上密码:

redis-cli.exe -h 127.0.0.1 -p 6379 -a 123456 //需添加密码参数

这样就设置密码成功了。

 

然后我们需要集成到框架中去

首先,我们需要导入依赖,这个地方有可能出现一个错误,就是依赖版本问题,大家按照我的依赖版本导入是不会出问题的,否则会出现bug的:

<!-- spring 集成redis -->
		<dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

        <!-- redis -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.8.4.RELEASE</version>
        </dependency>
        <dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.3.10.RELEASE</version>
		</dependency>

我们在resources目录下创建一个redis.properties文件保存redis配置信息:

redis.maxIdle=300
redis.minIdle=100
redis.maxWaitMillis=3000
redis.testOnBorrow=true
redis.maxTotal=500
redis.host=127.0.0.1
redis.port=6379
redis.password=root

然后,我们创建spring-redis.xml文件,用来配置redis并继承:

<?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:p="http://www.springframework.org/schema/p"
          xmlns:tx="http://www.springframework.org/schema/tx"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/>
    <!--设置数据池-->
    <bean id="poolConfig"  class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}"></property>
        <property name="minIdle" value="${redis.minIdle}"></property>
        <property name="maxTotal" value="${redis.maxTotal}"></property>
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}"></property>
        <property name="testOnBorrow" value="${redis.testOnBorrow}"></property>
    </bean>
    <!--链接redis-->
    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.host}"></property>
        <property name="port" value="${redis.port}"></property>
        <property name="password" value="${redis.password}"></property>
        <property name="poolConfig" ref="poolConfig"></property>
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
    	<property name="connectionFactory" ref="connectionFactory"/>
        <!--以下针对各种数据进行序列化方式的选择-->
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
    </bean>
</beans>

然后,我们需要把这些配置放到web.xml中,使得tomcat启动后,就直接加载这些配置文件:

 <!-- mybatis 和redis配置 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-redis.xml,classpath:spring-mybatis.xml</param-value>
	</context-param>

现在就算集成完毕了,我们可以测试一下,我新建一个controller,

package com.mail.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by linmeng.
 **/
@RestController
public class MailController {
    
    @Resource
	private RedisTemplate<String, Object>redisTemplate;
    /**
     * 发送邮件
     */
    @RequestMapping("/email")
    public void sendMail(){
    	Map<String,String> map = new HashMap<>();
    	map.put("name", "xiaoming");
    	map.put("age", "18");
    	redisTemplate.opsForValue().set("name", "xiaoming");
    	System.out.println(redisTemplate.opsForValue().get("name"));
        
    }
}

如果控制台能够打印出东西,说明已经可以在项目中使用了。

Guess you like

Origin blog.csdn.net/qq_36746327/article/details/81537000