SpringBoot 高级部分笔记3

一、Spring Boot与缓存

在这里插入图片描述

1. 准备工作

在这里插入图片描述

建一个springboot 选上web cache mysql 等等
在这里插入图片描述
Service层

package com.atguigu.cache.service;

import com.atguigu.cache.bean.Employee;
import com.atguigu.cache.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EmployeeService
{
    @Autowired
    EmployeeMapper employeeMapper;
    public Employee getEmp(int id)
    {
        System.out.println("查询"+id+"号员工");
        Employee emp=employeeMapper.getEmpById(id);
        return emp;
    }
}

Control层

package com.atguigu.cache.controller;

import com.atguigu.cache.bean.Employee;
import com.atguigu.cache.mapper.EmployeeMapper;
import com.atguigu.cache.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EmployeeController
{
    @Autowired
    EmployeeService employeeService;
    从路径变量中 取出id占位符的值
    @GetMapping("/emp/{id}")
    public Employee getEmployee(@PathVariable("id") int id)
    {
        Employee employee= employeeService.getEmp(id);
        return employee;
    }
}

专门写一个mapper放mapper,然后在Application写上扫描mapper

@MapperScan("com.atguigu.cache.mapper")

整合mybatis 在配置文件application.properties写上

spring.datasource.url=jdbc:mysql://localhost:3306/spring_cache?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=Ys04050016
mybatis.configuration.map-underscore-to-camel-case=true
#上边这个是开启驼峰命名匹配规则 这样名称不一模一样也是可以直接用的

2. Cache初体验以及其参数

在service里面的方法加上@Cacheable(cacheNames = {“xxx”})
名字属性自己取名字

package com.atguigu.cache.service;

import com.atguigu.cache.bean.Employee;
import com.atguigu.cache.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class EmployeeService
{
    @Autowired
    EmployeeMapper employeeMapper;
    @Cacheable(cacheNames = {"emp"})
    public Employee getEmp(int id)
    {
        System.out.println("查询"+id+"号员工");
        Employee emp=employeeMapper.getEmpById(id);
        return emp;
    }
}

然后在Application中加上 @EnableCaching

如果不加缓存我每次查询 二号都发出一个语句 如今加上缓存就只发出一次就可以了
在这里插入图片描述
在这里插入图片描述
同时@Cacheable有许多参数
condition那个“#a0>1”代表第一个参数必须要大于1才进行缓存,unless是除非第一个参数==2才进行缓存
在这里插入图片描述

3.@CachePut

既调用了方法,有更新了缓存数据,修改了数据库的某个数据又同时更新缓存
用法:
首先在service层,注意这个key一定要写否则更新缓存不能覆盖

@CachePut(value="emp",key="#result.id")
    public Employee updateEmp(Employee employee)
    {
        System.out.println("service的update调用"+employee);
        employeeMapper.updateEmp(employee);
        return employee;
    }

在control层

 @GetMapping("/emp")
    public Employee update(Employee employee)
    {
        Employee emp=employeeService.updateEmp(employee);
        return emp;
    }

调用
这个是更新的方法调用在这里插入图片描述
查询方法
在这里插入图片描述
运行结果与预期一致!
它是先调用方法 之后再存到缓存。

4.删除缓存@CacheEvict

@CacheEvict(value=“emp”,key = “#id”)
key指定缓存 ,他还有参数 例如allEntries=true清除所有缓存数据
还有一个参数:在这里插入图片描述
service:

@CacheEvict(value="emp",key = "#id")
    public void deleteEmp(int id)
    {
        System.out.println("执行了service的deleteEmp操作  所删除的id为:"+id);
        //employeeMapper.deleteEmpById(id);


    }

control

  @GetMapping("/delemp")
    public String deleteEmp(int id)
    {
        employeeService.deleteEmp(id);
        return "成功了\t\t\t\t!!!!!!!!!!!!!!!!!!!!!";
    }

查询http://localhost:8080/emp/1在这里插入图片描述
运行:清缓存
http://localhost:8080/emp?id=1
清楚缓存id=1的
然后再去查询 (清除缓存并没有删除数据库中的数据),会发现控制台
在这里插入图片描述

5.@CacheConfig

在这里插入图片描述
@CacheConfig(cacheNames=“emp”)
这样一些@cache注解可以不用写value!!!!

6.跳了redis的相关内容==!!

准备工作:docker的安装

二、SpringBoot与消息

下载一些软件
在这里插入图片描述在这里插入图片描述
默认用户名密码root 123456在这里插入图片描述在这里插入图片描述
打开SmarTTY
host name是上边得出来的->user name 默认为root 密码同上
在这里插入图片描述
在这里插入图片描述
在启动docker失败的时候,解决方案 输入 yum update 然后重启虚拟机然后再去下载docker就ok了

1.RabbitMQ

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

2.Exchange 类型

在这里插入图片描述在这里插入图片描述

3.RabbitMQ的安装

启动SmarTTY然后输入 docker pull rabbitmq: 3-management
然后等待下载。
在这里插入图片描述
然后输入在这里插入图片描述后面的东西是对应上边的那个ID的 要一样的。在这里插入图片描述
这样就ok了 。
然后在浏览器上输入192.168.1.101.15672

在这里插入图片描述
然后出来登录页面 输入用户名密码都是guest 就ok了!!!!

4.RabbitMQ的测试

在这里插入图片描述
添加交换器:在这里插入图片描述
添加了三个交换器在这里插入图片描述
添加四个消息队列在这里插入图片描述
然后按照第一张图片的关联关系进行绑定
direct 和 fanout都是按照这样绑定在这里插入图片描述
对于topic这样来绑定在这里插入图片描述
测试
在direct交换器中 publish message在这里插入图片描述发送这个消息。对于direct交换器它是完全匹配路由键的–atguigu,所以只有atguigu这个队列能收到!查看atguigu这个队列
在这里插入图片描述
相反的fanout绑定的所有的队列,不管你路由键是什么,他会全发过去!在fanout交换器中发送
在这里插入图片描述
可以发现所有队列都能收到在这里插入图片描述

5.rabbitTemplate发送消息和amqpadmin

package com.atguigu.amqp;

import com.sun.corba.se.spi.ior.ObjectKey;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.Map;

@SpringBootTest
class SpringbootAmqpApplicationTests {

    @Autowired
    RabbitTemplate rabbitTemplate;
    //单播--点对点
    @Test
    public void contextLoads()
    {
        Map<String, Object>map=new HashMap<>();
        map.put("msg","第2个消息");
        rabbitTemplate.convertAndSend("exchange.direct","atguigu.news",map);
    }
    @Test
    public void receive()
    {
        Object o=rabbitTemplate.receiveAndConvert("atguigu.news");
        System.out.println(o);
    }
}
第一个是发送的
第二个是接受的
添加监听
@Service
public class BookService
{
    @RabbitListener(queues = "atguigu.news")
    public void receive(Book book)
    {
        System.out.println("atguigu.new 有消息进入"+book);
    }
}

在这里插入图片描述

三. Springboot与消息检索

1. elasticsearch

入门操作 参考官方文档 elasticsearch官网
在这里插入图片描述

在这里插入图片描述

1.1 Jest整合

在这里插入图片描述在这里插入图片描述

1.2 SpringDate Elasticsearch

有点问题 未完

四. Springboot与任务

1.异步任务

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

运行三秒后显示

在这里插入图片描述
在方法上边加上这个注解代表异步在这里插入图片描述
然后在这里插入图片描述
这个时候不需要响应三秒直接显示

2.定时任务

在这里插入图片描述
在这里插入图片描述
然后开启注解在这里插入图片描述
这样 每次0,1,2,3,4秒的时候都会打印hello

注意那个注解里面的cron六位代表 秒分时日期月份星期几之间用逗号隔开哦~
*就是任意 上边图片显示了
0,12,3是枚举 等价于0-3
0/4没四秒执行一次

在这里插入图片描述

3.邮件任务

一个简单的发邮件
第二个是授权码
第三个是在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

package com.atguigu.springboottask;

import org.apache.naming.factory.SendMailFactory;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;

@SpringBootTest
class SpringbootTaskApplicationTests {
    @Autowired
    JavaMailSenderImpl MailSender;
    @Test
    void contextLoads()
    {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("这是邮件的主题!");
        message.setText("这是邮件的内容");

        //发给谁
        message.setTo("1414659694@qq.com");
        //谁发的
        message.setFrom("1414659694@qq.com");
        MailSender.send(message);//发送
    }

}

这样就可以啦!!!
复杂点的

@Test
void mailTest2() throws MessagingException {
    //邮件设置2:一个复杂的邮件
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    //组装      true:开启多文本上传
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

    //正文
    helper.setSubject("这是邮件的主题!");
    helper.setText("<p style='color:red'>这是文字内容...</p>",true);//true:开启html支持

    //添加附件
    helper.addAttachment("ntx.jpg",new File("C:\\Users\\lingStudy\\Desktop\\ntx.jpg"));

    //发给谁
    helper.setTo("lingstudyzlf@qq.com");
    //谁发的
    helper.setFrom("lingstudyzlf@qq.com");
    mailSender.send(mimeMessage);//发送
}


/**
     * 
     * @param html :是否开启多文本上传
     * @param subject :邮件标题
     * @param text :邮件内容
     * @throws MessagingException
     * @Author 编程之外
     */
//封装成一个方法(以后作为一个工具类直接使用)
public void sengMail(Boolean html,String subject,String text) throws MessagingException {
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    //组装   true:开启多文本上传
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, html);

    //正文
    helper.setSubject(subject);
    helper.setText(text,true);//true:开启html支持

    //添加附件
    helper.addAttachment("ntx.jpg",new File("C:\\Users\\lingStudy\\Desktop\\ntx.jpg"));

    //发给谁
    helper.setTo("lingstudyzlf@qq.com");
    //谁发的
    helper.setFrom("lingstudyzlf@qq.com");
    mailSender.send(mimeMessage);//发送
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值