整理一下redis与spring的整合。以及使用redisTemplate。首先是要导入spring所需要的jar。当然还有 jedis-2.1.0.jar,commons-pool-1.5.4.jar,spring-data-redis-1.0.0.RELEASE.jar  (这是我使用的版本,应该不新)

1. 导入完这些jar,开始整理配置文件:

首先就是web.xml。这个还是老样子:贴一下吧

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

还有就是spring配置文件,大家也就一看就明白了。我还是叫做applicationContext.xml,

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" default-lazy-init="false">
<!-- 支持注解 -->
<context:annotation-config />
<!-- 组件扫描 -->
<context:component-scan base-package="com.demo" />
<context:property-placeholder location="classpath:redis.properties" /> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxActive" value="${redis.pool.maxActive}" />
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<property name="maxWait" value="${redis.pool.maxWait}" />
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
</bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.ip}" />
<property name="port" value="${redis.port}" />
<property name="poolConfig" ref="jedisPoolConfig" />
</bean> <bean id="template" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean> <bean id="userDao" class="com.demo.spring.UserDaoImpl">
<property name="template" ref="template" />
</bean> </beans>

还有一个就是redis.properties了:

#最大分配的对象数
redis.pool.maxActive=1024
#最大能够保持idel状态的对象数
redis.pool.maxIdle=200
#当池内没有返回对象时,最大等待时间
redis.pool.maxWait=1000
#当调用borrow Object方法时,是否进行有效性检查
redis.pool.testOnBorrow=true #IP
redis.ip=127.0.0.1
#Port
redis.port=6379

好了,配置文件就是这些,也没什么特别之处。

2.编写一个User类:

public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = -1530813282496676263L;
private Integer id;
private String name; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }

3.编写一个userDao和UserDaoImpl

public interface UserDao {
void save(User user);
User read(User user); }
public class UserDaoImpl implements UserDao {

    private RedisTemplate<Serializable, Serializable> template;

    public RedisTemplate<Serializable, Serializable> getTemplate() {
return template;
} public void setTemplate(RedisTemplate<Serializable, Serializable> template) {
this.template = template;
} public User read(final User user) {
// User user2 = (User) template.execute(new RedisCallback<Object>() {
// @Override
// public User doInRedis(RedisConnection connection)throws DataAccessException {
// byte[] key = template.getStringSerializer().serialize("id"+user.getId());
// if(connection.exists(key)){
// byte[] value = connection.get(key);
// String name = template.getStringSerializer().deserialize(value);
// User user1 = new User();
// user1.setName(name);
// return user1;
// }
// return null;
// }
// });
ValueOperations<Serializable, Serializable> opsForValue = template.opsForValue();
User user2 = (User) opsForValue.get(user.getId());
return user2; } public void save(final User user) {
// template.execute(new RedisCallback<Object>() {
// public Object doInRedis(RedisConnection connection) throws DataAccessException {
// connection.set(template.getStringSerializer().serialize("id"+user.getId()), template.getStringSerializer().serialize(user.getName()));
// return null;
// }
// }); ValueOperations<Serializable, Serializable> opsForValue = template.opsForValue();
opsForValue.set(user.getId(), user);
} }

这里注释部分是另一种方法,也是可以的,自定义对象需要序列化  template.getStringSerializer().serialize("xxx");

注释中connection方法很多例如:

connection.mGet(keys);//byte[]... keys
connection.mSet(tuple);//Map<byte[], byte[]> tuple
connection.lSet(key, index, value);//byte[] key, long index, byte[] value
connection.lRange(key, begin, end);//byte[] key, long begin, long end)等等

4.最后是测试类

private ApplicationContext app;
private UserDao userDao; public UserDao getUserDao() {
return userDao;
} public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} @Before
public void before() throws Exception {
app = new ClassPathXmlApplicationContext("applicationContext.xml");
     //得到userDao 对象
userDao = (UserDao) app.getBean("userDao");
} @Test
public void test1() { String name = "fu";
User user = new User();
user.setName(name);
user.setId(1);
userDao.save(user); System.out.println("============添加完成"); User u = userDao.read(user); System.out.println("============获取:" + u.getName());
}
哦了,基本的整合也就完成了。。

jedis与spring整合及简单的使用RedisTemplate操作的更多相关文章

  1. Spring整合strus2简单应用总结

    本身strus2没接触过,所以这块学的一知半解,正常不整合的还没学(接着学) step: 1.创建web工程 2.在/WEB-INF/lib引入jar包 asm-3.3.jarasm-commons- ...

  2. Spring整合MyBatis(简单登录Demo)

    SpringMvc简单整合(登录模块) 1.1 整合思路 1.SqlSessionFactory对象应该放到spring容器中作为单例存在. 2.传统dao的开发方式中,应该从spring容器中获得s ...

  3. Spring Security4.X 简单实例介绍

    简介 本例子采用的是SpringMVC.SpringSecurity和Spring整合的简单使用 使用gradle搭建的项目(gradle比maven更加便捷),可以自行了解 web.xml配置 &l ...

  4. Redis入门很简单之五【Jedis和Spring的整合】

    Redis入门很简单之五[Jedis和Spring的整合] 博客分类: NoSQL/Redis/MongoDB redisnosql缓存jedisspring  在上一篇文章中,简单介绍了Jedis的 ...

  5. redis集群配置,spring整合jedis,缓存同步

    前台的商品数据(图片等加载缓慢)查询,先从redis缓存查询数据. redis是一个nosql数据库,内存版数据库,读取速度11w/s.本身具有内存淘汰机制,是单线程服务器(分时操作系统),线程安全. ...

  6. spring+springMVC+mybatis简单整合

    spring+springMVC+mybatis简单整合, springMVC框架是spring的子项目,所以框架的整合方式为,spring+Mybatis或springMVC+mybatis. 三大 ...

  7. 简单探讨spring整合mybatis时sqlSession不需要释放关闭的问题

    https://blog.csdn.net/RicardoDing/article/details/79899686 近期,在使用spring和mybatis框架编写代码时,sqlSession不需要 ...

  8. Spring整合JDBC实现简单的增删改

    Spring整合JDBC实现简单的增删改: 1.导入Spring的包和数据库的驱动包: 2.选择一个数据源(dbcp和C3P0) 3.导入数据源的包(这里我们使用dbcp) <span styl ...

  9. spring整合apache-shiro的简单使用

    这里不对shiro进行介绍,介绍什么的没有意义 初学初用,不求甚解,简单使用 一.导入jar包(引入坐标) <!--shiro和spring整合--> <dependency> ...

随机推荐

  1. LUOGU P1313 计算系数 (组合数学)

    解题思路 比较简单的题,用二项式定理即可. #include<iostream> #include<cstdio> #include<cstring> #inclu ...

  2. iOS开发之IMP和SEL(方法和类的反射)

    1.SEL:类方法的指针,相当于一种编号,区别与IMP! IMP:函数指针,保存了方法的地址! SEL是通过表取对应关系的IMP,进行方法的调用! 2.获取SEL和IMP方法和调用: SEL meth ...

  3. Hibernate O/R 映射

    O/R 映射 目前为止我们已经通过应用 Hibernate 见识过十分基础的 O/R 映射了,但是还有三个更加重要的有关映射的话题需要我们更详细的探讨.这三个话题是集合的映射,实体类之间的关联映射以及 ...

  4. shell对文件,文件夹的操作

    x=$ OUTFILENAME="output_${x}.sql" if [ -f $OUTFILENAME ];then rm $OUTFILENAME fi 如果文件存在则删除 ...

  5. MyBatis与JPA的区别

    参考博客: https://www.cnblogs.com/llywy/p/10103136.html https://www.jianshu.com/p/32ce87c163d6 MyBatis分为 ...

  6. ES5-ES8 数组拥有的方法

    1.判断是否是数组 Array.isArray( arg ) 有兼容性 2.toString 数组转字符串 arr.toString(); 3.join 数组每一项间的拼接 arr.join(); S ...

  7. linux服务器项目搭建常用命令

    linux下载链接文件 wget -c 后面是该网络地址和文件的位置. 例如:wget -c http://apache.opncas.or/MySQL/MySQL-7/v7.0.67/bin/MyS ...

  8. [转]js模块化——AMD及require.js

    由CommonJS组织提出了许多新的JavaScript架构方案和标准,希望能为前端开发提供统一的指引.AMD规范就是其中比较著名一个,全称是Asynchronous Module Definitio ...

  9. SQL2005自动备份,定期删除的维护计划及自动定期清除日志

    作为一名DBA,他们最常见的日常任务是: 1)定期完成数据库的完全备份或差异备份.2)定期清理备份文件,因为存储空间有限,可能只需要保存一个时期段内的文件(比如一周内或一月内). 而如何做到这两点呢? ...

  10. Python技巧—list与字符串互相转换

    Python技巧-list与字符串互相转换 在Python的编程中,经常会涉及到字符串与list之间的转换问题,下面就将两者之间的转换做一个梳理. 1.list转换成字符串 命令:list() 例子: ...