redis + spring 基本操作
来源:互联网 发布:警察 两种人 程序员 编辑:程序博客网 时间:2024/06/08 10:35
导入 spring 的相关包
导入 redis 包
导入spring 和redis相关包
1 web.xml需要配置spring
<!-- spring 配置 -->
<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>
2导入 spring配置文件
需要添加
<!-- redis 配置 -->
<!-- 配置 链接池-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="300" /><!-- 最大链接数 -->
<property name="maxActive" value="600" /><!-- pool可分配多少个jedis实例 -->
<property name="maxWait" value="1000" /> <!--表示当borrow一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException; -->
<property name="testOnBorrow" value="true"/><!--在borrow一个jedis实例时,是否提前进行alidate操作;如果为true,则得到的jedis实例均是可用的; -->
</bean>
<!--配置链接 -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="localhost" p:port="6379" p:pool-config-ref="poolConfig">
</bean>
<!--配置spring 对 redis 帮助类 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<!--把redisTemplate 注入-->
<bean id="test" class="redis.TestRedis">
<property name="redisTemplate" ref="redisTemplate"></property>
</bean>
3.编写 代码
定义接口:
public interface Test {
//查询
public String get(final String key);
//添加
public void add(Student student) ;
//删除
public void del(String key);
}
编写实现类
public class TestRedis implements Test{
public RedisTemplate getRedisTemplate() {
return redisTemplate;
}
public void setRedisTemplate(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
public RedisTemplate redisTemplate;
//添加 string
public void add(Student student) {
redisTemplate.execute(new RedisCallback() {
@Override
public String doInRedis(RedisConnection arg0)
throws DataAccessException {
String ter ="test";
String ttt ="yuntao";
arg0.set(ter.getBytes(),ttt.getBytes());
return null;
}
}) ;
}
//查询 String
public String get(final String key){
return (String) redisTemplate.execute(new RedisCallback() {
public String doInRedis(RedisConnection connection) throws DataAccessException {
try {
return new String(connection.get(key.getBytes()),"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
});
}
//删除
@Override
public void del(final String key) {
redisTemplate.execute(new RedisCallback() {
public Object doInRedis(RedisConnection arg0)
throws DataAccessException {
arg0.del(key.getBytes());
return null;
}
});
}
}
- redis + spring 基本操作
- Spring Redis基本使用
- redis的基本操作
- redis基本操作
- Redis基本操作
- Redis基本操作
- redis基本操作
- Redis基本操作
- redis基本操作命令
- redis基本操作
- php redis 基本操作
- Redis基本操作
- redis基本操作
- Redis 基本操作
- redis的基本操作
- redis基本操作
- memcached & redis基本操作
- Redis 基本操作一
- restTemplate使用及中文乱码问题
- 排序算法(四)——二分查找算法
- 基础1
- codeforces#371 B.Filya and Homework
- UML行为建模图——— 顺序图
- redis + spring 基本操作
- HUD 5883 The Best Path(2016亚洲区青岛站网络赛)
- 基础2
- java 字符串split有很多坑,使用时请小心!!
- HDU 5881 Tea 2016 ACM/ICPC Asia Regional Qingdao Online 1004
- UML行为建模图——— 活动图
- 计算机原理学习(序)
- linux系统上的帮助信息
- 历史