码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Springboot项目连接Redis(jedis)


    1.添加依赖:

        <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4

    2.修改配置文件
    将application.properties改为yml并添加如下配置:

    spring:
      redis:
        database: 0
        host: 192.168.145.128
        password: 123456
        port: 6379
        jedis:  #连接池相关配置
          pool:
            max-active: 8  #最大连接数
            max-idle: 8     #最大空闲数
            min-idle: 1    #最小空闲数
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    创建jedisConfig类:

    package com.example.moduledemo01.config;
    @Configuration
    @ConfigurationProperties(prefix = "spring.redis")
    public class jedisConfig {
        private int database;
        private String host;
        private String password;
        private int port;
    
        /*
        * 1.jedis连接池配置(得到JDBC连接池)
        *
        * */
        @Bean
    public JedisPool jedisPool(){
        /*
        * 连接池
        * */
        JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(8);
        jedisPoolConfig.setMaxIdle(8);
        jedisPoolConfig.setMinIdle(1);
        JedisPool jedisPool=new JedisPool(jedisPoolConfig,host,port,2000,password);
    
        return jedisPool;
    }
    
        public int getDatabase() {
            return database;
        }
    
        public void setDatabase(int database) {
            this.database = database;
        }
    
        public String getHost() {
            return host;
        }
    
        public void setHost(String host) {
            this.host = host;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public int getPort() {
            return port;
        }
    
        public void setPort(int port) {
            this.port = port;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    测试:

    package com.example.moduledemo01;
    @SpringBootTest
    class Moduledemo01ApplicationTests {
        @Autowired
        private JedisPool jedisPool;
        @Test
        void contextLoads() {
            //从连接池中得到链接对象
            Jedis jedis = jedisPool.getResource();
            jedis.set("java111","springboot");
            System.out.println(jedis.get("java111"));
            jedis.close();
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
  • 相关阅读:
    Oracle/PLSQL: Sign Function
    微信小程序|步骤条
    消息队列-RabbitMQ:延迟队列、rabbitmq 插件方式实现延迟队列、整合SpringBoot
    APP逆向案例之(二)对加固APP进行分析和破解
    26.Python3面向对象编程|菜鸟教程
    你不能错过的【Python爬虫】测试3(爬取所有内容 + 完整源代码 + 架构 + 结果)
    【Linux】Linux环境基础开发工具使用—— vim | gcc & g++ | make & makefile | 进度条 | git
    跨时钟域问题(三)异步FIFO的Verilog实现(格雷码)
    基于springboot小型车队管理系统毕业设计源码061709
    Java配置41-搭建Kafka服务器
  • 原文地址:https://blog.csdn.net/qq_56892136/article/details/126185230
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号