【SpringBoot】集成 redis 和SpringMVC 集成jsp (不推荐使用) 集成Thameleaf

1.Redis

1.原理图

在这里插入图片描述

2.步骤

(1)添加redis的起步依赖
(2) 配置redis的连接信息

spring.redis.host=localhost
# redis的端口号
spring.redis.port=6379


3)注入RedisTemplate测试redis操作

@SpringBootTest
class Demo06redisApplicationTests {
   

    @Autowired
    RedisTemplate<String, String> rt;

    @Test
    void test01() {
   
        //查找是否有缓存
        String json = rt.boundValueOps("cache1").get();

        //有就直接返回
        if (json != null) {
   
            System.out.println("来自缓存:" + json);
        } else {
   
            String json_ = "{name:jack,age:13}";
            //没有就先存,再返回
            rt.boundValueOps("cache1").set(json_);
            System.out.println("保存json " + json_);
        }


    }

}

2.SpringMVC

1.修改端口

可以修改tomcat的端口和访问项目中的静态资源

server.port=8090
server.servlet.context-path=/web01

2.静态资源

1)static目录的作用是什么?
类型webapp,存放静态资源的,如css,js,image
该目录下的资源可以直接访问

(2)全局搜索 CLASSPATH_RESOURCE_LOCATIONS
在这里插入图片描述

3.SpringBoot的注解开发(spirngBoot是全注解开发,但是想要使用配置也可以)

1)@Configuration标记在类上,相当于applicationContext.xml

2)@Bean配置在返回bean对象的方法上,将返回值对象放入ioc容器中
之后可以使用@Autowired 依赖注入(相当于标签)

@Bean将当前方法的返回值 放到 ioc容器 ,并且在扫描注解时就已经创建好了Person对象,并且加入IOC中

把Person对象加入IOC容器

@Configuration
public class DaoBeanConfig {
   
    @Bean//将当前方法的返回值 放到 ioc容器  
    public Person getPerson(){
   
        //一般不是@Service @Repository @Controller
        Person person = new Person();
        person.setPassword("12345");
        person.setUsername("root");
        return person;
    }
}

person

@Data
public class Person {
   
    private String username;
    private String password;

    public Person() {
   
        System.out.println("执行Person构造方法");
    }
}

测试

@SpringBootTest
class Demo07springmvcApplicationTests {
   

    @Autowired
    Person person;//从ioc里面取对象
    @Test
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Windows 10上使用Spring Boot集成Redis,你可以按照以下步骤进行操作: 步骤1:安装和配置Redis 1. 下载Redis:在Windows上,你可以从Redis官方网站(https://redis.io/download)下载最新的Redis版本。 2. 解压Redis:将下载的Redis压缩包解压到一个目录中,例如 `C:\redis`。 3. 配置Redis:在Redis目录中,找到并打开`redis.windows.conf`文件。根据你的需求,可以修改配置文件中的一些参数,比如端口号和密码。保存并关闭文件。 步骤2:启动Redis服务器 1. 打开命令提示符(CMD)或PowerShell,并导航到Redis目录。 2. 在命令提示符中输入 `redis-server.exe redis.windows.conf`,然后按回车键启动Redis服务器。 步骤3:在Spring Boot项目中集成Redis 1. 在你的Spring Boot项目的`pom.xml`文件中添加Redis依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 在`application.properties`(或`application.yml`)文件中配置Redis连接信息: ```properties spring.redis.host=localhost spring.redis.port=6379 spring.redis.password=your_password (如果设置了密码的话) ``` 3. 创建一个Java类作为Redis配置类,使用`@Configuration`注解标记,并使用`@EnableCaching`启用缓存支持: ```java import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Configuration; @Configuration @EnableCaching public class RedisConfig { // 配置其他Redis相关的bean或缓存配置 } ``` 4. 在需要使用Redis的地方,使用`@Cacheable`、`@CachePut`、`@CacheEvict`等注解来实现缓存操作。 现在你已经成功集成Redis到你的Spring Boot项目中。你可以使用Redis来实现缓存、分布式锁等功能。记得启动Redis服务器后,确保你的Spring Boot应用程序能够连接到Redis服务器。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值