美文网首页
Spring-Boot之Jedis-Client插件使用

Spring-Boot之Jedis-Client插件使用

作者: townof1997 | 来源:发表于2022-09-11 13:43 被阅读0次
1,配置maven插件,在pom.xml文件中引用Jedis-Client插件,
 <dependency>
       <groupId>redis.clients</groupId>
       <artifactId>jedis</artifactId>
       <version>2.9.0</version>
</dependency>

2,配置xml文件


Jedis配置文件图.png
以applicationContext.xml命名为例,在applicationContext.xml中配置如下代码:
    <bean id="jedisPollConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大空闲连接数-->
        <property name="maxIdle" value="1"/>
        <!-- 最大连接数-->
        <property name="maxTotal" value="5"/>
        <!-- 连接耗尽时最大阻塞, false 报异常,true 阻塞直到超时,默认为true-->
        <property name="blockWhenExhausted" value="true"/>
        <!--获取连接时最大等待毫秒数-->
        <property name="maxWaitMillis" value="30000" />
        <!--在获取连接的时候检查有效性-->
        <property name="testOnBorrow" value="true" />
    </bean>
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="close">
        <constructor-arg name="poolConfig" ref="jedisPollConfig"/>
        <constructor-arg name="host" value="127.0.0.1"></constructor-arg>
        <constructor-arg name="port" value="6379"></constructor-arg>
        <constructor-arg name="timeout" value="30000" />
        <!-- 设定密码时打开-->
        <!--<constructor-arg name="password" value="" />-->
    </bean>

3,相关代码如下:
注意这里加载的xml文件是applicationContext.xml

public class jedistest {
    @Test
    public void test() {
        ClassPathXmlApplicationContext context 
= new ClassPathXmlApplicationContext("applicationContext.xml");
        JedisPool jedisPool = context.getBean(JedisPool.class);
        Jedis jedis = jedisPool.getResource();
//        jedis.auth("");
        System.out.println(jedis.get("username"));
        jedis.close();
    }

4,也可以在代码中直接用Jedis或者JedisPool连接Redis数据库

public class jedistest {
    public static void main(String[] args) {
//        Jedis j = new Jedis("127.0.0.1", 6379);
////        j.auth("");//在D:\software\Redis-x64-3.2.100\redis.windows.conf的第443行修改requirepass foobared,打开注释foobared即为密码
//        j.select(0);//默认就是0号库
//        j.set("username", "张三");
//        System.out.println(j.get("username"));
//        j.close();
        JedisPool jedisPool = new JedisPool("127.0.0.1", 6379);
        Jedis jedisResource = jedisPool.getResource();
        //jedisResource.auth("");//在D:\software\Redis-x64-3.2.100\redis.windows.conf的第443行修改requirepass foobared,打开注释foobared即为密码
        jedisResource.select(0);//默认就是0号库
        jedisResource.set("username", "张三");
        System.out.println(jedisResource.get("username"));
        jedisResource.close();
        //JedisPool放在配置文件中,整合对象,Spring整合Jedis, 放在spring-redis.xml文件中
//        <beans><beans>
    }
}

5,Jedis也可以使用Spring-Boot注解来连接Jedis
a,引入maven仓库包如下:

        <!--redis客户端(连接及连接池) 或者2.7.3版本-->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        <!-- redis基于注解的导入-->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.6.0.RELEASE</version>
        </dependency>

b,applicationContext.xml配置如下:

    <!--开启基于redis注解的缓存支持标签-->
    <cache:annotation-driven></cache:annotation-driven>
    <bean id="jedisPollConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大空闲连接数-->
        <property name="maxIdle" value="1"/>
        <!-- 最大连接数-->
        <property name="maxTotal" value="5"/>
        <!-- 连接耗尽时最大阻塞, false 报异常,true 阻塞直到超时,默认为true-->
        <property name="blockWhenExhausted" value="true"/>
        <!--获取连接时最大等待毫秒数-->
        <property name="maxWaitMillis" value="30000" />
        <!--在获取连接的时候检查有效性-->
        <property name="testOnBorrow" value="true" />
    </bean>
    <!--<bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="close">
        <constructor-arg name="poolConfig" ref="jedisPollConfig"/>
        <constructor-arg name="host" value="127.0.0.1"></constructor-arg>
        <constructor-arg name="port" value="6379"></constructor-arg>
        <constructor-arg name="timeout" value="30000" />
        <!-- 设定密码时打开-->
        <!--<constructor-arg name="password" value="" />-->
    <!--</bean>-->
    <!--配置JedisConnectionFactory 用来代替JedisPool-->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="127.0.0.1"/>
        <property name="port" value="6379"/>
        <!--<property name="password" value=""/>-->
        <property name="database" value="0"/><!--0~15-->
        <property name="poolConfig" ref="jedisPollConfig"/>
    </bean>
    <!--配置RedisTemplate-->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>
    </bean>
    <!---->
    <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
        <constructor-arg index="0" ref="redisTemplate" />
        <property name="defaultExpiration" value="3000"/>
    </bean>

同时配置bean.xml

<import resource="applicationContext.xml"/>

如图所示:


Jedis的注解配置.png

相关文章

网友评论

      本文标题:Spring-Boot之Jedis-Client插件使用

      本文链接:https://www.haomeiwen.com/subject/yknxortx.html