【SpringBoot】 什么是springboot(三)?springboot使用ajax、springboot使用reids

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 
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

arjunna

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值