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 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 locations = new LinkedList();

        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 redisTemplate;

    @Resource(name = "stringRedisTemplate")
    ValueOperations valueOperations;

    @Resource(name = "redisTemplate")
    ValueOperations 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 redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate template = new RedisTemplate();
        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;
    }

 

转载于:https://www.cnblogs.com/jony-it/p/11436448.html

你可能感兴趣的:(springboot nosql)