文章目录
- SpringBoot
-
- 第五章
- 第六章
-
- 1、springboot使用ajax
- 2、springboot使用reids
-
- 1、单机版
-
- **使用步骤**
-
- 1-5步
- 6
- 7-9步
- RedisTemplate
-
- 使用RedisTemplate
- 2、集群版
-
- 开启集群
- 项目配置
-
- 1
- 2
- 3
- 4-5
- 第七章
-
- 1、springboot文件上传
-
- 使用步骤
-
- 1-2
- 3
- 4-5
- 2、springboot邮件发送
-
- 步骤
-
- 1-2
- 3
- 4
- 5
- 3、springboot拦截器
-
- 1、创建应用的基本结构与拦截器无关
- 2、加入拦截器
- 4、springboot整合shiro
-
- 1、与传统ssm区别
- 2、步骤
-
- 1-3
- 4
- 5
- 6-7
- 8
- 9-10
- 11
- 12-13
- 14
- 15
- 16-17
- 18-19
- 20
- 21
SpringBoot
第五章
整章都在讲thymeleaf的语法
第六章
1、springboot使用ajax
如果通过ajax请求访问springboot控制器的方法:
查询使用get请求: @GetMapping
添加使用post请求: @PostMapping
删除使用delete请求: @DeleteMapping
修改使用put请求: @PutMapping
2、springboot使用reids
1、单机版
使用步骤
1-5步
@@@@@@在springboot中使用单机版的redis
1、开启redis
A、进入/usr/local/d113/redis/bin
B、执行命令启动redis端
./redis-server redis.conf
C、执行命令启动redis客户端
./redis-cli
2、创建springboot项目
3、导入下列依赖
1、热部署
2、web
3、lombok
4、mysql
5、mybatis-plus
6、druid
7、spring-boot-data-starter-redis
8、velocity
9、swagger
4、使用代码生成器生成代码
5、编写配置类,扫描Mapper接口
@Configuration
@MapperScan(basePackages = "com.qs.mapper")
@EnableSwagger2
public class WebConfig {
}
6
6、编写yml
server:
port: 9999
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///d113
username: root
password: root
redis:
host: 192.168.47.128
port: 6379
jedis:
pool:
min-idle: 100 #最小闲置连接数
max-idle: 200 #最大闲置连接数
max-active: 300 #最大连接数
time-between-eviction-runs: 3s #如果闲置连接超过上限,就将3s未使用的闲置连接销毁
max-wait: 3s #等待连接的时间
logging:
level:
com:
qs:
mapper: debug
7-9步
7、在配置类中,启用缓存管理
@EnableCaching //启用缓存管理
8、在service接口以及实现类中编写方法,并胜在service的方法上加上注解,使用redis进行缓存
@Service
public class InfoServiceImpl extends ServiceImpl<InfoMapper, Info> implements InfoService {
@Autowired
private InfoMapper infoMapper;
/**
*加了该注解后,在查询时,将会在redis进行缓存,并且自动生成key的名称
* key的名称= findById::id
*/
@Cacheable(value = "findById")
@Override
public Info findById(Integer id) {
System.out.println("--------------------从数据库查询了编号为"+id+"的数据");
return infoMapper.selectById(id);
}
}
9、编写控制器类进行测试
@RestController
@RequestMapping("/info")
public class InfoController {
@Autowired
private InfoService infoService;
@GetMapping("/find")
public Info find(Integer id){
return infoService.findById(id);
}
}
RedisTemplate
@@@@@@这种方式的缓存主要是适用于非高并发环境,如果在高并发环境下,容易出现缓存击穿。为了解决该问题,我们可以使用RedisTemplate模板类配置缓存
如果使用RedisTemplate配置redis,下列两个注解不需要指定:
@EnableCaching //启用缓存管理
@Cacheable(value = "findById")
使用RedisTemplate
@@@@@@@使用RedisTemplate
@Autowired
private InfoMapper infoMapper;
//注入redisTemplate模板
@Autowired
private RedisTemplate<Object,Object> redisTemplate;
public Info findById(Integer id) {
//默认情况下,RedisTemplate模板它在进行缓存时,有自己的序列化规则缓存数据,但这样会导致产生乱码,浏览起
//来不方便,为了解决该问题,我们一般要自己设置RedisTemplates模板的键使用序列化规则,防止产生乱码
//指定序列化规则,防止出现乱码,这种序列化规则不会产生乱码
StringRedisSerializer serializer = new StringRedisSerializer();
//指定RedisTemplate在缓存数据时,key(键)采用我们设置的序列化规则,这样就不会出现乱码
redisTemplate.setKeySerializer(serializer);
//查询数据库之前,首先判断redis中有没有需要的数据
Info info = (Info) redisTemplate.opsForHash().get("springboot_113_7_01",id);//键,字段
//判断info是否为空
if(ObjectUtils.isEmpty(info)){
//缓存中没有查询到该对象,准备查询数据库,此时会有100个线程准备查询数据库
synchronized (this){
//再一次查询缓存,但此时,只允许一个线程一个线程查询
info = (Info) redisTemplate.opsForHash().get("springboot_113_7_01",id);
//再一次判断是否查询到对象
if(ObjectUtils.isEmpty(info)){
//此时,缓存中依然没有对象,需要去找数据库
System.out.println("----------------------从数据库中加载了编号为"+id+"的数据");
//查询数据库,获得对象
info = infoMapper.selectById(id);
//将对象存放到redis
redisTemplate.opsForHash().put("springboot_113_7_01",id,info);
return info;
}else{
//此时,缓存中已经有了对象,不需要查询数据库,直接返回缓存中的对象即可
System.out.println("----------------------从缓存中加载了编号为"+id+"的数据");
return info;
}
}
}else{
//缓存中查询到了对象,直接返回缓存中的对象
System.out.println("----------------------从缓存中加载了编号为"+id+"的数据");
return info;
}
}
2、集群版
开启集群
@@@@@@@@@在springboot中访问redis集群
####开启redis集群
1、进入 /usr/local/d113/redis-cluster目录
2、分别进入 7001~7006目录,启动6台redis服务器
./redis-server redis.conf
3、进入7001~7006任意目录,登录redis集群的客户端
cd /usr/local/d113/redis-cluster/7001
./redis-cli -h 192.168.47.128 -p 7001 -c
项目配置
1
#########在项目中进行如下配置
1、导入依赖
@@@如果只是使用redis集群,spring-boot-starter-data-redis可以导入也可以不导入
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
2
2、在yml文件中配置集群的服务器节点
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///d113
username: root
password: root
redis:
host: 192.168.47.128
port: 6379
jedis:
pool:
min-idle: 100 #最小闲置连接数
max-idle: 200 #最大闲置连接数
max-active: 300 #最大连接数
time-between-eviction-runs: 3s #如果闲置连接超过上限,就将3s未使用的闲置连接销毁
max-wait: 3s #等待连接的时间
cluster:
nodes: 192.168.47.128:7001,192.168.47.128:7002,192.168.47.128:7003,192.168.47.128:7004,
192.168.47.128:7005,192.168.47.128:7006
3
3、在配置类解析集群节点,获得jedis集群
@Configuration
@MapperScan(basePackages = "com.qs.mapper")
@EnableSwagger2
//@EnableCaching //启用缓存管理
public class WebConfig {
//192.168.47.128:7001,192.168.47.128:7002,192.168.47.128:7003,192.168.47.128:7004,192.168.47.128:7005,192.168.47.128:7006
@Value("${spring.redis.cluster.nodes}")//-------------------读取yml文件根据键取值赋值给属性
private