Spring Boot (8) 整合NoSQL

Spring Boot (8) 整合NoSQL

Scroll Down

Spring Boot 整合 NoSQL

整合 Redis

Redis 是一个使用C语言编写的基于内存的NoSQL数据库,是目前流行的键值对存储数据库.

引入依赖

        <!-- NoSQL 排除Lettuce 引入Jedis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.lettuce</groupId>
                    <artifactId>lettuce-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

yml 配置

  # redis 配置
  redis:
    database: 0
    host:xxx
    port: 6379
    password: xxx

测试案例

Test.java

@Data
@Entity
@Table(name = "test")
public class Test implements Serializable {
    /**
     * 测试
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

}

TestController.java

/**
 * 测试控制器
 *
 * @Auther: MaWenyi
 * @Date: 2019/6/29
 * @Description: work.idler.dtai.controller
 * @version: 1.0
 */
@RestController
public class TestController {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @GetMapping("/test1")
    public void test1() {
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        operations.set("name", "iscolt");
        String name = operations.get("name");
        System.out.println(name);
        ValueOperations operations1 = redisTemplate.opsForValue();
        Test test = new Test();
        test.setName("abc");
        test.setId(1L);
        operations1.set("abc", test);
        Test test1 = (Test) operations1.get("abc");
        System.out.println(test1);
    }
}

Redis更多参考官网

整合 MongoDB

MongoDB 是一种面向文档的数据管理系统, 它是一个介于关系型数据库和非关系型数据库之间的产品.

未完待续...

支付宝 微信