SpringBoot2概览-基础篇

知识点


  • 基于阿里云创建SpringBoot项目:https://start.aliyun.com

  • SpringBoot配置文件加载顺序:application.properties > application.yml > application.yaml

  • Restful

    • 案例:
    @PostMapping("/users")  // 请求为http://localhost:8080/users(一般都采用复数)
    public String save(){...}
    @DeleteMapping("/users/{id}")
    public String delete(@PathVariable Integer id){...}  // 请求为http://localhost:8080/users/1
    @PutMapping("/users")
    public String update(@RequestBody User user){...}  // 请求为http://localhost:8080/users
    @GetMapping("/users/{id}")
    public String getById(@PathVariable Integer id){...}  // 请求为http://localhost:8080/users/1
    @GetMapping("/users")
    public String getAll(){...}  // 请求为http://localhost:8080/users
    
    • 注释区别:
注释作用应用
@RequestBody用于接收json数据发送请求参数超过1个时,或者以json格式为主时
@RequestParam接收url地址传参或表单传参发送非json格式数据
@PathVarible用于接收路径参数发送请求参数较少时,常用于传递Id值
  • 获取yml配置文件中的属性值:

    • 单一属性:
    // 在java文件中需要使用配置文件中属性的值
    @value("${name}")  
    private String name;
    @value("${server.port}")
    private String port;
    
    • 全部属性:
    // 把配置文件中的属性封装到Enviroment对象中,使用env.getProperty("xxx")获取到文件中定义所有属性的值
    @Autowired
    private Enviroment env;  
    
    • 变量引用:
    # 在yml文件中,一个属性需要引用另一个属性的值也是使用${xxx}
    base: \dir
      tempDir1: ${base}\t1  # 解析出来是\dir\demo1
      tempDir2: "${base}\t2"  # 解析出来是\dir 2,即把\t视为转义字符
    
    • 引用类型属性:
    # 下面为配置文件中的属性,现在想把这些属性封装为一个对象
    dataSource:
     driver: com.mysql.jdbc.Driver
    
    @Component // 定义为Spring管理的bean
    @ConfigurationProperties(prefix = "dataSource")  // 指定加载的数据
    public class MyDataSource{
        private String driver;
    }
    
    // 使用定义的MyDataSource
    @Autowired
    private MyDataSource dataSource;
    
  • SpringBooot中的分页操作:

// service层
public IPage<Book> getPage(int currentPage, int pageSize){
   IPage page = new Page(currnetPage, pageSize);
   bookDao.selectPage(page, null);
   return page;
}
// Controller层
@GetMapping("{currentPage}/{pageSize}")
public IPage<Book> getPage(@PathVarible int currentPage, @PathVarible int pageSize){
   return bookService.getPage(currentPage, pageSize); 
}
  • Controller层所有异常进行处理:
// 在Controller中写一个类,如果在其他Controller类中某个方法出现异常则会执行该方法,返回R对象,保持返回数据的一致性
@RestControllerAdvice
public class ProjectExceptionAdvice {
   //拦截所有的异常信息
   @ExceptionHandler(Exception.class)
   public R doException(Exception ex){
        //记录日志
       //通知运维
       //通知开发
       ex.printStackTrace();
       return new R("服务器故障,请稍后再试!");
   }
}

注意事项


  • MyBatis-Plus中进行条件查询时最好使用LambdaQueryWrapper,这样就无需担心属性写错:
String name = null; 
LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<>();
// 如果不加name != null,最后的SQL语句为select * from t1 where name like %null%,即解析为字符串
// 正确应该是select * from t1 
lqw.like(name != null, Book:getName, "xxx");  
bookDao.selectList(lqw);
  • 如果最后一页只有一条数据,删除后应该跳转到倒数第二页:
@GetMapping("{currentPage}/{pageSize}")
public R getPage(@PathVariable int currentPage,@PathVariable int pageSize,Book book){
  IPage<Book> page = bookService.getPage(currentPage, pageSize,book);
  // 如果当前页码值大于了总页码值,那么重新执行查询操作,使用最大页码值作为当前页码值
  if( currentPage > page.getPages()){
      page = bookService.getPage((int)page.getPages(), pageSize,book);
  }
  return new R(true, page);
}

资料



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值