Springboot知识

注解

@SpringBootApplication

参考网址:springboot快速入门及@SpringBootApplication注解分析

scanBasePackages

    此注解其实是ComponentScan注解的basePackages,通过查看scanBasePackages即可得知。
    @SpringBootApplication只会扫描@SpringBootApplication注解标记类包下及其子包的类,将这些类纳入到spring容器,只要类有@Component注解即可。
    有的注解的定义中已加入@Component,所以这些注解也会被扫描到:@Controller,@Service,@Configuration,@Bean

    详见:《深入浅出Spring Boot 2.x》=> 3.2.1 通过扫描装配你的Bean

@MapperScan

作用:定义扫描MyBatis接口

示例:@MapperScan(annotationClass = Mapper.class, basePackages = "com.springboot.chapter15")

SpringBootServletInitializer

其他网址

使用jsp作为视图模板&常规部署

作用

    和web.xml有相同作用:部署web应用程序。
    继承SpringBootServletInitializer之后,可以使用外部tomcat容器,自己可设置端口号,项目名等。不需要用外部tomcat的话不用继承(继承了也不会被执行)。
    现在JavaConfig配置方式在逐步取代xml配置方式。而WebApplicationInitializer可以看做是web.xml的替代,它是一个接口。通过实现WebApplicationInitializer,在其中可以添加servlet,listener等,在加载Web项目的时候会加载这个接口实现类,从而起到web.xml相同的作用。查看SpringBootServletInitializer定义可知,它实现了WebApplicationInitializer。    

用法

public class ServletInitializer extends SpringBootServletInitializer {
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(SsmShopApplication.class);
	}
}

分析

SpringBoot 中的 ServletInitializer

常规容器下SpringBootServletInitializer如何实现web.xml作用解析

Template

StringRedisTemplate

另见:《深入浅出Spring Boot 2.x》=> 7.1.2 RedisTemplate

其他网址

StringRedisTemplate操作redis数据
如何使用StringRedisTemplate操作Redis详解

StringRedisTemplate与RedisTemplate区别

  • 两者的关系:StringRedisTemplate继承RedisTemplate。

  • 两者的数据是不共通的:也就是说StringRedisTemplate只能管理StringRedisTemplate里面的数据,RedisTemplate只能管理RedisTemplate中的数据。

  • 其实他们两者之间的区别主要在于他们使用的序列化类:

    RedisTemplate用的是JdkSerializationRedisSerializer,会将数据先序列化成字节数组然后存入Redis数据库。 
      StringRedisTemplate使用的是StringRedisSerializer

  • 使用时注意事项:

   当你的redis数据库里面本来存的是字符串数据或者你要存取的数据就是字符串类型数据的时候,那么你就使用       
      StringRedisTemplate即可。
   但是如果你的数据是复杂的对象类型,而取出的时候又不想做任何的数据转换,直接从Redis里面取出一个对象,
       那么使用RedisTemplate是更好的选择。

  • RedisTemplate使用时常见问题:

   redisTemplate 中存取数据都是字节数组。当redis中存入的数据是可读形式而非字节数组时,使用redisTemplate取值
       的时候会无法获取导出数据,获得的值为null。可以使用 StringRedisTemplate 试试。

StringRedisTemplate中定义了5种数据结构操作

        和redis中的数据类型是一样的。也分为五种,对应着不同的方法:            

类型

获取方法

作用

GeoOperations

redisTemplate.opsForGeo("xxx");

获取地理位置操作接口

HashOperations

redisTemplate.opsForHash("xxx");

获取散列操作接口

ListOperations

redisTemplate.opsForList("xxx");

获取列表(链表)操作接口

SetOperations

redisTemplate.opsForSet("xxx");

获取集合操作接口

ValueOperations

redisTemplate.opsForValue("xxx");

获取字符串操作接口

ZSetOperations

redisTemplate.opsForZSet("xxx");

获取有序集合操作接口

        可以通过bound封装指定的key,然后进行一系列的操作而无须“显式”的再次指定Key。Spring提供了对应的BoundXXXOperations接口。(BoundXXXOperations与XXXOperations的方法完全一致)

类型

获取方法

作用

BoundGeoOperations

redisTemplate.boundGeoOps("xxx");

获取地理位置绑定键操作接口

BoundHashOperations

redisTemplate.boundHashOps("xxx");

获取散列绑定键操作接口

BoundListOperations

redisTemplate.boundListOps("xxx");

获取列表(链表)绑定键操作接口

BoundSetOperations

redisTemplate.boundSetOps("xxx");

获取集合绑定键操作接口

BoundValueOperations

redisTemplate.boundValueOps("xxx");

获取字符串绑定键操作接口

BoundZSetOperations

redisTemplate.boundZSetOps("xxx");

获取有序集合绑定键操作接口

常用操作

方法

作用

字符串

stringRedisTemplate.opsForValue().set("test", "100");

向redis永久存入数据

stringRedisTemplate.opsForValue().set("test", "100",60*10,TimeUnit.SECONDS);

向redis里存入数据和设置过期时间  

stringRedisTemplate.opsForValue().get("test")

根据key获取redis中的val

集合(set)

stringRedisTemplate.opsForSet().add("red_123", "1","2","3");

向指定key中存放set集合

stringRedisTemplate.opsForSet().isMember("red_123", "1")

根据key查看集合中是否存在指定数据

stringRedisTemplate.opsForSet().members("red_123");

根据key获取set集合

过期(expire)

stringRedisTemplate.getExpire("test")

根据key获取过期时间

stringRedisTemplate.getExpire("test",TimeUnit.SECONDS)

根据key获取过期时间并换算成指定单位

stringRedisTemplate.expire("red_123",1000 , TimeUnit.MILLISECONDS); 

设置过期时间

事务

stringRedisTemplate.boundValueOps("test").increment(-1);

val做-1操作

其他

stringRedisTemplate.delete("test")

根据key删除

使用示例

1.配置pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

2.配置application.yml

spring:
  redis:
    port: 6379
    host: 192.168.xxx.xxx
    #    passwd: xxxxxx
    # 下边的可以省略
    timeout: 1000

3.java

public class TestRedis{
    @Autowired
    private StringRedisTemplate stringRedisTemplate = null;

    public void testFunc(void){
        stringRedisTemplate.opsForValue().set("hello", "world");
        String str = stringRedisTemplate.opsForValue().get("hello");
        System.out.println(str);
    }
}

测试结果:打印出: world

RestTemplate

发布了126 篇原创文章 · 获赞 11 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/feiying0canglang/article/details/104123932