本小札记录所用到的资料:
链接:https://pan.baidu.com/s/1sdnkpWwxoZkozp7omu3X8g
提取码:i2p6
部分docker命令
docker启动
systemctl start docker
查看镜像 docker images
查看容器快照 docker ps -a
docker search XX
docker pull XX
docker start containerID
在docker中运行Mysql:
docker run ‐di ‐‐name=tensquare_mysql ‐p 3306:3306 ‐e
MYSQL_ROOT_PASSWORD=root centos/mysql‐57‐centos7
SpringData Redis的使用
1.依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.在docker中运行Redis:
docker run -di --name=tensquare_redis -p 6379:6379 redis
3. yml 配置
spring:
redis:
host: 192.168.74.130
port: 6379
database: 2
4.java操作
@Autowired
private RedisTemplate redisTemplate;
//设置值和过期时长
redisTemplate.opsForValue().set("article_"+id,article,20, TimeUnit.SECONDS);
5.其他操作
redisTemplate.opsForValue().set("test", "100",60*10,TimeUnit.SECONDS);//向redis里存入数据和设置缓存时间
redisTemplate.opsForValue().get("test")//根据key获取缓存中的val
redisTemplate.boundValueOps("test").increment(-1);//val做-1操作
redisTemplate.boundValueOps("test").increment(1);//val +1
redisTemplate.getExpire("test")//根据key获取过期时间
redisTemplate.getExpire("test",TimeUnit.SECONDS)//根据key获取过期时间并换算成指定单位
redisTemplate.delete("test");//根据key删除缓存
redisTemplate.hasKey("546545");//检查key是否存在,返回boolean值
redisTemplate.expire("red_123",1000 , TimeUnit.MILLISECONDS);//设置过期时间
redisTemplate.opsForSet().add("red_123", "1","2","3");//向指定key中存放set集合
redisTemplate.opsForSet().isMember("red_123", "1")//根据key查看集合中是否存在指定数据
redisTemplate.opsForSet().members("red_123");//根据key获取set集合
SpringCache 的使用
1.不需引用依赖,只需在启动类标注 @EnableCaching
2.在需要使用缓存的方法上注释 @Cacheable(value = “表明当前缓存片属于哪个目录,属于全局的唯一标识”,key=“key才是真正的索引键”)
e.g
/** 默认把方法的返回值放入缓存
* 参数一:value 别名: 用于清除缓存的时候来使用
参数二:key,放入缓存的key
Map集合 缓存 -> gathering
key: id值
value: Gathering对象
*/
@Cacheable(value = "gathering",key = "#id")
public Gathering findById(String id) {
return gatheringDao.findById(id).get();
}
// 删除
@CacheEvict(value = "gathering",key = "#id")
public void deleteById(String id) {
gatheringDao.deleteById(id);
}
MongoDB
mysql/oracle 关系型
redis nosql 非关系型 – 表之间无关系
文档型数据库 MongoDB:
默认端口:27017
使用特点:
- 数据量大
- 写入操作频繁
- 价值较低
文件存储格式 BSON(一种json的扩展)
cmd命令:
在d盘创建文件夹作为MongoDB的数据存放处
md mongodb_data
mongod -dbpath=d:\mongodb_data
再另起cmd:
mongo + ip 默认是本地
exit 退出
SpringDataMongoDB的使用
1.依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
2.在docker中运行MongoDB:
docker run -di --name=tensquare_mongo -p 27017:27017 mongo
3. yml 配置
spring:
application:
name: tensquare-spit
data:
mongodb:
host: 192.168.66.138
port: 27017
database: spitdb
redis:
host: 192.168.66.138
4.java的crud操作
/**
* MongoTemplate的作用:实现构建不同命令操作mongoDB。
* db.spit.update({},{})
* db.spit.insert()
* db.spit.find({xxx})
*
*/
@Autowired
private MongoTemplate mongoTemplate;
//1.构建查询条件
//{"_id":"1064806492508647424"}
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(spitid));
//2.构建更新对象
//{$inc:{"thumbup":NumberInt(1)}
Update update = new Update();
update.inc("thumbup",1);
//3.使用MongoTemplae调到方法
//db.spit.update( {"_id":"1064806492508647424"},{$inc:{"thumbup":NumberInt(1)}} )
mongoTemplate.updateFirst(query,update,"spit");