博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Redis(六)-- SpringMVC整合Redis
阅读量:4347 次
发布时间:2019-06-07

本文共 11497 字,大约阅读时间需要 38 分钟。

一、pom.xml

1 
3
4.0.0
4
com.xbq.demo
5
SpringRedisDemo
6
war
7
0.0.1-SNAPSHOT
8
SpringRedisDemo Maven Webapp
9
http://maven.apache.org
10 11
12
13
4.2.3.RELEASE
14
15 16
17
18
javax.servlet
19
servlet-api
20
2.5
21
22
23
junit
24
junit
25
4.10
26
test
27
28
29
commons-logging
30
commons-logging
31
1.1.1
32
33
34
log4j
35
log4j
36
1.2.16
37
38
39
org.slf4j
40
slf4j-api
41
1.7.7
42
43
44
org.slf4j
45
slf4j-log4j12
46
1.7.7
47
48
49
redis.clients
50
jedis
51
2.9.0
52
53
54
org.apache.commons
55
commons-pool2
56
2.4.2
57
58
59
commons-fileupload
60
commons-fileupload
61
1.3.2
62
63
64
org.springframework.data
65
spring-data-redis
66
1.8.1.RELEASE
67
68
69
org.springframework
70
spring-context
71
${org.springframework.version}
72
73
74
org.springframework
75
spring-core
76
${org.springframework.version}
77
78
79
org.springframework
80
spring-beans
81
${org.springframework.version}
82
83
84
org.springframework
85
spring-webmvc
86
${org.springframework.version}
87
88
89
org.springframework
90
spring-orm
91
${org.springframework.version}
92
93
94
org.springframework
95
spring-test
96
${org.springframework.version}
97
98
99
org.springframework
100
spring-aspects
101
${org.springframework.version}
102
103
104
org.springframework
105
spring-tx
106
${org.springframework.version}
107
108
109 110
111
SpringRedisDemo
112
113
View Code

二、applicaltionContext.xml

1 
2
16
17
18
19
20 21
22
23
24
25
26
27
28
29
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

三、redis.properties

1 redis.host=192.168.242.1302 redis.port=63793 redis.pass=xbq1234 redis.timeout=-15   6 redis.maxIdle=1007 redis.minIdle=88 redis.maxWait=-19 redis.testOnBorrow=true

四、spring-mvc.xml

1 
2
17 18
19
21
22
23
24 25
27
28
29
30 31
32
33

五、web.xml

SpringDemoTest
index.jsp
contextConfigLocation
classpath*:applicationContext.xml,
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
true
encoding
UTF-8
encodingFilter
/*
org.springframework.web.context.ContextLoaderListener
org.springframework.web.util.IntrospectorCleanupListener
springMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath*:spring-mvc.xml
1
true
springMVC
*.do

六、封装一个操作hash基本数据类型的工具类

1 package com.xbq.demo.util; 2  3 import javax.annotation.Resource; 4 import org.springframework.data.redis.core.StringRedisTemplate; 5 import org.springframework.stereotype.Component; 6  7 /** 8  * 操作 hash 的基本操作 9  * @author xbq10  */11 @Component("redisCache")12 public class RedisCacheUtil {13 14     @Resource15     private StringRedisTemplate  redisTemplate;16     17     /**18      * 向Hash中添加值19      * @param key      可以对应数据库中的表名20       * @param field    可以对应数据库表中的唯一索引21      * @param value    存入redis中的值22      */23     public void hset(String key, String field, String value) {24         if(key == null || "".equals(key)){25             return ;26         }27         redisTemplate.opsForHash().put(key, field, value);28     }29     30     /**31      * 从redis中取出值32      * @param key33      * @param field34      * @return35      */36     public String hget(String key, String field){37         if(key == null || "".equals(key)){38             return null;39         }40         return (String) redisTemplate.opsForHash().get(key, field);41     }42     43     /**44      * 判断 是否存在 key 以及 hash key45      * @param key46      * @param field47      * @return48      */49     public boolean hexists(String key, String field){50         if(key == null || "".equals(key)){51             return false;52         }53         return redisTemplate.opsForHash().hasKey(key, field);54     }55     56     /**57      * 查询 key中对应多少条数据58      * @param key59      * @return60      */61     public long hsize(String key) {62         if(key == null || "".equals(key)){63             return 0L;64         }65         return redisTemplate.opsForHash().size(key);66     }67     68     /**69      * 删除70      * @param key71      * @param field72      */73     public void hdel(String key, String field) {74         if(key == null || "".equals(key)){75             return;76         }77         redisTemplate.opsForHash().delete(key, field);78     }79 }

七、测试类

1 package com.xbq.demo; 2  3 import org.junit.Before; 4 import org.junit.Test; 5 import org.springframework.context.support.ClassPathXmlApplicationContext; 6 import com.xbq.demo.util.RedisCacheUtil; 7  8 /** 9  * 测试10  */11 public class RedisTest {12 13     private RedisCacheUtil redisCache;14     private static String key;15     private static String field;16     private static String value;17     18     @Before19     public void setUp() throws Exception {20         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");21         context.start();22         redisCache = (RedisCacheUtil) context.getBean("redisCache");23     }24 25     // 初始化 数据26     static {27         key = "tb_student";28         field = "stu_name";29         value = "一系列的关于student的信息!";30     }31     32     // 测试增加数据33     @Test34     public void testHset() {35         redisCache.hset(key, field, value);36         System.out.println("数据保存成功!");37     }38 39     // 测试查询数据40     @Test41     public void testHget(){42         String re = redisCache.hget(key, field);43         System.out.println("得到的数据:" + re);44     }45     46     // 测试数据的数量47     @Test48     public void testHsize(){49         long size = redisCache.hsize(key);50         System.out.println("数量为:" + size);51     }52 }

八、控制层 StudentController

1 package com.xbq.demo.controller; 2  3 import java.io.PrintWriter; 4 import javax.annotation.Resource; 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 import org.springframework.stereotype.Controller; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import com.xbq.demo.util.RedisCacheUtil;10 11 /**12  * 控制层13  * @author xbq14  */15 @Controller16 @RequestMapping("/student/")17 public class StudentController {18 19     @Resource20     private RedisCacheUtil redisCache;21     22     // 查询数据23     @RequestMapping("list")24     public String list(HttpServletResponse response, HttpServletRequest request){25         String re = redisCache.hget("tb_student", "stu_id");26         try {27             this.write(response, re);28         } catch (Exception e) {29             e.printStackTrace();30         }31         return null;32     }33     34     /**35      * 回写到页面上36      * @param response37      * @param o38      * @throws Exception39      */40     private void write(HttpServletResponse response, Object o) throws Exception{41         response.setContentType("text/html;charset=utf-8");42         PrintWriter out=response.getWriter();43         out.println(o.toString());44         out.flush();45         out.close();46     }47 }

九、jsp页面

    Hello World!    显示数据

十、源码下载

   

 

转载于:https://www.cnblogs.com/xbq8080/p/6739152.html

你可能感兴趣的文章
PL/SQL系列1-PL/SQL介绍
查看>>
关于render函数的总结
查看>>
JavaScript 小刮号
查看>>
BZOJ USACO 银组 水题集锦
查看>>
Android为TV端助力 Linux命令查看包名类名
查看>>
【zabbix】自动注册,实现自动发现agent并添加监控(agent不需要任何配置)
查看>>
[简单到爆]eclipse-jee-neon的下载和安装
查看>>
vector
查看>>
Redis学习之set类型总结
查看>>
栈和队列
查看>>
CSS2-3常见的demo列子总结一
查看>>
XML & JSON---iOS-Apple苹果官方文档翻译
查看>>
数据挖掘之功能
查看>>
2018-07-13E-R图设计数据库+三大范式+修改用户密码+分配用户权限
查看>>
移动广告行业的复苏
查看>>
Cookie机制,session机制
查看>>
nginx配置错误
查看>>
47 【golang】mysql操作
查看>>
Using ARITHABORT with LLBLGen
查看>>
增量模型与快速模型的异同。
查看>>