利用spring-data-redis連接redis

先開啟服務(wù)器端的redis

1.配置

1)pom.xml

<projectxmlns="http://maven.apache.org/POM/4.0.0"

??? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

??? xsi:schemaLocation="http://maven.apache.org/POM/4.0.0

? http://maven.apache.org/xsd/maven-4.0.0.xsd">

??? <modelVersion>4.0.0</modelVersion>

??? <groupId>com.yang</groupId>

??? <artifactId>jedis01</artifactId>

??? <version>0.0.1-SNAPSHOT</version>


??? <properties>

?????? <spring.version>5.1.5.RELEASE</spring.version>

??? </properties>

??? <dependencies>


?????? <dependency>

?????????? <groupId>org.springframework</groupId>

?????????? <artifactId>spring-context</artifactId>

?????????? <version>${spring.version}</version>

?????? </dependency>


?????? <dependency>

?????????? <groupId>org.springframework</groupId>

?????????? <artifactId>spring-beans</artifactId>

?????????? <version>${spring.version}</version>

?????? </dependency>


?????? <dependency>

?????????? <groupId>org.springframework</groupId>

?????????? <artifactId>spring-webmvc</artifactId>

?????????? <version>${spring.version}</version>

?????? </dependency>


?????? <dependency>

?????????? <groupId>org.springframework</groupId>

?????????? <artifactId>spring-jdbc</artifactId>

?????????? <version>${spring.version}</version>

?????? </dependency>




?????? <!--

? https://mvnrepository.com/artifact/org.springframework/spring-aspects -->

?????? <dependency>

?????????? <groupId>org.springframework</groupId>

?????????? <artifactId>spring-aspects</artifactId>

?????????? <version>${spring.version}</version>

?????? </dependency>



?????? <!--

? https://mvnrepository.com/artifact/org.springframework/spring-jms -->

?????? <dependency>

?????????? <groupId>org.springframework</groupId>

?????????? <artifactId>spring-jms</artifactId>

?????????? <version>${spring.version}</version>

?????? </dependency>


?????? <!--https://mvnrepository.com/artifact/org.springframework/spring-test -->

?????? <dependency>

?????????? <groupId>org.springframework</groupId>

?????????? <artifactId>spring-test</artifactId>

?????????? <version>${spring.version}</version>

?????? </dependency>

?????? <dependency>

?????????? <groupId>org.springframework</groupId>

?????????? <artifactId>spring-tx</artifactId>

?????????? <version>${spring.version}</version>

?????? </dependency>

?????? <!--

? https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis

? -->

?????? <dependency>

?????????? <groupId>org.springframework.data</groupId>

?????????? <artifactId>spring-data-redis</artifactId>

?????????? <version>2.2.3.RELEASE</version>

?????? </dependency>


?????? <!--

? https://mvnrepository.com/artifact/redis.clients/jedis -->

?????? <dependency>

?????????? <groupId>redis.clients</groupId>

?????????? <artifactId>jedis</artifactId>

?????????? <version>3.1.0</version>

?????? </dependency>


?????? <!--

? https://mvnrepository.com/artifact/junit/junit -->

?????? <dependency>

?????????? <groupId>junit</groupId>

?????????? <artifactId>junit</artifactId>

?????????? <version>4.12</version>

?????? </dependency>


??? </dependencies>

</project>

2)redis-config.properties

redis.host=192.168.109.128? #redis服務(wù)器的地址

redis.port=6379

redis.pass=

redis.database=0? #默認(rèn)使用的數(shù)據(jù)庫

redis.maxIdle=300 #最大連接數(shù)

###\u6700\u5927\u8FDE\u63A5\u6570

redis.maxWait=3000? #等待時(shí)間

redis.testOnBorrow=true

3)applicationContext-redis.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

??? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

??? xmlns:mvc="http://www.springframework.org/schema/mvc"

??? xmlns:context="http://www.springframework.org/schema/context"

??? xmlns:p="http://www.springframework.org/schema/p"

??? xmlns:tx="http://www.springframework.org/schema/tx"

??? xmlns:aop="http://www.springframework.org/schema/aop"

??? xsi:schemaLocation="http://www.springframework.org/schema/mvc

? http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd

?????? http://www.springframework.org/schema/beans? http://www.springframework.org/schema/beans/spring-beans.xsd

?????? http://www.springframework.org/schema/context? http://www.springframework.org/schema/context/spring-context-4.3.xsd

?????? http://www.springframework.org/schema/aop? http://www.springframework.org/schema/aop/spring-aop-4.3.xsd

?????? http://www.springframework.org/schema/tx? http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

<!-加載配置文件-à

??? <context:property-placeholderlocation="classpath:property/*.properties"/>


??? <beanid="poolConfig"class="redis.clients.jedis.JedisPoolConfig">

??? <propertyname="maxIdle"value="${redis.maxIdle}"></property>

??? <propertyname="maxWaitMillis"value="${redis.maxWait}"></property>

??? <propertyname="testOnBorrow"value="${redis.testOnBorrow}"></property>

??? </bean>

??? <beanid="JedisConnectionFactory"

??? ?class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"

??? ?p:host-name="${redis.host}"p:port="${redis.port}"p:password="${redis.pass}"

??? ?p:pool-config-ref="poolConfig"/>

??? ?<beanid="redisTemplate"class="org.springframework.data.redis.core.RedisTemplate">

????? <propertyname="connectionFactory"ref="JedisConnectionFactory"/>

???? </bean>

</beans>

2.代碼

1)測試連接

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")

public class TestString {


??? @SuppressWarnings("rawtypes")

??? @Autowired

??? private RedisTemplate redisTemplate;


??? @Test

??? public void test01() {

System.out.println(redisTemplate);

}

}

2)對(duì)String的操作

?

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")

public class TestString {


??? @SuppressWarnings("rawtypes")

??? @Autowired

??? private RedisTemplate redisTemplate;


??? @Test

??? public voidtest01() {? //放值

??? //? redisTemplate.boundValueOps("user").set("zs");;

??? ??? System.out.println(redisTemplate.boundValueOps("user").append("123"));

?????? redisTemplate.delete("user");

??? }


??? @Test

??? public void get() {//取值

?????? System.out.println(redisTemplate.boundValueOps("user").get());

??? }


??? @Test

??? public voiddelete() {? //放值

?????? System.out.println(redisTemplate.delete("user"));

??? }



}

3)對(duì)List的操作

?

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")

public class TestList {


??? @Autowired

??? privateRedisTemplateredisTemplate;


??? @Test

??? public voidtest01() {? //放值

?????? redisTemplate.boundListOps("list001").rightPush("right001");

?????? redisTemplate.boundListOps("list001").rightPush("right002");

?????? redisTemplate.boundListOps("list001").rightPush("right003");

??? }


??? @Test

??? public void get() {

?????? List? lists=(List)redisTemplate.boundListOps("list002").range(0, 20);

?????? System.out.println(lists);

??? }



??? @Test

??? public void delete(){

?????? redisTemplate.delete("list002");

??? }


??? @Test

??? public void leftPut() {

?????? redisTemplate.boundListOps("list002").leftPush("left001");

?????? redisTemplate.boundListOps("list002").leftPush("left002");

?????? redisTemplate.boundListOps("list002").leftPush("left003");

??? }



??? @Test

??? public void removeValue(){

?????? redisTemplate.boundListOps("list002").remove(0, "left001");

??? }


}

4)對(duì)Set的操作

?

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")

public class TestSet {


??? @Autowired

??? privateRedisTemplateredisTemplate;


??? @Test

??? public void add() {

?????? redisTemplate.boundSetOps("set01").add("set001");

?????? redisTemplate.boundSetOps("set01").add("set002");

?????? redisTemplate.boundSetOps("set01").add("set003");

??? }


??? @Test

??? public void get() {

?????? Sets=redisTemplate.boundSetOps("set01").members();

?????? System.out.println(s);

??? }


??? @Test

??? public void remove() {

?????? redisTemplate.boundSetOps("set01").remove("set001");

??? }

??? @Test

??? public void delete() {

?????? redisTemplate.delete("set01");

??? }

}

5)對(duì)hash的操作

?

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")

public class TestMap {


??? @Autowired

??? privateRedisTemplateredisTemplate;


??? @Test

??? public void add() {

?????? Mapm=new HashMap<String,Object>();

?????? m.put("a01", "001");

?????? m.put("a02", "002");

?????? m.put("a03", "003");

?????? m.put("a04", 4);


?????? redisTemplate.boundHashOps("hash01").putAll(m);

??? }


??? @Test

??? public void get() {

?????? Setkeys=redisTemplate.boundHashOps("hash01").keys();

?????? System.out.println(keys);

??? }


??? @Test

??? public void value() {

?????? Listls=redisTemplate.boundHashOps("hash01").values();

?????? System.out.println(ls);

??? }

??? @Test

??? public void getkey_value() {

?????? Strings=(String)redisTemplate.boundHashOps("hash01").get("a03");

?????? System.out.println(s);

??? }


??? @Test

??? public void remove() {

?????? redisTemplate.boundHashOps("hash01").delete("a03","a04");

??? }


}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末巍膘,一起剝皮案震驚了整個(gè)濱河市缺虐,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌落追,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,036評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件偿洁,死亡現(xiàn)場離奇詭異撒汉,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)涕滋,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門睬辐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人宾肺,你說我怎么就攤上這事溯饵。” “怎么了锨用?”我有些...
    開封第一講書人閱讀 164,411評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵丰刊,是天一觀的道長。 經(jīng)常有香客問我黔酥,道長藻三,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,622評(píng)論 1 293
  • 正文 為了忘掉前任跪者,我火速辦了婚禮棵帽,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘渣玲。我一直安慰自己逗概,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,661評(píng)論 6 392
  • 文/花漫 我一把揭開白布忘衍。 她就那樣靜靜地躺著逾苫,像睡著了一般。 火紅的嫁衣襯著肌膚如雪枚钓。 梳的紋絲不亂的頭發(fā)上铅搓,一...
    開封第一講書人閱讀 51,521評(píng)論 1 304
  • 那天,我揣著相機(jī)與錄音搀捷,去河邊找鬼星掰。 笑死,一個(gè)胖子當(dāng)著我的面吹牛嫩舟,可吹牛的內(nèi)容都是我干的氢烘。 我是一名探鬼主播,決...
    沈念sama閱讀 40,288評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼家厌,長吁一口氣:“原來是場噩夢啊……” “哼播玖!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起饭于,我...
    開封第一講書人閱讀 39,200評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤蜀踏,失蹤者是張志新(化名)和其女友劉穎维蒙,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體脓斩,經(jīng)...
    沈念sama閱讀 45,644評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡木西,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,837評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了随静。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片八千。...
    茶點(diǎn)故事閱讀 39,953評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖燎猛,靈堂內(nèi)的尸體忽然破棺而出恋捆,到底是詐尸還是另有隱情,我是刑警寧澤重绷,帶...
    沈念sama閱讀 35,673評(píng)論 5 346
  • 正文 年R本政府宣布沸停,位于F島的核電站,受9級(jí)特大地震影響昭卓,放射性物質(zhì)發(fā)生泄漏愤钾。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,281評(píng)論 3 329
  • 文/蒙蒙 一候醒、第九天 我趴在偏房一處隱蔽的房頂上張望能颁。 院中可真熱鬧,春花似錦倒淫、人聲如沸伙菊。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽镜硕。三九已至,卻和暖如春返干,著一層夾襖步出監(jiān)牢的瞬間兴枯,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評(píng)論 1 269
  • 我被黑心中介騙來泰國打工矩欠, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留念恍,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,119評(píng)論 3 370
  • 正文 我出身青樓晚顷,卻偏偏與公主長得像,于是被迫代替她去往敵國和親疗疟。 傳聞我的和親對(duì)象是個(gè)殘疾皇子该默,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,901評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容