本文主要是介绍【SpringBoot】 什么是springboot(三)?springboot使用ajax、springboot使用reids,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 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、开启redisA、进入/usr/local/d113/redis/binB、执行命令启动redis端./redis-server redis.confC、执行命令启动redis客户端./redis-cli2、创建springboot项目3、导入下列依赖1、热部署2、web3、lombok4、mysql5、mybatis-plus6、druid7、spring-boot-data-starter-redis8、velocity9、swagger4、使用代码生成器生成代码 5、编写配置类,扫描Mapper接口@Configuration@MapperScan(basePackages = "com.qs.mapper")@EnableSwagger2public class WebConfig {}
6
6、编写ymlserver:port: 9999spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql:///d113username: rootpassword: rootredis:host: 192.168.47.128port: 6379jedis: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进行缓存@Servicepublic class InfoServiceImpl extends ServiceImpl<InfoMapper, Info> implements InfoService {@Autowiredprivate InfoMapper infoMapper;/***加了该注解后,在查询时,将会在redis进行缓存,并且自动生成key的名称* key的名称= findById::id*/@Cacheable(value = "findById")@Overridepublic Info findById(Integer id) {System.out.println("--------------------从数据库查询了编号为"+id+"的数据");return infoMapper.selectById(id);}}9、编写控制器类进行测试@RestController@RequestMapping("/info")public class InfoController {@Autowiredprivate InfoService infoService;@GetMapping("/find")public Info find(Integer id){return infoService.findById(id);}}
RedisTemplate
@@@@@@这种方式的缓存主要是适用于非高并发环境,如果在高并发环境下,容易出现缓存击穿。为了解决该问题,我们可以使用RedisTemplate模板类配置缓存如果使用RedisTemplate配置redis,下列两个注解不需要指定:@EnableCaching //启用缓存管理 @Cacheable(value = "findById")
使用RedisTemplate
@@@@@@@使用RedisTemplate@Autowiredprivate InfoMapper infoMapper;//注入redisTemplate模板@Autowiredprivate 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);//将对象存放到redisredisTemplate.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.conf3、进入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.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql:///d113username: rootpassword: rootredis:host: 192.168.47.128port: 6379jedis: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
这篇关于【SpringBoot】 什么是springboot(三)?springboot使用ajax、springboot使用reids的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!