1.maven
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.2</version>
</dependency>
2.创建applicationContext-redis.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 连接池配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大连接数 -->
<property name="maxTotal" value="30" />
<!-- 最大空闲连接数 -->
<property name="maxIdle" value="10" />
<!-- 每次释放连接的最大数目 -->
<property name="numTestsPerEvictionRun" value="1024" />
<!-- 释放连接的扫描间隔(毫秒) -->
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<!-- 连接最小空闲时间 -->
<property name="minEvictableIdleTimeMillis" value="1800000" />
<!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
<property name="softMinEvictableIdleTimeMillis" value="10000" />
<!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
<property name="maxWaitMillis" value="1500" />
<!-- 在获取连接的时候检查有效性, 默认false -->
<property name="testOnBorrow" value="true" />
<!-- 在空闲时检查有效性, 默认false -->
<property name="testWhileIdle" value="true" />
<!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
<property name="blockWhenExhausted" value="false" />
</bean>
<!-- redis单机 通过连接池 -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="close">
<constructor-arg name="poolConfig" ref="jedisPoolConfig" />
<constructor-arg name="host" value="127.0.0.1" />
<constructor-arg name="port" value="6379" />
</bean>
</beans>
3.web.xml
读取applicationContext-redis.xml
<!-- 读取spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:conf/spring-mybatis.xml,classpath:conf/applicationContext-redis.xml
</param-value>
</context-param>
4.注册登录使用
package com.steam.controller;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.google.gson.Gson;
import com.steam.VO.CodesToken;
import com.steam.VO.CodesVO;
import com.steam.entity.Article;
import com.steam.entity.User;
import com.steam.service.UserService;
import com.steam.util.FileUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@Controller
public class UserController {
@Autowired
private UserService userService;
@Autowired
private JedisPool jedisPool;
/**
* 用户注册的方法
*
* @param user
* 用户注册的属性
*/
@RequestMapping(value = "addUser")
@ResponseBody
public String addUser(User user) {
int addUser = userService.addUser(user);
Gson gson = new Gson();
if (addUser > 0) {
User login = userService.login(user, "1");// 走注册的一定是普通的注册
// 第三方会直接走登陆不会走注册
String uuid = UUID.randomUUID().toString();
try {
Jedis resource = jedisPool.getResource();
resource.setex(uuid, 60 * 60, gson.toJson(login));
} catch (Exception e) {
System.out.println("Redis服务未启动");
e.printStackTrace();
}
CodesToken CodesToken = new CodesToken();
CodesToken.setStatus("0");
CodesToken.setResult("注册成功");
CodesToken.setToken(uuid);
CodesToken.setObject(login);
return gson.toJson(CodesToken);
} else if (addUser == -1) {
return gson.toJson(new CodesVO("-1", "该手机号已经注册", null));
} else {
return gson.toJson(new CodesVO("1", "注册失败", null));
}
}
/**
* 用户登陆的方法(包括普通的登陆和第三方的登陆)
*
* @param user
* 用户注册的属性
* @param appType
* 1是普通登陆 2是第三方登陆
*/
@RequestMapping(value = "login", method = RequestMethod.GET)
@ResponseBody
public String login(User user, String appType) {
User selectUser = userService.login(user, appType);
Gson gson = new Gson();
if (selectUser != null) {// 根据登陆信息查询出来的人的信息
String uuid = UUID.randomUUID().toString();
try {
Jedis resource = jedisPool.getResource();
resource.setex(uuid, 60 * 6000, gson.toJson(selectUser));
} catch (Exception e) {
System.out.println("Redis服务未启动");
e.printStackTrace();
}
CodesToken CodesToken = new CodesToken();
CodesToken.setStatus("0");
CodesToken.setResult("登陆成功");
CodesToken.setToken(uuid);
CodesToken.setObject(selectUser);
return gson.toJson(CodesToken);
} else {
return gson.toJson(new CodesVO("1", "登陆失败", null));
}
}
}