Spring Boot 学习01-----搭建一个简单的spring-boot-demo

今天开始要系统性的学习Spring Boot。这个熟悉又陌生的框架,已经陪伴了我2年多。百尺竿头更进一步,这里使用Idea来搭建一个SpringBoot项目。

系统环境

工具版本号
spring-boot2.4.5
jdk1.8

实施步骤

  1. 选中 File---->New—>Module,进入创建项目的页面。
    在这里插入图片描述
  2. 进入Module页面之后,选中Spring Initializer 会引导我们创建一个SpringBoot项目,默认选择最新的SpringBoot,当然也可以选中Custom,选择一个我们自己的私域连接,不过要确保这链接是有效的。
    在这里插入图片描述
  3. 对项目进行配置,指定项目的 groupId和artifactId,这两个非常关键,直接决定了项目名,下面还有可以选在JDK的版本,以及指定包路径。
    在这里插入图片描述
  4. 我们一般都是用SpringBoot来创建一个Web项目,所以需要选中Spring Web
    在这里插入图片描述
    点击确认之后,项目就创建成功了,项目的结构如下图所示:
    在这里插入图片描述
    选中启动类SpringBootDemoNewApplication,右键运行项目可以直接可以启动成功,默认的启动端口是 8080, 默认项目名是 /
    在这里插入图片描述

不过这样的启动是空洞无味了,让我们给他上点色吧,比如添加一个controller

controller

依赖添加好之后,就是编写一个简单的controller了。

。。。。。。。。。。。。。。。。。

版权原因,完整文章,请参考如下:Spring Boot 学习01-----搭建一个简单的spring-boot-demo

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,我可以为你提供一个Spring Boot Starter Data Redis的Demo。以下是一个简单的演示: 首先,确保你已经在你的项目中添加了`spring-boot-starter-data-redis`依赖。 接下来,创建一个Redis配置类,比如`RedisConfig`: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericToStringSerializer; @Configuration public class RedisConfig { @Bean public RedisConnectionFactory redisConnectionFactory() { return new LettuceConnectionFactory(); } @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory()); redisTemplate.setValueSerializer(new GenericToStringSerializer<>(Object.class)); return redisTemplate; } } ``` 然后,创建一个简单的控制器类,比如`RedisController`: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class RedisController { private final RedisTemplate<String, Object> redisTemplate; @Autowired public RedisController(RedisTemplate<String, Object> redisTemplate) { this.redisTemplate = redisTemplate; } @GetMapping("/set/{key}/{value}") public void setValue(@PathVariable String key, @PathVariable String value) { redisTemplate.opsForValue().set(key, value); } @GetMapping("/get/{key}") public Object getValue(@PathVariable String key) { return redisTemplate.opsForValue().get(key); } } ``` 最后,启动你的Spring Boot应用程序,并通过以下URL进行测试: 设置值:`http://localhost:8080/set/{key}/{value}` 获取值:`http://localhost:8080/get/{key}` 请确保替换`{key}`和`{value}`为实际的键和值。 这只是一个简单的示例,你可以根据你的需求进一步扩展和定制。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值