Redis(六)-- SpringMVC整合Redis
一、pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xbq.demo</groupId>
<artifactId>SpringRedisDemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringRedisDemo Maven Webapp</name>
<url>http://maven.apache.org</url> <!-- Spring版本号 -->
<properties>
<org.springframework.version>4.2.3.RELEASE</org.springframework.version>
</properties> <dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>
</dependencies> <build>
<finalName>SpringRedisDemo</finalName>
</build>
</project>
二、applicaltionContext.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd ">
<!-- 自动扫描 -->
<context:component-scan base-package="com.xbq.demo.util"/>
<!-- 加载文件 -->
<context:property-placeholder location="classpath:redis.properties" /> <!-- jedis 配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="minIdle" value="${redis.minIdle}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<!-- redis服务器中心 -->
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="poolConfig" ref="poolConfig" />
<property name="port" value="${redis.port}" />
<property name="hostName" value="${redis.host}" />
<property name="password" value="${redis.pass}" />
<property name="timeout" value="${redis.timeout}" />
</bean>
<!-- redis操作模板,面向对象的模板 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<!-- 如果不配置Serializer,那么存储的时候只能使用String,如果用对象类型存储,那么会提示错误 -->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
</bean>
</beans>
三、redis.properties
redis.host=192.168.242.130
redis.port=6379
redis.pass=xbq123
redis.timeout=-1 redis.maxIdle=100
redis.minIdle=8
redis.maxWait=-1
redis.testOnBorrow=true
四、spring-mvc.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp"></property>
</bean> <bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
<property name="maxUploadSize" value="10000000"/>
</bean> <!-- 使用注解的包,包括子集 -->
<context:component-scan base-package="com.xbq.demo.controller"/>
</beans>
五、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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringDemoTest</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:applicationContext.xml,
</param-value>
</context-param> <!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!--spring监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 防止spring内存溢出监听器-->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener> <!-- 添加对springmvc的支持 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
六、封装一个操作hash基本数据类型的工具类
package com.xbq.demo.util; import javax.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component; /**
* 操作 hash 的基本操作
* @author xbq
*/
@Component("redisCache")
public class RedisCacheUtil { @Resource
private StringRedisTemplate redisTemplate; /**
* 向Hash中添加值
* @param key 可以对应数据库中的表名
* @param field 可以对应数据库表中的唯一索引
* @param value 存入redis中的值
*/
public void hset(String key, String field, String value) {
if(key == null || "".equals(key)){
return ;
}
redisTemplate.opsForHash().put(key, field, value);
} /**
* 从redis中取出值
* @param key
* @param field
* @return
*/
public String hget(String key, String field){
if(key == null || "".equals(key)){
return null;
}
return (String) redisTemplate.opsForHash().get(key, field);
} /**
* 判断 是否存在 key 以及 hash key
* @param key
* @param field
* @return
*/
public boolean hexists(String key, String field){
if(key == null || "".equals(key)){
return false;
}
return redisTemplate.opsForHash().hasKey(key, field);
} /**
* 查询 key中对应多少条数据
* @param key
* @return
*/
public long hsize(String key) {
if(key == null || "".equals(key)){
return 0L;
}
return redisTemplate.opsForHash().size(key);
} /**
* 删除
* @param key
* @param field
*/
public void hdel(String key, String field) {
if(key == null || "".equals(key)){
return;
}
redisTemplate.opsForHash().delete(key, field);
}
}
七、测试类
package com.xbq.demo; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xbq.demo.util.RedisCacheUtil; /**
* 测试
*/
public class RedisTest { private RedisCacheUtil redisCache;
private static String key;
private static String field;
private static String value; @Before
public void setUp() throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.start();
redisCache = (RedisCacheUtil) context.getBean("redisCache");
} // 初始化 数据
static {
key = "tb_student";
field = "stu_name";
value = "一系列的关于student的信息!";
} // 测试增加数据
@Test
public void testHset() {
redisCache.hset(key, field, value);
System.out.println("数据保存成功!");
} // 测试查询数据
@Test
public void testHget(){
String re = redisCache.hget(key, field);
System.out.println("得到的数据:" + re);
} // 测试数据的数量
@Test
public void testHsize(){
long size = redisCache.hsize(key);
System.out.println("数量为:" + size);
}
}
八、控制层 StudentController
package com.xbq.demo.controller; import java.io.PrintWriter;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.xbq.demo.util.RedisCacheUtil; /**
* 控制层
* @author xbq
*/
@Controller
@RequestMapping("/student/")
public class StudentController { @Resource
private RedisCacheUtil redisCache; // 查询数据
@RequestMapping("list")
public String list(HttpServletResponse response, HttpServletRequest request){
String re = redisCache.hget("tb_student", "stu_id");
try {
this.write(response, re);
} catch (Exception e) {
e.printStackTrace();
}
return null;
} /**
* 回写到页面上
* @param response
* @param o
* @throws Exception
*/
private void write(HttpServletResponse response, Object o) throws Exception{
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
out.println(o.toString());
out.flush();
out.close();
}
}
九、jsp页面
<body>
Hello World!
<a href="${pageContext.request.contextPath}/student/list.do">显示数据</a>
</body>
十、源码下载
Redis(六)-- SpringMVC整合Redis的更多相关文章
- springmvc整合redis架构搭建实例
新换环境,又有新东西可以学习了,哈皮! 抽空学习之余看了一下redis,个人对Springmvc的爱是忠贞不渝,所以整理了一下Springmvc整合redis的环境搭建.分享学习. 第一步: 创建ma ...
- Redis(七)-- SpringMVC整合Redis集群
1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...
- springmvc 整合 redis
引入依赖 <!--redis--> <dependency> <groupId>redis.clients</groupId> <artifact ...
- Redis-基本概念、java操作redis、springboot整合redis,分布式缓存,分布式session管理等
NoSQL的引言 Redis数据库相关指令 Redis持久化相关机制 SpringBoot操作Redis Redis分布式缓存实现 Resis中主从复制架构和哨兵机制 Redis集群搭建 Redis实 ...
- 【SpringBoot | Redis】SpringBoot整合Redis
SpringBoot整合Redis 1. pom.xml中引入Redis相关包 请注意,这里我们排除了lettuce驱动,采用了jedis驱动 <!-- redis的依赖 --> < ...
- springmvc整合redis
1.引入依赖 2.在resources包下创建配置文件redis-context.xml 3.在spring-servlet文件引入redis配置文件
- 【Redis】SpringBoot整合Redis
一.Maven依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId& ...
- springMVC整合jedis+redis,以注解形式使用
前两天写过 springMVC+memcached 的整合,我从这个基础上改造一下,把redis和springmvc整合到一起. 和memcached一样,redis也有java专用的客户端,官网推荐 ...
- springMVC整合jedis+redis
http://www.cnblogs.com/zhengbn/p/4140549.html 前两天写过 springMVC+memcached 的整合,我从这个基础上改造一下,把redis和sprin ...
随机推荐
- 【Unity笔记】常用插件
记录一些常见插件,随时补充. iTween动画插件 原理:插值法,给出初始值和终点值,自动算出中间值. DoTween Tween动画 Playmaker $45 Playmaker由第三方软件商Hu ...
- 3DS更新R4烧录卡内核
机子是N3DSLL,用的R4烧录卡是银卡HK版. 关于R4烧录卡的基础知识科普贴: https://tieba.baidu.com/p/4855297365 为了防止该网页挂掉还是存图吧. 找最新内核 ...
- 【WPF】XAML引入资源和在C#代码中动态添加样式
转载自: http://blog.csdn.net/honantic/article/details/48781543 XAML引入资源参考这里: http://blog.csdn.net/qq_18 ...
- IP数据包格式
IP数据包格式 0 4 8 16 31 |4位版本 | 4位首部长度 | 8位服务类型 | 16位总长度(字节数)| |16位标识 | 3位标志 | 13位片偏移 | |8位生存时间| 8位协议 | ...
- jquery轮播控件
网上找了一个轮播控件,效果不错,而且很容易改,需要的同学去下载咯 地址是:http://download.csdn.net/detail/jine515073/7704143
- spring boot之访问静态页面
楼主前两天自学spring boot,然后在学习的过程中,出现一个疑问,就是如何去访问静态的html网页,这个问题,楼主上网上搜了下,找到的是在resource目录下建立一个templates文件夹, ...
- java-事务-案例
项目结构: 数据库: /* SQLyog Ultimate v12.09 (64 bit) MySQL - 5.5.53 : Database - threadlocal ************** ...
- pick王菊?作为“菊外人”的程序员能做点什么?
转载:https://mp.weixin.qq.com/s/s1cb9Ij6ouTYYCZovLhXTA 最近,想必大家的朋友圈都被“王菊”占领了,打开朋友圈到处可以见到“pick王菊”.“陶渊明”. ...
- C++中 char *s 和 char s[] 的区别
原因 刚好看到给main传递参数,书上(C++Primer)说“ int main(int argc, char *argv[])也可以写成 int main(int argc, char **arg ...
- c++下面的一个单例
Public FileManager { private FileManager(); public static FileManager Instance=new FileManager(); } ...