在网络上有一个很多人转载的springmvc+redis整合的案例,不过一直不完整,也是被各种人装来转去,现在基本将该框架搭建起来。

package com.pudp.bae.base;

import java.io.Serializable;

public abstract class BaseModel implements Serializable{

}

  

package com.pudp.bae.base;

import java.util.Map;

import org.springframework.web.servlet.ModelAndView;

public class BaseMultiController {
protected ModelAndView toView(final String url,final Map<String,Object> map)
{
ModelAndView view = new ModelAndView(url);
return view;
}
}
package com.pudp.bae.base;

import java.io.Serializable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer; public abstract class RedisGeneratorDao<K extends Serializable, V extends Serializable> { @Autowired
protected RedisTemplate<K,V> redisTemplate ; /**
* 设置redisTemplate
* @param redisTemplate the redisTemplate to set
*/
public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) {
this.redisTemplate = redisTemplate;
} /**
* 获取 RedisSerializer
* <br>------------------------------<br>
*/
protected RedisSerializer<String> getRedisSerializer() {
return redisTemplate.getStringSerializer();
} }
package com.pudp.bae.controller;

import java.util.HashMap;
import java.util.Map; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView; import com.pudp.bae.base.BaseMultiController;
import com.pudp.bae.model.Member;
import com.pudp.bae.service.MemberService; @Controller
@RequestMapping(value = "/member")
public class MemberController extends BaseMultiController { @Autowired
private MemberService memberService; public void setMemberService(MemberService memberService) {
this.memberService = memberService;
} @RequestMapping(value = { "/add", "/add.html" }, method = { RequestMethod.GET })
public ModelAndView add(HttpServletRequest request,
HttpServletResponse response) {
Map<String, Object> map = new HashMap<String, Object>();
Member member = new Member();
member.setId("1");
member.setNickname("guoxiaoming");
this.memberService.add(member); return toView("add", map);
} @RequestMapping(value = { "/add", "/add.html" }, method = { RequestMethod.POST })
public ModelAndView addMember(HttpServletRequest request,
HttpServletResponse response,
@ModelAttribute("member") Member member) {
Map<String, Object> map = new HashMap<String, Object>();
System.out.println(member);
map.put("message", "成功添加数据到库," + member);
this.memberService.add(member);
return toView("message", map);
} @RequestMapping(value = { "/{id:\\d+}/query", "/{id:\\d+}/query.html" }, method = {
RequestMethod.GET, RequestMethod.POST })
public ModelAndView queryMember(HttpServletRequest request,
HttpServletResponse response, @PathVariable("id") String id) {
Map<String, Object> map = new HashMap<String, Object>();
System.out.println(id);
Member member = this.memberService.get(id);
if (null != member) {
map.put("message", "查询Id=" + id + "的用户名为:" + member.getNickname());
} else {
map.put("message", "没有查询到与Id=" + id + "相关的数据");
}
return toView("message", map);
} @RequestMapping(value = { "/{id:\\d+}/delete", "/{id:\\d+}/delete.html" }, method = {
RequestMethod.GET, RequestMethod.POST })
public ModelAndView deleteMember(HttpServletRequest request,
HttpServletResponse response, @PathVariable("id") String id) {
Map<String, Object> map = new HashMap<String, Object>();
try {
this.memberService.delete(id);
map.put("message", "删除Id为" + id + "的用户成功.");
} catch (Exception e) {
e.printStackTrace();
map.put("message", "删除Id为" + id + "的用户失败, " + e.getMessage());
}
return toView("message", map);
} }

  

package com.pudp.bae.dao.redis;

import java.util.List;

import com.pudp.bae.model.Member;

public interface MemberDao {
boolean add(Member member); abstract boolean add(List<Member> list); void delete(String key); Member get(String keyId); }

 

package com.pudp.bae.dao.redis;

import java.util.ArrayList;
import java.util.List; import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert; import com.pudp.bae.base.RedisGeneratorDao;
import com.pudp.bae.model.Member; @Repository(value="memberDao")
public class MemberDaoImpl extends RedisGeneratorDao<String,Member> implements MemberDao{ /**
* 添加对象
*/
@Override
public boolean add(final Member member) {
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection)
throws DataAccessException {
RedisSerializer<String> serializer = getRedisSerializer();
byte[] key = serializer.serialize(member.getId());
byte[] name = serializer.serialize(member.getNickname());
return connection.setNX(key, name);
}
});
return result;
} /**
* 添加集合
*/
@Override
public boolean add(final List<Member> list) {
Assert.notEmpty(list);
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection)
throws DataAccessException {
RedisSerializer<String> serializer = getRedisSerializer();
for (Member member : list) {
byte[] key = serializer.serialize(member.getId());
byte[] name = serializer.serialize(member.getNickname());
connection.setNX(key, name);
}
return true;
}
}, false, true);
return result;
} /**
* 删除对象 ,依赖key
*/
@Override
public void delete(String key) {
List<String> list = new ArrayList<String>();
list.add(key);
delete(list);
} /**
* 删除集合 ,依赖key集合
*/
public void delete(List<String> keys) {
redisTemplate.delete(keys);
} /**
* 修改对象
*/
public boolean update(final Member member) {
String key = member.getId();
if (get(key) == null) {
throw new NullPointerException("数据行不存在, key = " + key);
}
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection)
throws DataAccessException {
RedisSerializer<String> serializer = getRedisSerializer();
byte[] key = serializer.serialize(member.getId());
byte[] name = serializer.serialize(member.getNickname());
connection.set(key, name);
return true;
}
});
return result;
} /**
* 根据key获取对象
*/
@Override
public Member get(final String keyId) {
Member result = redisTemplate.execute(new RedisCallback<Member>() {
public Member doInRedis(RedisConnection connection)
throws DataAccessException {
RedisSerializer<String> serializer = getRedisSerializer();
byte[] key = serializer.serialize(keyId);
byte[] value = connection.get(key);
if (value == null) {
return null;
}
String nickname = serializer.deserialize(value);
return new Member(keyId, nickname);
}
});
return result;
} }
package com.pudp.bae.model;

import com.pudp.bae.base.BaseModel;

public class Member extends BaseModel{

  private static final long serialVersionUID = -1959528436584592183L;
private String id;
private String nickname; public Member(){} public Member(String id, String nickname){
this.setId(id);
this.setNickname(nickname);
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getNickname() {
return nickname;
} public void setNickname(String nickname) {
this.nickname = nickname;
} }
package com.pudp.bae.service;

import javax.annotation.Resource;

import com.pudp.bae.dao.redis.MemberDao;
import com.pudp.bae.model.Member; public class MemberService { @Resource(name="memberDao")
private MemberDao memberDao; public void setMemberDao(MemberDao memberDao)
{
this.memberDao = memberDao;
} public void add(Member member){
memberDao.add(member);
} public void delete(String id){
memberDao.delete(id);
} public Member get(String id)
{
return memberDao.get(id);
}
}

redis-context.xml

<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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
"> <!-- scanner redis properties -->
<context:property-placeholder location="/WEB-INF/property/redis.properties" /> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxActive" value="${redis.maxActive}" />
<property name="maxWait" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}"
p:port="${redis.port}"
p:password="${redis.pass}"
p:pool-config-ref="poolConfig"/> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean> </beans>

spring-context.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!-- 激活@Controller模式 -->
<mvc:annotation-driven /> <!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 需要更改 -->
<context:component-scan base-package="com.pudp.bae.*" /> <bean id="memberService" class="com.pudp.bae.service.MemberService"/> <!-- 引入同文件夹下的redis属性配置文件 -->
<import resource="redis-context.xml"/> </beans>

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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 配置视图解析器,把控制器的逻辑视频映射为真正的视图 -->
<!-- /WEB-INF/jsp/start.jsp -->
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean> </beans>

redis.properties

# Redis settings
#redis.host=192.168.20.101
#redis.port=6380
#redis.pass=foobared
redis.host=127.0.0.1
redis.port=6379
redis.pass=xx
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" aaxmlns="http://java.sun.com/xml/ns/j2ee"
aaxmlns:xsi="w3.org/2001/XMLSchema-instance"
aaxsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>SpringMVCRedis</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/context/spring-context.xml</param-value>
</context-param> <filter>
<filter-name>SpringEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter
</filter-class> <init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param> <init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>SpringEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/context/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

2018年5月16号更

时过境迁,没想到两年多前的一个笔记还有那么多人关注,在此补上当时源码包(幸好找到了)

https://download.csdn.net/download/wuxinliulei/10417976

附上csdn的下载路径

SpringMVC+redis整合的更多相关文章

  1. ssm+redis整合(通过cache方式)

    这几天的研究ssm redis 终于进入主题了,今天参考了网上一些文章搭建了一下ssm+redis整合,特别记录下来以便以后可以查询使用,有什么不足请大牛们提点 项目架构 1.pom.xml < ...

  2. [手把手教程][JavaWeb]优雅的SpringMvc+Mybatis整合之路

    来源于:http://www.jianshu.com/p/5124eef40bf0 [手把手教程][JavaWeb]优雅的SpringMvc+Mybatis整合之路 手把手教你整合最优雅SSM框架:S ...

  3. Spring+springmvc+Mybatis整合案例 annotation版(myeclipse)详细版

    Spring+springmvc+Mybatis整合案例 Version:annotation版 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version=&qu ...

  4. Spring+springmvc+Mybatis整合案例 xml配置版(myeclipse)详细版

    Spring+springmvc+Mybatis整合案例 Version:xml版(myeclipse) 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version ...

  5. sonne_game网站开发03 spring-mvc+freemarker整合

    今天的任务就是在spring+mybatis+springmvc的基础上,将freemarker整合进来. freemarker是什么? freemarker是一种模板引擎.它的目的是基于模板和数据, ...

  6. Nginx+Lua+Redis整合实现高性能API接口 - 网站服务器 - LinuxTone | 运维专家网论坛 - 最棒的Linux运维与开源架构技术交流社区! - Powered by Discuz!

    Nginx+Lua+Redis整合实现高性能API接口 - 网站服务器 - LinuxTone | 运维专家网论坛 - 最棒的Linux运维与开源架构技术交流社区! - Powered by Disc ...

  7. SpringMVC 中整合之JSON、XML

    每次看到好的博客我就想好好的整理起来,便于后面自己复习,同时也共享给网络上的伙伴们! 博客地址: springMVC整合Jaxb2.xStream:  http://www.cnblogs.com/h ...

  8. 框架篇:Spring+SpringMVC+hibernate整合开发

    前言: 最近闲的蛋疼,搭个框架写成博客记录下来,拉通一下之前所学知识,顺带装一下逼. 话不多说,我们直接步入正题. 准备工作: 1/ IntelliJIDEA的安装配置:jdk/tomcat等..(本 ...

  9. IDEA下使用maven构建web项目(SpringMVC+Mybatis整合)

    需求背景:由于最近总是接到一些需求,需要配合前端团队快速建设移动端UI应用或web应用及后台业务逻辑支撑的需求,若每次都复用之前复杂业务应用的项目代码,总会携带很多暂时不会用到的功能或组件,这样的初始 ...

随机推荐

  1. springmvc的一个小例子学习(一)

    个人觉得,学框架最好的 方法无外乎两个:一个是实践这个框架,真实的去用它,比如spring框架,先搭一个简单的spring项目,然后一步一步得去丰富它,从中学到spring框架的精髓和知识:另外一个就 ...

  2. U3D 飞机大战(MVC模式)解析--面向对象编程思想

    在自己研究U3D游戏的时候,看过一些人的简单的游戏开发视频,写的不错,只是个人是java web 开发的人,所以结合着MVC思想,对游戏开发进行了一番考虑. 如果能把游戏更加的思想化,分工化,开发便明 ...

  3. JS基础之属性操作注意事项

    1.js中注意问题 font-size 改成fontSize padding-top 改成paddingTop 2.js动态添加Class class    改成className 3.oInp.ty ...

  4. Sql 求比率 类型 影响 结果

  5. SQL Server 2008数据库同步配置

    以下内容转载于网络,个人做了简单的修改.          数据库同步可以是同一个服务器之间不同数据库之间数据同步,也可以是局域网内不同服务器间数据库的同步. 下面具体说一下步骤,首先我们说一下发布服 ...

  6. slickgrid 一个优秀的JS表格插件

    从熟悉JS以来,慢慢的喜欢上了这个门语言. 不自觉的,看了好多js的知识,可能也是因为做项目的原因吧. 这里稍微说下一个JS的grid插件 --slickgrid 了解这个插件也不是很多,稍微了解了下 ...

  7. vs怎么创建MVC及理解其含义

    怎么创建MVC项目 一·1.点击 文件à新建à项目à模板àVisua C#(选择 .NET Framework 4.0或以上版本) à选择 MVC 3 Web应用程序 或者MVC 4 Web应用程序à ...

  8. Windows删除大文件

    Temp是目录 或者是 文件很大很大很大很大 cmd rd /s /q Temp

  9. 20150303--从SQL中获取数据的三级联动

    省市地区的三级联动,每变更一次所选地都需要提交,但是又不需要把整个页面提交,所以我们需要使用控件:UdataPanel.工具--AJAX扩展 还有ScriptManager,并要将其放在页面的最顶端. ...

  10. HTTP Status 500 - An exception occurred processing JSP page /WEB-INF

    HTTP Status 500 - An exception occurred processing JSP page /WEB-INF/test/showCountry.jsp at line 11 ...