java j2EE 对 reids的使用,包含事物

xiaoxiao2025-09-03  224

1、redis基础

http://www.runoob.com/redis/redis-intro.html

2、主要结合springboot学习了,采用的test类学习

package com.zh; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.transaction.annotation.EnableTransactionManagement; @MapperScan("com.zh.dao") @SpringBootApplication @EnableTransactionManagement public class SpringBootDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringBootDemoApplication.class, args); } }

主要是开启了支持事物  @EnableTransactionManagement

jar依赖加入

<!-- redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>

 

配置文件加入redis:

# redis spring.redis.host=127.0.0.1 spring.redis.port=6379 #spring.redis.password= #spring.redis.cluster.nodes=127.0.0.1:6379,127.0.0.1:6378,127.0.0.1:6377 spring.redis.jedis.pool.max-active=8 spring.redis.timeout=10000

 

3、测试类使用redis

package com.zh.redisStudy; import com.zh.SpringBootDemoApplication; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.web.WebAppConfiguration; import javax.annotation.Resource; import java.util.*; /** * @author zhangH * @date 2018/10/25 * 学习java操作redis的数据类型,多类型的操作 */ @RunWith(SpringRunner.class) @SpringBootTest(classes = SpringBootDemoApplication.class) @WebAppConfiguration public class RedisStudyTest { /** * spring 结合的 redis 注入 */ @Resource private RedisTemplate redisTemplate; /** * redis 数据类型 * redisTemplate.opsForValue(); * 操作字符串 * string */ @Test public void operationStringTest() { try { redisTemplate.opsForValue().set("stringOp", "operationVal"); redisTemplate.opsForValue().get("stringOp"); } catch (Exception e) { e.printStackTrace(); } } /** * redis 数据类型 * redisTemplate.opsForList(); * 操作list * list */ @Test public void operationListTest() { try { List<String> list = new ArrayList<>(); for (int i = 0; i < 20; i++) { if (i % 2 == 0) { redisTemplate.opsForList().leftPush("operationList", "operation" + i); } else { redisTemplate.opsForList().rightPush("operationList", "operation" + i); } list.add("operationAll" + i + i); } System.out.println("单值插入"); System.out.println(redisTemplate.opsForList().size("operationList")); System.out.println(redisTemplate.opsForList().range("operationList", 1, 10).toString()); System.out.println(redisTemplate.opsForList().range("operationList", 1, -1).toString()); System.out.println("整个list插入"); redisTemplate.opsForList().leftPushAll("operationAll", list); System.out.println(redisTemplate.opsForList().size("operationAll")); System.out.println(redisTemplate.opsForList().range("operationAll", 1, 10).toString()); System.out.println(redisTemplate.opsForList().range("operationAll", 1, -1).toString()); } catch (Exception e) { e.printStackTrace(); } } /** * redis 数据类型 * redisTemplate.opsForHash(); * 操作hash * list */ @Test public void operationHashTest() { try { for (int i = 0; i < 5; i++) { redisTemplate.opsForHash().put("map", "mapOne" + i, "mapOne" + i + i); } Map<String, String> map = new HashMap<>(); for (int i = 5; i < 10; i++) { map.put("map" + i, "map" + i + i); } redisTemplate.opsForHash().putAll("map", map); System.out.println(redisTemplate.opsForHash().size("map")); System.out.println(redisTemplate.opsForHash().keys("map")); System.out.println(redisTemplate.opsForHash().values("map")); for (Object o : redisTemplate.opsForHash().keys("map")) { System.out.println(o + "=" + redisTemplate.opsForHash().get("map", o)); } } catch (Exception e) { e.printStackTrace(); } } /** * redis 数据类型 * redisTemplate.opsForSet(); * 操作set --插入顺序 * list */ @Test public void operationSetTest() { try { Set set = new HashSet(); for (int i = 0; i < 20; i++) { redisTemplate.opsForSet().add("operationSet", "operation" + i); set.add("operationAll" + i + i); } System.out.println(redisTemplate.opsForSet().size("operationSet")); System.out.println(redisTemplate.opsForSet().members("operationSet")); } catch (Exception e) { e.printStackTrace(); } } /** * redis 数据类型 * redisTemplate.opsForZSet(); * 操作有序set --指定顺序 * list */ @Test public void operationZSetTest() { try { int a = 20; for (int i = 0; i < 20; i++) { redisTemplate.opsForZSet().add("operationZSetNew1", "val" + i, a--); } System.out.println(redisTemplate.opsForZSet().size("operationZSetNew1")); System.out.println(redisTemplate.opsForZSet().range("operationZSetNew1", 1, -1)); } catch (Exception e) { e.printStackTrace(); } } }

 

 

4、redis的事物采用

 

package com.zh.redisStudy; import com.zh.SpringBootDemoApplication; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.web.WebAppConfiguration; import javax.annotation.Resource; import java.beans.Transient; /** * @author zhangH * @date 2018/10/25 * redis 事物 */ @RunWith(SpringRunner.class) @SpringBootTest(classes = SpringBootDemoApplication.class) @WebAppConfiguration public class RedisThingsTest { /** * spring 结合的 redis 注入 */ @Resource private RedisTemplate redisTemplate; @Test public void things1Test() { try { if (transactionDoing()) { System.out.println("----------" + redisTemplate.opsForValue().get("TransactionStart3")); System.out.println("==========" + redisTemplate.opsForValue().get("TransactionEnd3")); } } catch (Exception e) { System.out.println("this operation is fail"); e.printStackTrace(); } System.out.println("----------" + redisTemplate.opsForValue().get("TransactionStart3")); System.out.println("==========" + redisTemplate.opsForValue().get("TransactionEnd3")); } @Transient public boolean transactionDoing() { redisTemplate.multi(); redisTemplate.setEnableTransactionSupport(true); redisTemplate.multi(); redisTemplate.opsForValue().set("TransactionStart3", "TransactionStart"); int make_error = 1 / 0; redisTemplate.opsForValue().set("TransactionEnd3", "TransactionEnd"); redisTemplate.exec(); return true; } }

目前就这样了,有啥问题得欢迎提出,我也加深学习下,

go go go

 

转载请注明原文地址: https://www.6miu.com/read-5035683.html

最新回复(0)