NoSQL与Redis五次作业回顾
文章目录
-
- 1. 作业1
- 2. 作业2
- 3. 作业3
- 4. 作业4
- 5. 作业5
1. 作业1
要求:
在 VM 上安装 CentOS Linux 系统,并在 Linux 上通过 yum 安装 C++编译器,对 Redis 进行编译安装。
观察 Redis 目录结构,使用 redis-server 启动服务器,分别使用前端模式和后端模式启动
把运行的命令和服务器状态、日志等信息截图,并整理到 word 中,提交 word 文件。
一、运行的命令:
[souranker@localhost ~]$ su root
[root@localhost souranker]# cd root
[root@localhost souranker]# tar -zxvf redis-3.0.7.tar.gz
[root@localhost souranker]# cd redis-3.0.7
[root@localhost redis-3.0.7]# yum install gcc-c++ -y
[root@localhost redis-3.0.7]# make
[root@localhost redis-3.0.7]# make install
[root@localhost redis-3.0.7]# cd /usr/local/bin
[root@localhost redis-3.0.7]# redis-server
[root@localhost redis-3.0.7]# cp redis.conf /usr/local/bin
[root@localhost redis-3.0.7]# cd /usr/local/bin
[root@localhost redis-3.0.7]# vim redis.conf
[root@localhost redis-3.0.7]# redis-server ./redis.conf
[root@localhost redis-3.0.7]# ps -ef | grep redis
二、信息截图:
2. 作业2
要求:
- 创建多个 redis.conf 文件,并对端口号进行修改,启动多个 redis 服务器,使用 ps 观察服务器进程。
- 在 redis 中实现以下操作:
1)对现有仓库的所有数据进行删除
2)切换到编号为 2 的数据库
3)新增键值对分别存储学号,姓名,年龄,性别
4)切换到编号为3得数据库,使用hash方式存储student对象,保存学号,姓名,年龄,性别信息
5)切换到编号为4得数据库,使用list方式存储学号,姓名,年龄,性别信息
6)分别在234数据库中添加email数据,并删除性别信息 - 把运行的命令和服务器状态、日志等信息截图,并整理到 word 中,提交 word 文件。
信息截图:
3. 作业3
在 java中实现以下操作:
1.String类型
1)对现有仓库的所有数据进行删除
2)切换到编号为 0 的数据库
3)新增键值对分别存储学号,姓名,年龄,性别
2.Hash类型
1)切换到编号为1得数据库,使用hash方式存储student对象,保存学号,姓名,年龄,性别信息
2)控制台分别单独打印字段名和字段值
3)删除性别信息
3.List类型
1)切换到编号为2的数据库,使用list方式存储学号,姓名,年龄,性别信息,并在控制台打印
2)在性别信息前新增邮箱信息
3)删除性别信息
4. 作业4
通过Java代码实现以下功能
- 使用 set 类型在 redis 中实现以下操作:
1)添加 3 个班级的成绩,每个班级使用一个 set 描述,键为班级编号
2)查看 3 个班级中所有不重复的成绩
3)对比第一以及第二个班级的成绩,查看第二个班级相比于第一个班级有哪些差异化的成绩
4)把运行的命令和服务器状态、日志等信息截图,并整理到 word 中,提交 word 文件。
2.使用 zset 类型实现以下操作:
1)存储用户姓名以及积分
2)根据积分由高到低排列所有用户的姓名
3)查看姓名为 tom 的用户排名
4)查看积分在 1000-5000 内的所有用户
5)把运行的命令和服务器状态、日志等信息截图,并整理到 word 中,提交 word 文件。
5. 作业5
1.使用mybatis连接mysql数据库并调试成功
2.配置实现redis替代mybatis二级缓存
3.实现redis自动缓存mysql数据并测试
package com.igeek.bean;import java.util.Date;public class Student {private String ID;private String name;private Date birthday;private Double score;private String major;private String telphone;@Overridepublic String toString() {return "Student{" +"ID='" + ID + '\\'' +", name='" + name + '\\'' +", birthday=" + birthday +", score=" + score +", major='" + major + '\\'' +", telphone='" + telphone + '\\'' +'}';}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 Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public double getScore() {return score;}public void setScore(double score) {this.score = score;}public String getMajor() {return major;}public void setMajor(String major) {this.major = major;}public String getTelphone() {return telphone;}public void setTelphone(String telphone) {this.telphone = telphone;}
}package com.igeek.mapper;import com.igeek.bean.Student;public interface StudentMapper {Student selectById(String ID);}package com.igeek.utils;import com.igeek.cache.RedisCache;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.stereotype.Component;@Component
public class RedisCacheTranster {@Autowiredpublic void setRedisTemplate(RedisTemplate redisTemplate){RedisCache.setRedisTemplate(redisTemplate);}
}<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.igeek.mapper.StudentMapper"><!-- <cache type="cache.RedisCache"/>--><!-- <cache type="com.igeek.cache.RedisCache"/>-->
<!--auto generated Code-->
<select id="selectById" useCache="true" parameterType="String" resultType="com.igeek.bean.Student">SELECT *FROM studentwhere id=#{id}
</select></mapper>import com.igeek.bean.Student;import com.igeek.mapper.StudentMapper;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestMapper {@AutowiredStudentMapper studentMapper;@Testpublic void test1(){System.out.println("---------第一次查询---------");Student student=studentMapper.selectById("1001");System.out.println(student);System.out.println("---------第二次查询---------");Student student1=studentMapper.selectById("1001");System.out.println(student);}}