springboot針對redis開發了一套api,將redis常規操作進行了整合.template底層實現時將jedis物件再次進行了封裝.
匯入jar包
<
!--springboot整合redis --
>
org.springframework.boot<
/groupid>
spring-boot-starter-data-redis<
/artifactid>
<
/dependency>
編輯yml配置檔案
server:
port:
8091
servlet:
context-path:
/spring:
#資料來源配置
datasource:
driver-
class
-name: com.mysql.jdbc.driver
url: jdbc:mysql:
//localhost:
3306
/jtdb?servertimezone=gmt%
2b8&useunicode=
true
&characterencoding=utf8&autoreconnect=
true
&allowmultiqueries=
true
username: root
password: root
#配置mvc的視**析器的字首和字尾
mvc:
view:
prefix:
/web-inf/views/
suffix:
.jsp
#spring整合redis
redis:
cluster:
nodes:
192.168
.126
.129
:7000
,192.168
.126
.129
:7001
,192.168
.126
.129
:7002
,192.168
.126
.129
:7003
,192.168
.126
.129
:7004
,192.168
.126
.129
:7005
jedis:
pool:
max-active:
1000 #最大鏈結數量
max-idle:
10 #最大的空閒數量
max-wait:
3 #最大的等待鏈結數量3
min-idle:
3 #最小空閒數量
#mybatis-plush配置
mybatis-plus:
type-aliases-
package
: com.jt.pojo
configuration:
map-underscore-to-camel-case: true
logging:
level:
springboot自動的例項化了2個api物件.
1.redistemplate 可以儲存任意資料型別包括物件型別
2.stringredistemplate 只能操作string資料型別.
@configuration
(proxybeanmethods =
false
)@conditionalonclass
(redisoperations.
class
)@enableconfigurationproperties
(redisproperties.
class
)@import()
public
class
redisautoconfiguration
@bean
@conditionalo****singbean
public stringredistemplate stringredistemplate
(redisconnectionfactory redisconnectionfactory)
throws unknownhostexception
}
@springboottest
public
class
testredistemplate
//具體操作的值是string型別
@test
public
void
teststring()
@test
public
void
testitemdesc()
}
開啟redis快取()
@enablecaching
//讓springboot中的redis註解生效
public
class
springbootrun
}快取註解說明
@restcontroller
//要求返回json
public
class
rediscontroller
/** * 快取更新
* 執行業務進行更新操作,之後將返回值快取到redis中.
* * spel表示式可以動態獲取物件中的屬性值!!!!!
* * key=itemdesc::itemdesc.getitemid()的值
*/("/updateitemdesc"
)@cacheput
(cachenames=
"itemdesc"
,key=
"#itemdesc.getitemid()"
)public itemdesc updateitemdesc
(itemdesc itemdesc)
("/deleteitemdesc"
)@cacheevict
(cachenames=
"itemdesc"
,key=
"#itemid"
)public string deleteitemdesc
(long itemid)
}
RedisTemplate操作Redis常用
redistemplate中定義了對5種資料結構操作 redistemplate.opsforvalue 操作字串 redistemplate.opsforhash 操作hash redistemplate.opsforlist 操作list redistemplate.opsforset 操作se...
redistemplate事務實踐
code public object testredismulti catch interruptedexception e now string operations.opsforvalue get testredismulti system.out.println now object rs o...
RedisTemplate快取用法小記
2 redis的引用包 org.springframework.data spring data redis 1.8.6.release 3 redistemplate裡面有如下幾種常用的形式 1 string型別 redistemplate.opsforvalue 2 list型別 rediste...