最近一直是以自学的状态,想把自己学习到的知识分享给大家 ,也是好久没有写博客了。废话不多说 ,直接上代码。
本次项目是 用maven 管理的.开发工具 eclipse
在pom.xml文件中,中关于redis的配置:
org.springframework.data
spring-data-redis
1.0.2.RELEASE
redis.clients
jedis
2.1.0
在 applicationContext.xml 中关于redis的配置,<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
在web.xml<?xml version="1.0" encoding="UTF-8"?>
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_3_0.xsd"
version="3.0">
Archetype Created Web Application
contextConfigLocation
classpath:spring-mybatis.xml,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
/
/index.jsp
由于是入门的很简单的demo ,所以直接在 写Controllerpackage com.basessm.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("redis")
public class RedisController {
@Autowired
private StringRedisTemplate redisTemplate;// redis操作模板
@RequestMapping("test1")
public void test1() {
redisTemplate.opsForValue().set("fruit3","apple3");
System.out.println("success1");
}
@RequestMapping("test2")
public void test2() {
redisTemplate.opsForValue().set("aliceWang","alice2");
String a = redisTemplate.opsForValue().get("aliceWang");
System.out.println("success2");
System.out.println("a:" + a);
}
}
这样就已经完成了一个 很基础,很简单的 demo了,在运行项目之前一定要记得先把redis-server 启动起来!