驴のSpringBoot学习笔记(二)
部分笔记来自于黑马教育
1.NoSQL
1.1Redis数据库
Redis和MySQL一样,也是一种数据库类型,但和MySQL相比有着一些不同:
- MySQL是关系型数据库,而Redis是非关系型数据库(NoSQL)。
- MySQL用于持久化存储数据到硬盘,功能强大,但是速度缓慢;而Redis用于存储使用较为频繁的数据到缓存中,读取速度快。
- MySQL的数据存放在磁盘中;而Redis的数据存放在内存中。
下载地址:https://github.com/tporadowski/redis/releases (科学上网)
1.1.1Redis数据库简单使用
下载完成后文件目录:
在该目录下启动cmd,并输入 redis-cli.exe ,你会看到这玩意
这里先不要操作,先输入shutdown,再exit
执行完上述操作后,再输入 redis-server.exe redis.windows.conf,就可以运行我们的redis了
再重新打开 redis-cli.exe,尝试一次简单的存取
ok,现在你已经会了redis了👍
1.2Mongodb数据库
Mongodb是一个开源文档型数据库,是NoSQL数据库产品的一种,是最像关系型数据库的非关系型数据库
特点:
临时性存储相结合,且修改频率高(类似于游戏道具,直播间在线人数),应用场景数据变化较快
2.SpringBoot整合Redis
2.1创建整合Redis的新项目

2.2简单编写一个测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| @SpringBootTest class SpringbootRedisDemoApplicationTests {
@Autowired RedisTemplate redisTemplate;
@Test void set() { ValueOperations ops = redisTemplate.opsForValue(); ops.set("name","hello world!");
}
@Test void get() { ValueOperations ops = redisTemplate.opsForValue(); Object name = ops.get("name"); System.out.println(name); }
}
|
执行结果:

成功get到了
3.常用第三方技术整合
3.1关于缓存

3.2使用缓存
1.导入缓存依赖
1 2 3 4 5
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
|
2.启用缓存
1 2 3 4 5 6 7 8 9
| @SpringBootApplication @EnableCaching public class DemoApplication {
public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }
}
|
3.使用缓存
1 2 3 4 5 6 7 8 9 10 11 12 13
|
@Override @Cacheable(value = "cacheSpace",key = "All") public List<User> getAll() { return userDao.getAll(); }
|
3.3关于缓存的实例:手机验证码模拟
关于在pom里整合cache,还有启动类的注解在上面讲过了,这里就省略
包装类:
1 2 3 4 5 6 7 8 9 10 11
|
@Data public class SMSCode {
private String tele; private String code; }
|
service接口
1 2 3 4 5 6 7 8 9 10 11 12 13
|
public interface SMSCodeService {
public String sendCodeToSMS(String tele); public boolean checkCode(SMSCode smsCode);
}
|
接口实现类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
@Service public class SMSCodeServviceImpl implements SMSCodeService {
@Autowired private CodeUtil codeUtil;
@Override @CachePut(value = "smsCode",key = "#tele") public String sendCodeToSMS(String tele) { String generator = codeUtil.generator(tele); return generator; }
@Override public boolean checkCode(SMSCode smsCode) { String tele = smsCode.getTele(); String code = codeUtil.get(tele); return smsCode.getCode().equals(code); } }
|
工具类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
@Component public class CodeUtil {
private String[] patch = {"000000", "00000", "0000", "000", "00", "0", ""};
public String generator(String tele) { int code = tele.hashCode(); int encryption = 2023510;
long result = code ^ encryption; long now = System.currentTimeMillis();
result = result ^ now; long pwcode = result % 1000000; pwcode = pwcode < 0 ? -pwcode : pwcode; String codestr = pwcode + ""; int length = codestr.length();
return patch[length] + codestr; }
@Cacheable(value = "smsCode",key = "#tele") public String get(String tele){ return null; }
}
|
control类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
@RestController @RequestMapping("/sms") public class SMSCodeController {
@Autowired private SMSCodeService smsCodeService;
@GetMapping public String getCode(String tel){
String code = smsCodeService.sendCodeToSMS(tel); return code; } @PostMapping public boolean checkCode(SMSCode smsCode){ return smsCodeService.checkCode(smsCode); } }
|
结果实现:
启动咱们的apifox,嘿,发送个get请求,嘿:
嘿,咱再发送个post请求,验证验证我们的手机号和验证码

true了😆
4.Quartz触发器
Quartz是一个触发器框架,主要功能就是配置程序的一个触发器,比如(零点秒杀,就需要一个在零点自动启动的程序)

步骤:
1.在pom里整合Quartz
1 2 3 4 5
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency>
|
2.创建quartz类,注意,该类必须继承 QuartzJobBean才能生效(乌龟的屁股——龟腚)
1 2 3 4 5 6 7 8 9 10 11 12 13
|
public class MyQuartz extends QuartzJobBean { @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { System.out.println("quartz is running..."); } }
|
3.创建配置类来配置工作明细,触发器
这里面门道有点多,建议各位自己找资料深入了解了解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
@Configuration public class QuartzConfig {
@Bean public JobDetail jobDetail() { return JobBuilder.newJob(MyQuartz.class).storeDurably().build(); }
@Bean public Trigger jobTrigger() {
CronScheduleBuilder schedule = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
return TriggerBuilder.newTrigger().forJob(jobDetail()).withSchedule(schedule).build(); }
}
|
简而言之:工作明细,肯定需要你把具体工作传进来,所以它的作用就是将工作传入Quartz,触发器肯定要知道你的工作明细才知道你做什么,所以它需要你传入工作明细,再把它传入Quartz,触发器肯定也需要一个触发时间,那么就还需要一个CronScheduleBuilder对象来设置时间
工作,工作明细,触发器一环扣一环,也体现出Quartz结构比较严谨
5.spring简化定时任务
上面的步骤比较繁琐,还有更简单的方法
1.首先在启动类上加上开启定时任务功能的注解
2.另外编写一个类,专门写定时任务
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
@Component public class task {
@Scheduled(cron = "0/1 * * * * ?") public void print(){ System.out.println("hello world"); } }
|
这个要注意加@Component
6.监控
