springboot nosql

Posted jony-it

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot nosql相关的知识,希望对你有一定的参考价值。

1.mongodb(对象):

docker :

拉取docker 镜像

docker pull mongo

运行

docker run -d -p 27017:27017 mongo

 

下载mongodb 客户端

robomongo https://robomongo.org/download

 

项目导入依赖

implementation ‘org.springframework.boot:spring-boot-starter-data-mongodb‘

 

repo

package com.duoke.demo.repo;

import com.duoke.demo.pojo.Animal;
import org.springframework.data.mongodb.repository.MongoRepository;

public interface AnimalRepo extends MongoRepository<Animal,String> 
    Animal findByName(String name);

 

#db配置
spring.data.mongodb.host=192.168.0.120
spring.data.mongodb.port=27107
spring.data.mongodb.uri=mongodb://192.168.0.120/test

controller

    @RequestMapping("/save")
    public Animal dosave()
        Animal animal = new Animal("wxx",18);
        List<Location> locations = new LinkedList<Location>();

        Location l1 = new Location("徐州","2009");
        Location l2 = new Location("南京","2020");

        locations.add(l1);
        locations.add(l2);
        animal.setLocations(locations);
        return repo.save(animal);
    

 

2.redis(键值对):

docker 拉取redis

运行

docker run -d -p 6379:6379 --name redis redis

下载RedisClient

github.com/caoxinyu/RedisClient

运行双击jar-add

redis 默认初始化没有密码

加载依赖

implementation ‘org.springframework.boot:spring-boot-starter-data-redis‘

实体

package com.duoke.demo.pojo;

import java.io.Serializable;

public class Fish implements Serializable 
    private String id;
    private String name;
    private Integer age;

    public Fish() 
    

    public Fish(String id, String name, Integer age) 
        this.id = id;
        this.name = name;
        this.age = age;
    

    public String getId() 
        return id;
    

    public void setId(String id) 
        this.id = id;
    

    public String getName() 
        return name;
    

    public void setName(String name) 
        this.name = name;
    

    public Integer getAge() 
        return age;
    

    public void setAge(Integer age) 
        this.age = age;
    

repo(template 操作redis 数据库)

package com.duoke.demo.repo;

import com.duoke.demo.bean.Person;
import com.duoke.demo.pojo.Fish;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

@Repository
public class FishRepo 
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    RedisTemplate<Object,Object> redisTemplate;

    @Resource(name = "stringRedisTemplate")
    ValueOperations<String,String> valueOperations;

    @Resource(name = "redisTemplate")
    ValueOperations<Object,Object> objectValueOperations;
    public void setStringRedisTemplateDemo()
        valueOperations.set("xx","yy");
    

    public void save(Fish fish)
        objectValueOperations.set(fish.getId(),fish);
    

    public String getStr()
       return valueOperations.get("xx");
    

    public Fish getFish()
        return (Fish) objectValueOperations.get("1");
    

 

controller

package com.duoke.demo.controller;

import com.duoke.demo.pojo.Fish;
import com.duoke.demo.repo.FishRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/fish")
public class FishController 
    @Autowired
    FishRepo repo;

    @RequestMapping("/set")
    public void set()
        Fish fish = new Fish("1","wxx",30);
        repo.save(fish);
        repo.setStringRedisTemplateDemo();
    

    @RequestMapping("/getStr")
    public String getStr()
        return repo.getStr();
    

    @RequestMapping("/getFish")
    public Fish getFish()
        return repo.getFish();
    

 

使用jackon 存储键值,重写redistemplate bean

@Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory factory) 
        RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
        template.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    

 

以上是关于springboot nosql的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot入门到精通-SpringBoot自定义starter

SpringBoot.06.SpringBoot日志管理

SpringBoot.06.SpringBoot日志管理

最全面的SpringBoot教程——SpringBoot概述

SpringBoot入门到精通-SpringBoot集成SSM开发项目

如何把springboot插件删除干净