0下载redis(windows版本)
https://raw.githubusercontent.com/ServiceStack/redis-windows/master/downloads/redis-latest.zip
压缩包文件目录
1.启动服务:双击 redis-server.exe 或者cmd下进入本目录 执行 redis-server
2.’如果需要配置东西,譬如配置密码requirepass(在redis.windows.conf里面配置)
则cmd下执行命令:redis-server redis.windows.conf 可以把上述命令写一个bat文件
ps:如果不配置文件,每次重启redis,譬如你命令设置了密码,重启都会还原(变成了没密码)
3.如果不用bat命令,也可以创建一个redis-server的快捷方式
然后右键快捷方式的属性,把配置文件给加上,效果一样
linux下的安装和配置自行百度
1.导入jar包
<!-- redis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.1.RELEASE</version>
</dependency>
一开始这两个都使用最新的jar包,不兼容
报这个错误
java.lang.NoSuchMethodError: org.springframework.core.serializer.support.DeserializingConverter.<init>(Ljava/lang/ClassLoader;)V
就找了两个2015的版本就行了
2.配置xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<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="maxTotal" value="${redis.maxActive}" />
<property name="maxWaitMillis" value="${redis.maxWait}"></property>
<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.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<!-- 下面这个是开启事务的-->
<property name="enableTransactionSupport" value="true" />
</bean>
</beans>
这个过程遇到的两个错误
1.旧版本是这两个属性
<!-- <property name="maxActive" value="${redis.maxActive}" /> -->
<!-- <property name="maxWait" value="${redis.maxWait}" /> -->
新版本是这两个属性
<property name="maxTotal" value="${redis.maxActive}" />
<property name="maxWaitMillis" value="${redis.maxWait}"></property>
如果启动报这两个属性没有的,就新旧替换一下
2.
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
一开始这里的class用的是org.springframework.data.redis.core.StringRedisTemplate
然后java里注解的是RedisTemplate,
@Autowired
protected RedisTemplate<K, V> redisTemplate ;
然后启动就报没有这样的bean错误
No qualifying bean of type [org.springframework.data.redis.core.RedisTemplate] found
for
dependency
要初始化的和注解的对应上
3.redis查询数据的时候,我的配置文件redis.host配置成localhost会报错
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.UnknownHostException: localhost
配置成ip 127.0.0.1就不可以查询了,暂时不知道原因在哪
3.属性文件
#ip
redis.host=127.0.0.1
#端口
redis.port=6379
#密码
redis.pass=
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true
#以下属性是旧版本的,目前只知道maxActive和maxWait新版本变了
##最大连接数:能够同时建立的“最大链接个数”
#redis.pool.maxActive=200
##最大空闲数:空闲链接数大于maxIdle时,将进行回收
#redis.pool.maxIdle=20
##最小空闲数:低于minIdle时,将创建新的链接
#redis.pool.minIdle=5
##最大等待时间:单位ms
#redis.pool.maxWait=3000
##使用连接时,检测连接是否成功
#redis.pool.testOnBorrow=true
#返回连接时,检测连接是否成功
redis.pool.testOnReturn=true
4.demo dao
一个redisbasedao
package com.zhshch.redis.basedao;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
public abstract class RedisBaseDao<K,V> {
//注解得到操作模板
@Autowired
protected RedisTemplate<K, V> redisTemplate ;
//得到序列化对象
protected RedisSerializer<String> getRedisSerializer() {
return redisTemplate.getStringSerializer();
}
//把字符串序列化
protected byte[] serialize(String string){
return getRedisSerializer().serialize(string) ;
}
//反序列化
protected String deserialize(byte[] bytes){
return getRedisSerializer().deserialize(bytes);
}
//根据对象得到hash的Map<byte[],byte[]>
@SuppressWarnings("unchecked")
protected Map<byte[],byte[]> getHashes(Object obj){
Map<byte[],byte[]> hashes = new HashMap<byte[], byte[]>() ;
Class clazz = obj.getClass() ;
Field[] fields = clazz.getDeclaredFields() ;
for(Field field : fields){
String name = field.getName();
byte[] nameByte = serialize(name) ;
//
Method method = null ;
String value = null ;
byte[] valueByte = null ;
try {
method = clazz.getMethod(name,null) ;
value = (String) method.invoke(obj, null) ;
} catch (Exception e) {
e.printStackTrace();
}
valueByte = serialize(value) ;
//
hashes.put(nameByte, valueByte) ;
}
return hashes ;
}
}
实现类:(接口就不写了)
package com.zhshch.redis.dao.impl;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.stereotype.Repository;
import com.zhshch.po.Person;
import com.zhshch.redis.basedao.RedisBaseDao;
import com.zhshch.redis.dao.IRedisTestDao;
@Repository
@SuppressWarnings("unchecked")
public class RedisTestDao extends RedisBaseDao implements IRedisTestDao {
public boolean add(final Person person) {
boolean result = (Boolean) super.redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection conn) throws DataAccessException {
byte[] key = serialize(person.getId()) ;
Map<byte[],byte[]> hashes = getHashes(person) ;
conn.hMSet(key, hashes);
return true;
}
}) ;
return result ;
}
public boolean add(final List<Person> persons) {
boolean result = (Boolean) super.redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection conn) throws DataAccessException {
for(Person person : persons){
byte[] key = serialize(person.getId()) ;
Map<byte[],byte[]> hashes = getHashes(person) ;
conn.hMSet(key, hashes);
}
return true;
}
}) ;
return result ;
}
public void delete(final String key) {
boolean result = (Boolean) super.redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection conn) throws DataAccessException {
byte[] keyByte = serialize(key) ;
conn.del(keyByte) ;
return true;
}
}) ;
}
public void delete(final List<String> keys) {
}
public boolean update(final Person person) {
boolean result = add(person) ;
return result ;
}
public Person get(final String key) {
Person result = null ;
if(super.redisTemplate.hasKey(key)){
result = (Person) super.redisTemplate.execute(new RedisCallback<Person>() {
public Person doInRedis(RedisConnection conn) throws DataAccessException {
byte[] keyByte = serialize(key) ;
byte[] nameByte = serialize("name") ;
byte[] ageByte = serialize("age") ;
byte[] idByte = serialize("id") ;
String name = deserialize(conn.hGet(keyByte, nameByte)) ;
String age = deserialize(conn.hGet(keyByte, ageByte)) ;
String id = deserialize(conn.hGet(keyByte, idByte)) ;
Person p = new Person();
p.setName(name);
p.setAge(Integer.parseInt(age));
p.setId(id);
return p;
}
}) ;
}
return result ;
}
public List<Person> get(final List<String> keys) {
// TODO Auto-generated method stub
return null;
}
}
service调用就可以,
其他具体语法可以百度找