java整合Redis
1、引入依赖或者导入jar包
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
2、代码实现
public class JedisTest {
 public static void main(String[] args) {
  //连接redis
  //Jedis jedis=new Jedis("192.168.65.128",6379);
  //使用Jedis连接池
  Jedis jedis=getJedis();
  //操作redis
  jedis.set("name","小白");
  jedis.set("age","19");
  System.out.println("操作成功!");
  jedis.close();
 }
 public static Jedis getJedis(){
  //创建连接池配置对象
  JedisPoolConfig config=new JedisPoolConfig();
  config.setMaxIdle(10);
  config.setMinIdle(5);
  config.setMaxTotal(100);
  //需要redis的服务密码时,使用第一种创建方式
  //JedisPool jedisPool=new JedisPool(config,"192.168.65.128",6379,10000,"root");
  JedisPool jedisPool=new JedisPool(config,"192.168.65.128",6379,10000);
  return jedisPool.getResource();
 }
}
Spring整合Redis
1、添加依赖
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>2.1.8.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
2、redis配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--1、配置redis连接池对象--> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!--最大空闲数--> <property name="maxIdle" value="50"/> <!--最大连接数--> <property name="maxTotal" value="100"/> <!--最大等待时间--> <property name="maxWaitMillis" value="60000"/> </bean> <!--2、配置redis连接工厂--> <bean id="factory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <!--连接池的配置--> <property name="poolConfig" ref="poolConfig"/> <!--连接主机--> <property name="hostName" value="192.168.65.128"/> <!--端口--> <property name="port" value="6379"/> <!--密码--> <!-- 当出现以下错误时,说明并不需要设置密码 Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR Client sent AUTH, but no password is set --> <!--<property name="password" value="root"/>--> </bean> <!--3、配置redis模板对象--> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <!--配置连接工厂--> <property name="connectionFactory" ref="factory"/> </bean> </beans>
3、注入模板对象
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:application-redis.xml")
public class RedisTest {
 @Autowired
 private RedisTemplate redisTemplate;
 @Test
 public void test(){
  //redisTemplate.opsForValue().set("name","小黑");
  Object name = redisTemplate.opsForValue().get("name");
  System.out.println(name);
  System.out.println("操作完成");
 }
}
注意:使用Spring(SpringBoot)整合redis后,RedisTemplate对象会自带key和value的序列化功能。在redis的客户端操作时,获取的key是被序列化后的key.
思考:为什么Spring要提供一个序列化的功能? 为了开发者方便将对象存入redis中。可在xml中配置自定义的序列化类型。
<!--3、配置redis模板对象--> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <!--配置连接工厂--> <property name="connectionFactory" ref="factory"/> <!--配置String类型的key value序列化方式 当存储的key是String类型时,则vlaue也是String类型,且key和value不被序列化--> <property name="keySerializer" ref="stringRedisSerializer"/> <property name="valueSerializer" ref="stringRedisSerializer"/> </bean> <!--自定义序列化类型--> <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/> <!--默认的jdk序列化--> <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
springboot整合Redis
1、添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2、配置application.yml

3、注入模板对象
@RunWith(SpringRunner.class)
@SpringBootTest
class SpringbootRedisApplicationTests {
 @Autowired
 private RedisTemplate redisTemplate;
 @PostConstruct
 public void init(){
  //配置String类型的key value序列化方式
  redisTemplate.setStringSerializer(new StringRedisSerializer());
  redisTemplate.setValueSerializer(new StringRedisSerializer());
 }
 @Test
 void contextLoads() {
  redisTemplate.opsForValue().set("age",12);
  Object age = redisTemplate.opsForValue().get("age");
  System.out.println(age);
  System.out.println("操作成功");
 }
 //获取几种数据结构的对象
 @Test
 public void getRedisType(){
  //1、操作字符串数据类型
  redisTemplate.opsForValue();
  //2、操作hash的数据类型
  redisTemplate.opsForHash();
  //3、操作List的数据类型
  redisTemplate.opsForList();
  //4、操作Set的数据类型
  redisTemplate.opsForSet();
  //5、操作hSet的数据类型
  redisTemplate.opsForZSet();
  //6、操作基数的数据类型
  redisTemplate.opsForHyperLogLog();
 }
}
注意:不能在yml配置文件中配置自定义序列化,可以在springboot启动类或者测试类中,通过@PostConstruct注解来触发执行方法,从而达到配置自定义序列化的效果。
补充:
1.@PostConstruct说明
被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。
2.@PreDestroy说明
被@PreDestroy修饰的方法会在服务器卸载Servlet的时候运行,并且只会被服务器调用一次,类似于Servlet的destroy()方法。被@PreDestroy修饰的方法会在destroy()方法之后运行,在Servlet被彻底卸载之前。
总结
1、当项目报以下错误:Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR Client sent AUTH, but no password is set
报错的原因:是redis服务没设置密码,而项目配置文件中写了有redis密码
解决方案:
1)把项目配置文件中的密码password设置为空或者不设置。
2)设置redis服务密码
——可通过直接修改redis.conf配置文件中的requirepass属性方式,如果修改不生效,可通过命令方式修改,进入redis的客户端
redis 127.0.0.1:6379> CONFIG SET requirepass “root” OK redis 127.0.0.1:6379> AUTH root Ok
然后重启项目就可以连接本机的redis服务了。

评论(0)