spring boot連接redis


1,redis配置


# REDIS (RedisProperties)

# Redis數(shù)據(jù)庫(kù)索引(默認(rèn)為0)

spring.redis.database=0

# Redis服務(wù)器地址

spring.redis.host=localhost

# Redis服務(wù)器連接端口

spring.redis.port=6379

# Redis服務(wù)器連接密碼(默認(rèn)為空

# 連接池最大連接數(shù)(使用負(fù)值表示沒有限制)

spring.redis.pool.max-active=8

# 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒有限制)

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

# 連接池中的最大空閑連接

spring.redis.pool.max-idle=8

# 連接池中的最小空閑連接

spring.redis.pool.min-idle=0

# 連接超時(shí)時(shí)間(毫秒)

spring.redis.timeout=250

2,meven配置



<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-redis</artifactId>

</dependency>


<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-redis</artifactId>

</dependency>

<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

</dependency>

3,注解添加


package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cache.annotation.Cacheable;

import org.springframework.stereotype.Service;

@Service

public class AccountService {

@Autowired

? ? private AccountRepositoryaccountrepository;

@Cacheable(value="account", key ="#username")

public Account find(String username) {

System.out.println("put username in redis catch");

return? accountrepository.findFirstByUsername(username);

}

public Account save (Account account) {

accountrepository.save(account);

return account;

}

}

4,繼承serilizeable


package com.example.demo;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.Table;

import java.io.Serializable;

@Table(name="accout")

@Entity

public class Accountimplements Serializable {

private Stringusername;

private Stringpassword;

@Id

? ? @Column(name="USERNAME")

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

@Column(name="PASSWORD")

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public String toString()

{

return "this is person:"+"name:"+this.username+"——password:"+this.password;

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容