先開啟服務(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");
??? }
}