1.mongodb(對象):java
docker :git
拉取docker 鏡像github
docker pull mongoweb
運行redis
docker run -d -p 27017:27017 mongospring
下載mongodb 客戶端mongodb
robomongo https://robomongo.org/downloaddocker
項目導入依賴數據庫
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
repoapp
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; }