核心概念
概念 说明 IOC(控制反转) 将对象的创建和管理交给 Spring 容器,而非程序员手动 new DI(依赖注入) IOC 的具体实现手段,容器自动将依赖对象注入到需要的地方
环境启动相关代码
package com.ruoyi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
/**
* 启动程序
*
* @author ruoyi
*/
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class RuoYiApplication
{
public static void main(String[] args)
{
// System.setProperty("spring.devtools.restart.enabled", "false");
SpringApplication.run(RuoYiApplication.class, args);
System.out.println("(♥◠‿◠)ノ゙ 若依启动成功 ლ(´ڡ`ლ)゙ \n" +
" .-------. ____ __ \n" +
" | _ _ \\ \\ \\ / / \n" +
" | ( ' ) | \\ _. / ' \n" +
" |(_ o _) / _( )_ .' \n" +
" | (_,_).' __ ___(_ o _)' \n" +
" | |\\ \\ | || |(_,_)' \n" +
" | | \\ `' /| `-' / \n" +
" | | \\ / \\ / \n" +
" ''-' `'-' `-..-' ");
}
}
其中的注解说明如下
@SpringBootApplication:组合注解,用于启动 Spring 环境,自动扫描组件
SpringApplication.run():启动 Spring 容器,所有 Bean 由框架创建和管理
@Component
public class RedisCache
此处的注解 @Component 将类注册Bean到Spring容器中,由Spring负责创建和管理
@Component
public class RedisCache
{
@Autowired
public RedisTemplate redisTemplate;
而这里的 @Autowired 注解则将依赖的对象自动注入前面spring创建的依赖 RedisCache 当中,其下行的代码为依赖的对象,而后便可直接使用 redisTemplate
public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit)
{
redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
}
这便是 RedisCache 工具类的一个泛型方法( <T> )
其作用便是向 Redis 缓存中存储数据,并同时设置过期时
其中的 setCacheObject 为其缓存对象
而 set 括号内的是它的几个参数
MVC的核心逻辑 :Spring MVC 通过注解建立起 “请求 URL → 控制器方法 → 数据绑定 → 响应格式” 的完整映射关系。
@RestController
@RequestMapping("/system/student")
public class MyStudentController extends BaseController
第二行输入的 @RequestMapping 为所有的方法添加了统一的路径前缀,这一用法简化了方法的路径配置
@GetMapping(value = "/{id}")
public AjaxResult getInfo(@PathVariable("id") Long id)
{
return AjaxResult.success(myStudentService.selectMyStudentById(id));
}
使用了 @Pathvariable 方法自动提取变量值
主要区别于使用了 @RequestMapping 和 @Pathvariable 的写法