【Spring Boot 12,mysql索引面试题


好了,我们现在配置好了连接池的相关属性,那么具体使用哪种工厂实现呢?在Spring Data Redis中有四种可供我们选择的工厂模型,它们分别是:

  • JredisConnectionFactory

  • JedisConnectionFactory

  • LettuceConnectionFactory

  • SrpConnectionFactory

我们这里就简单配置成JedisConnectionFactory:

3、配置RedisTemplate


普通的连接根本没有办法直接将对象直接存入 Redis 内存中,我们需要替代的方案:将对象序列化(可以简单的理解为继承Serializable接口)。我们可以把对象序列化之后存入Redis缓存中,然后在取出的时候又通过转换器,将序列化之后的对象反序列化回对象,这样就完成了我们的要求:

RedisTemplate可以帮助我们完成这份工作,它会找到对应的序列化器去转换Redis的键值:

<bean id=“redisTemplate”

class=“org.springframework.data.redis.core.RedisTemplate”

p:connection-factory-ref=“connectionFactory”/>

4、测试


首先编写好支持我们测试的POJO类:

/**

  • @author: 素小暖

  • @create: 2020-2-12

*/

public class Student implements Serializable{

private String name;

private int age;

/**

  • 给该类一个服务类用于测试

*/

public void service() {

System.out.println(“学生名字为:” + name);

System.out.println(“学生年龄为:” + age);

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

}

然后编写测试类:

@Test

public void test() {

ApplicationContext context =

new ClassPathXmlApplicationContext(“applicationContext.xml”);

RedisTemplate redisTemplate = context.getBean(RedisTemplate.class);

Student student = new Student();

student.setName(“我没有三颗心脏”);

student.setAge(21);

redisTemplate.opsForValue().set(“student_1”, student);

Student student1 = (Student) redisTemplate.opsForValue().get(“student_1”);

student1.service();

}

运行结果:

六、springboot中使用Redis

=====================

1、在springboot中添加Redis依赖


org.springframework.boot

spring-boot-starter-data-redis

2、添加配置文件application.peoperties


REDIS (RedisProperties)

Redis数据库索引(默认为0)

spring.redis.database=0

Redis服务器地址

spring.redis.host=localhost

Redis服务器连接端口

spring.redis.port=6379

Redis服务器连接密码(默认为空)

spring.redis.password=

连接池最大连接数(使用负值表示没有限制)

spring.redis.pool.max-active=8

连接池最大阻塞等待时间(使用负值表示没有限制)

spring.redis.pool.max-wait=-1

连接池中的最大空闲连接

spring.redis.pool.max-idle=8

连接池中的最小空闲连接

spring.redis.pool.min-idle=0

连接超时时间(毫秒)

spring.redis.timeout=0

3、测试访问


@RunWith(SpringJUnit4ClassRunner.class)

@SpringBootTest()

public class ApplicationTests {

@Autowired

private StringRedisTemplate stringRedisTemplate;

@Test

public void test() throws Exception {

// 保存字符串

stringRedisTemplate.opsForValue().set(“aaa”, “111”);

Assert.assertEquals(“111”, stringRedisTemplate.opsForValue().get(“aaa”));

}

}

通过上面这段极为简单的测试案例演示了如何通过自动配置的StringRedisTemplate对象进行Redis的读写操作,该对象从命名中就可注意到支持的是String类型。原本是RedisTemplate<K, V>接口,StringRedisTemplate就相当于RedisTemplate<String, String>的实现。

4、存储对象


这一步跟上面使用Spring一样,只需要将POJO类实现Serializable接口就可以了,我这里就贴一下测试代码:

@RunWith(SpringJUnit4ClassRunner.class)

@SpringBootTest()

public class ApplicationTests {

@Autowired

private RedisTemplate redisTemplate;

@Test

public void test() throws Exception {

小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
img

技术停滞不前!**

因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-oQsLGBfx-1710850552206)]
[外链图片转存中…(img-6aEUvXco-1710850552206)]
[外链图片转存中…(img-vr0EneDd-1710850552207)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
[外链图片转存中…(img-DDyTVrhw-1710850552207)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值