springmvc整合redis
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));
}
}
}