初级篇ssm+redis (key value调用) 整合

查看ssm项目请看  初级篇 Eclipse+ssm简单配置附源码地址 (https://blog.csdn.net/qq_32179695/article/details/78733683

-------------------------------------------------------------------spring-redis.xml---------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:redis="http://www.springframework.org/schema/redis" xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/redis
    http://www.springframework.org/schema/redis/spring-redis.xsd
    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache.xsd
    ">
    <!-- 读取配置文件 -->
        <!--  项目中引入redis -->
        <!-- <import resource="spring-redis.xml" /> -->
    <!--     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>redis.properties</value>
            </list>
        </property>
    </bean>
     -->
    <context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/>

    <!-- jedis连接池配置 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最小空闲连接数 -->
        <property name="minIdle" value="${redis.minIdle}" />
        <!-- 最大空闲连接数 -->
        <property name="maxIdle" value="${redis.maxIdle}" />
        <!-- 最大连接数 -->
        <property name="maxTotal" value="${redis.maxTotal}" />
        <!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
        <!-- 在获取连接的时候检查有效性, 默认false -->
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
        <!-- 每次释放连接的最大数目 -->
        <property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}" />
        <!-- 释放连接的扫描间隔(毫秒) -->
        <property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}" />
        <!-- 连接最小空闲时间 -->
        <property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}" />
        <!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
        <property name="softMinEvictableIdleTimeMillis" value="${redis.softMinEvictableIdleTimeMillis}" />
        <!-- 在空闲时检查有效性, 默认false -->
        <property name="testWhileIdle" value="${redis.testWhileIdle}" />
        <!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
        <property name="blockWhenExhausted" value="${redis.blockWhenExhausted}" />
    </bean>
    
    <!-- redis连接池,构造注入 -->
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="close">
        <constructor-arg name="poolConfig" ref="poolConfig" />
        <constructor-arg name="host" value="${redis.host}" />
        <constructor-arg name="port" value="${redis.port}" />
        <constructor-arg name="timeout" value="${redis.timeout}"/>
        <constructor-arg name="password" value="${redis.pwd}"/>
    </bean>
    
</beans>
-------------------------------------------------------------------redis.properties---------------------------------------------------------------------------------

redis.host=127.0.0.1
redis.port=6379
redis.pwd=foobared
redis.maxIdle=100
redis.maxWait=1000
redis.testOnBorrow=true
redis.timeout=100000
defaultCacheExpireTime=3600

#redis settings
redis.minIdle=5
redis.maxIdle=10
redis.maxTotal=50
redis.maxWaitMillis=1500
redis.testOnBorrow=true
redis.numTestsPerEvictionRun=1024
redis.timeBetweenEvictionRunsMillis=30000
redis.minEvictableIdleTimeMillis=1800000
redis.softMinEvictableIdleTimeMillis=10000
redis.testWhileIdle=true
redis.blockWhenExhausted=false

 

-------------------------------------------------------------------web.xml------------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
<display-name>hdemo</display-name>
<!-- 设置跳转页面 -->
 <welcome-file-list>
        <!-- <welcome-file>login.jsp</welcome-file> -->
        <welcome-file>Login.html</welcome-file>
    </welcome-file-list>
    <!-- 惊天自愿配置 -->
      <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.html</url-pattern>
       <!-- <url-pattern>*.jsp</url-pattern>-->
    </servlet-mapping>
    <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
   <!-- 扫描spring开头的xml文件 -->
      <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- 扫描文件 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
          classpath:applicationContext.xml,
          classpath:spring-redis.xml
 <!--,
          classpath:applicationContext-redis.xml -->  <!-- ,
          classpath:applicationContext-shiro.xml -->
         </param-value>
    </context-param>
    <servlet>
        <servlet-name>DruidStatView</servlet-name>
        <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>DruidStatView</servlet-name>
        <url-pattern>/druid/*</url-pattern>
    </servlet-mapping>
    <!-- 启动日志文件 -->
    <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/classes/log4j.properties</param-value>
  </context-param>
  <context-param>
    <param-name>log4jRefreshInterval</param-name>
    <param-value>30000</param-value>
  </context-param>
    <!-- 设置session存在时间 -->
  <session-config>
    <session-timeout>30</session-timeout>
  </session-config>
    
</web-app>

-------------------------------------------------------------------RedisUtil工具类---------------------------------------------------------------------------------

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import redis.clients.jedis.JedisPool;

import java.io.Serializable;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/** 
* @author liupengtao
* @version 2018年12月21日 下午3:45:00
*/
@Repository("RedisUtils")
public class RedisUtils {
    @Autowired
    private JedisPool jedisPool;
    public String get(String key) {
        return jedisPool.getResource().get(key);
    }
 
    public String set(String key, String value) {
        return jedisPool.getResource().set(key,value);
        
    }
    /**
     * redi设置过期时间
     */
    public void expire(String key,int value){
        jedisPool.getResource().expire(key, value);
    }
    /**
     * 批量删除key
     */
    public void del(String... key){
        for(String keys:key){
        jedisPool.getResource().del(keys);
        }
    }

}
-------------------------------------------------------------------controller调用方式------------------------------------------------------------------------------

@RequestMapping("/Login")
@Controller
public class loginController {
    @Autowired
    userService userservice;
    @Autowired
    private RedisUtils redisutils;
    Message message;
    @RequestMapping("/login")
    @ResponseBody
    public Object login(HttpServletRequest req,HttpSession session,huser info){
        String yzm      = req.getParameter("yzm");
    huser user=userservice.getInfoByNamePass(info);
    session=req.getSession();
    if(user!=null){
        session.setAttribute("user", user);
        session.setAttribute("nickname", user.getNickname());
        redisutils.set("121212", user.getNickname());
         Token.createJWT(user.getId(),user.getNickname(),user.getPassword(),201809029);
        message=new Message("200",Token.createJWT(user.getId(),user.getNickname(),user.getPassword(),201809029));
        }else{
            message=new Message("250","error");
        }
        return message;
    }
}

-------------------------------------------------------------------成果展示------------------------------------------------------------------------------------

redis设置密码 config set requirepass foobared(密码)

密码验证:auth foobared(密码)

..我也是醉了,文章类型只有原创、转载、翻译么,我这应该属于翻译吧,把别人的拿来自己理解一下,然后改成自己试用的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值