当前位置:   article > 正文

Redis--------java引入redis_java 引入redis

java 引入redis

一、new一个project

用idea软件点击左上角File--》new--》project

二、引入pom依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>org.example</groupId>
  7. <artifactId>redisTest</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <parent>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-parent</artifactId>
  12. <version>2.7.18</version>
  13. <relativePath/>
  14. </parent>
  15. <properties>
  16. <maven.compiler.source>8</maven.compiler.source>
  17. <maven.compiler.target>8</maven.compiler.target>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-data-redis</artifactId>
  28. <version>2.6.7</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-test</artifactId>
  33. <scope>test</scope>
  34. <version>2.6.7</version>
  35. </dependency>
  36. </dependencies>
  37. </project>

三、创建SpringBootApplication

  1. package org.example;
  2. import org.springframework.boot.Banner;
  3. import org.springframework.boot.Banner.Mode;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. /**
  7. * @author zx
  8. * @date 2024/6/3 15:57
  9. */
  10. @SpringBootApplication(scanBasePackages = {"org.example"})
  11. public class application {
  12. public static void main(String[] args) {
  13. SpringApplication springApplication = new SpringApplication(application.class);
  14. // 关闭banner图案(spring 启动时的图案) http://patorjk.com/software/taag/ 在线生成图案
  15. /*springApplication.setBannerMode(Mode.OFF);*/
  16. springApplication.run();
  17. }
  18. }

其中的banner.txt(放在resources目录下就可以)就是spring启动时的图案,可以换成自己想要的,属于覆盖了原先spring启动,大佛(大家应该都见过了)

  1. // _ooOoo_ //
  2. // o8888888o //
  3. // 88" . "88 //
  4. // (| ^_^ |) //
  5. // O\ = /O //
  6. // ____/`---'\____ //
  7. // .' \\| |// `. //
  8. // / \\||| : |||// \ //
  9. // / _||||| -:- |||||- \ //
  10. // | | \\\ - /// | | //
  11. // | \_| ''\---/'' | | //
  12. // \ .-\__ `-` ___/-. / //
  13. // ___`. .' /--.--\ `. . ___ //
  14. // ."" '< `.___\_<|>_/___.' >'"". //
  15. // | | : `- \`.;`\ _ /`;.`/ - ` : | | //
  16. // \ \ `-. \_ __\ /__ _/ .-` / / //
  17. // ========`-.____`-.___\_____/___.-`____.-'======== //
  18. // `=---=' //
  19. // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
  20. // 佛祖保佑 永不宕机 永无BUG //

四、创建application.properties

其中填入配置信息

  1. # 启动端口
  2. server.port=9099
  3. ##redis settings
  4. ##本地连接
  5. spring.redis.host=127.0.0.1
  6. ##使用默认端口
  7. spring.redis.port=6379
  8. spring.redis.database=15
  9. ##未设密码,默认pwd为空
  10. spring.redis.password=

这里的database就是redis用的16个库中14

五、搞一个test目录,用来测试

也可以直接用idea自带的ALT+Insert添加一个对应的测试用例,就会在同级目录下生成这个类

  1. package org.example.util;
  2. import org.junit.jupiter.api.Test;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.boot.test.context.SpringBootTest;
  5. import org.springframework.data.redis.core.RedisTemplate;
  6. /**
  7. * @author zx
  8. * @date 2024/6/3 16:36
  9. */
  10. @SpringBootTest
  11. class RedisUtilTest {
  12. @Autowired
  13. private RedisTemplate redisTemplate;
  14. @Test
  15. public void test01() {
  16. // 第一步 存储String 字符串
  17. redisTemplate.opsForValue().set("ki-test", "chinese");
  18. Object o = redisTemplate.opsForValue().get("ki-test");
  19. System.out.println(o);
  20. }
  21. }

点击执行之后能够看到redis中仓库14已经有数据,但是乱码,需要配置

六、格式化字符

此处搞一个config,这里也就应用了spring重写bean的几种方式之一,暴力覆盖

  1. package org.example.configuration;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.data.redis.connection.RedisConnectionFactory;
  5. import org.springframework.data.redis.core.RedisTemplate;
  6. import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
  7. import org.springframework.data.redis.serializer.StringRedisSerializer;
  8. /**
  9. * @author zx
  10. * @date 2024/6/3 16:44
  11. */
  12. @Configuration
  13. public class RedisConfig {
  14. @Bean
  15. public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
  16. RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
  17. //key 采用String的序列化的方式
  18. redisTemplate.setKeySerializer(new StringRedisSerializer());
  19. //value的序列化采用jackson
  20. redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
  21. //hash的 key也采用String序列化的方式
  22. redisTemplate.setHashKeySerializer(new StringRedisSerializer());
  23. //hash的value也采用jackson
  24. redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
  25. //注入连接工厂
  26. redisTemplate.setConnectionFactory(factory);
  27. redisTemplate.afterPropertiesSet();
  28. return redisTemplate;
  29. }
  30. }

这一步骤之后,再跑test,结果就是ok的

参考了文章spring-boot启动时酷炫效果banner.txt

                  Java中引入redisc​​​​​​​​​​​​

 好玩的banner.txt可以参考这个SpringBoot炫酷的banner.txt​​​​​​​

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/948884
推荐阅读
相关标签
  

闽ICP备14008679号