Spring基本的注释
@Controller
@Controller
public class ViewController {
@RequestMapping("view")
public String view(){
return "view.html";
}
}
--或者--
@Controller
public class ViewController {
@RequestMapping("view")
public String view(){
return "view";//可以抛去后缀
}
}
但需要配置yml
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.html
HTML页面数据渲染(含freemarker)
@Controller
public class ViewController {
public static final String PAGE = "view";
/*
* 不带数据直接返回,String直接返回
*/
@RequestMapping("view")//http://localhost:8080/view
public String view(){
return PAGE;
}
/*
* 带数据返回页面,ModelAndView对方返回
*/
@RequestMapping("data")//http://localhost:8080/data
public ModelAndView data(){
ModelAndView view = new ModelAndView(PAGE);
view.addObject("name","zhangsan");
view.addObject("age",18);
return view;
}
}
配置
#freemarker后缀
spring.freemarker.suffix=.html
spring.freemarker.template-loader-path=classpath:/static/
依赖
<!--引入freemaerker模板依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
@RequestParam
@Controller
public class ParamsController {
public static final String PAGE = "param";
@RequestMapping("/param")//http://localhost:8080/param?id=100 解决问号
public ModelAndView param(@RequestParam(value = "id",defaultValue = "0") int id){
System.out.println(id);
ModelAndView view = new ModelAndView(PAGE);
view.addObject("id",id);
return view;
}
}
@PathVariable
@RequestMapping("/path/{id}/{type}")//http://localhost:8080/path/12/ok 解决下划线
public ModelAndView path(@PathVariable(required = false) int id,
@PathVariable(required = false) String type){
System.out.println("id = " + id);
System.out.println("type = " + type);
ModelAndView view = new ModelAndView(PAGE);
view.addObject("id",id);
return view;
}
@RestController
@RestController注解等价于@ResponseBody + @Controller
@RestController
@RequestMapping("rest")
public class RestFullController {
/**
* 查询数据,不带数据
* @return
*/
@GetMapping//http://localhost:8080/rest
public String get() {
return "Hello World";
}
/**
* 查询数据,带数据
* @param id
* @return
*/
@GetMapping("{id}")//http://localhost:8080/rest/5
public User getById(@PathVariable("id") int id)
{
User user=new User(id, "18","女");
return user;
}
/**
* 新增数据
*/
@PostMapping
public User addUser(@RequestBody User user){
//使用RequestBody注解来web请求中的body体部分内容,并且通常会将其转换为一个javaBean的对象!
System.out.println(user);
return user;
}
}
@RestController注解等价于@ResponseBody + @Controller
@GetMapping("/param/user")//http://localhost:8080/param/user
@ResponseBody
public User getUser(){
System.out.println("现在使用controller+ResponseBody");
User user=new User(11111, "18","女");
return user;
}
@Service
正常情况下的代码
正常
private UserService userService;
@GetMapping("{id}")//http://localhost:8080/user/5
public User getById(@PathVariable("id") int id)
{
userService = new UserServiceImpl();
return userService.getById(id);
}
--UserService层
public interface UserService {
public User getById( int id);
}
--UserServiceImpl层
public class UserServiceImpl implements UserService {
public User getById(@PathVariable("id") int id)
{
User user=new User(id, "18","女");
return user;
}
}
其中需要反复出现userService = new UserServiceImpl();
其中 @Service 是注册bean到ico容器 @Autowired 是获取bean
@Service
是一个特殊的@Component
,它本质上是@Component
的派生注解。通过使用@Service
,我们可以告诉Spring容器去自动扫描和注册这些类为Bean,供依赖注入使用。
故
@Autowired
private UserService userService;
@GetMapping("{id}")//http://localhost:8080/user/5
public User getById(@PathVariable("id") int id)
{
return userService.getById(id);
}
--UserServiceImpl层
--注意只在接口层
@Service
public class UserServiceImpl implements UserService {
public User getById(@PathVariable("id") int id)
{
User user=new User(id, "18","女");
return user;
}
}
@Component与@Service的区别
@Service
@Service注解用于标识一个类作为服务层组件。服务层组件通常用于处理业务逻辑和数据访问。当我们希望在应用程序中使用某种服务时,使用@Service注解标记相关的类。该注解将类作为服务注册到Spring的上下文中,并按需注入到其他组件中。
@Component
@Component注解是Spring框架中最通用的注解之一,用于标识一个类作为组件。在使用@Component注解时,不需要指定具体的角色或职责,它可以表示不同层次的组件。除非有更准确的注解可用,否则可以使用@Component注解来代替
@Resource
@Resource=@Autowired+@Qualifier
如果接口实现只有一个,那么用@Autowired就可以
如果接口有多个实现,那么,用@Resource并指定name
或者使用@Autowired+@Qualifier 的value值
如果接口有多个实现
public class AdminServiceImpl implements UserService {
public User getById(int id) {
return null;
}
}
public class UserServiceImpl implements UserService {
public User getById(@PathVariable("id") int id)
{
User user=new User(id, "18","女");
return user;
}
}
--
@Autowired
@Qualifier("adminServiceImpl")
private UserService userService;
@Resource(name = "userServiceImpl")
private UserService userService1;
@Configuration+@Bean 人为注册
@Configuration
public class MyBeans {
@Bean
public UserService adminServiceImpl(){//要小写,对应AdminServiceImpl类
return new AdminServiceImpl();
}
@Bean
public UserService userServiceImpl(){
return new UserServiceImpl();
}
}
@Value
--配置文件中
#自定义
view.page=view
local.username=min20
--类中
@Value("${view.page}")
private String page;
@Value("${local.username}")
private String username;
本文作者:小羊不叉烧
本文链接:https://www.cnblogs.com/hereskiki/p/18669253
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步