jedis就像jdbc一樣,用于兩個端直接的連接。
1.創建Spring項目
這里不過多贅述...
2.導入連接工具jedis
在pom文件中導入jedis的依賴。
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>4.3.1</version>//版本號根據需要選擇</dependency>
3.創建jedis對象
//redis所在主機的IP地址和監聽的端口
Jedis jedis = new Jedis(IP地址, 端口號);
//指定訪問服務器的密碼
jedis.auth("132132qa..");
4.測試是否連通
jedis.ping()
System.out.println(jedis.ping());//若輸出為PONG表示連通
之后就可以通過jedis進行操作啦——
Set<String> set= jedis.keys("*"); jedis.set("name", "qyyb"); Map<String, String> map =new HashMap<>(); jedis.hset("tengjie",map);
....................
Jedis和Lettuce區別
Jedis客戶端連接時,都需要創建實例,造成很大開銷。線程之間也不安全,一個線程修改會對別的線程影響。
Lettuce底層使用Netty,保證只創建一個Lettuce連接,所有線程共享,故也不會存在數據修改對別的線程影響。
RedisTemplate使用
pom配置
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId> </dependency> <dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>3.0.0</version> </dependency> <dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>3.0.0</version> </dependency>
properties文件配置
SwaggerConfig配置
?注入bean并操作
顯示中文:啟動redis加上--raw
感謝觀看——