springboot注解加深

以标签模块为例

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import util.IdWorker;
//这里是一个启动类
@SpringBootApplication
public class BaseApplication {
    public static void main(String[]args){
        SpringApplication.run(BaseApplication.class, args);
    }

    @Bean
    public IdWorker idWorker(){
        return new IdWorker();
    }
}

包结构

总体来说比以前那种模式来说更简单了没有了实现类,

一、先从pojo来说

作用在实体类上的注解有 @Entity   @Table(name = "表名") 这两个 如果是多个服务器之前就要实现 Serializable 在io流中传输

作用在属性上主键 id 的 注解有  @Id  其他的正常生成构造函数和getset方法还有tostring方法

二、controller层

作用在类上的注解有

@RestController

@RestController的作用就相当于@Controller+@ResponseBody的结合体 ,ResponseBody可以将响应的 json转换成实体

@CrossOrigin 跨域

@RequestMapping("/label") 一个总的请求地址

直接贴代码简单的增删改查

package com.tensquare.base.controller;

import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelSerivce;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/label")
public class LabelController {
    @Autowired
    private LabelSerivce labelSerivce;
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll(){
        return new Result(true, StatusCode.OK,"查询成功",labelSerivce.findAll());
    }
    @RequestMapping(value = "/{labelId}",method = RequestMethod.GET)
    public  Result findById(@PathVariable("labelId")String id){
        return new Result(true, StatusCode.OK,"查询成功",labelSerivce.findById(id));
    }
   //RequestBody注解是将 前端的json数据转换成实体 @RequestMapping(method
= RequestMethod.POST) public Result save(@RequestBody Label label){ labelSerivce.save(label); return new Result(true, StatusCode.OK,"添加成功"); } @RequestMapping(value = "/{labelId}",method = RequestMethod.PUT) public Result update(@RequestBody Label label,@PathVariable("labelId") String id){ label.setId(id); labelSerivce.update(label); return new Result(true, StatusCode.OK,"更新成功"); }
  //@PathVariable是用来对指定请求的URL路径里面的变量 
  //
{labelId}在这个请求的URL里就是个变量,可以使用@PathVariable来获取
  //@PathVariable和@RequestParam的区别就在于:@RequestParam用来获得静态的URL请求参数;@PathVariable用来获得动态的URL请求入参
    @RequestMapping(value = "/{labelId}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable("labelId") String id){
        labelSerivce.deleteById(id);
        return new Result(true, StatusCode.OK,"删除成功");
    }

}

 三、service层代码

package com.tensquare.base.service;

import com.tensquare.base.dao.LabelDao;
import com.tensquare.base.pojo.Label;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;
import java.util.List;

@Service
@Transactional
public class LabelSerivce {
    @Autowired
    private LabelDao labelDao;
    @Autowired
    private IdWorker idWorker;
    public List<Label> findAll(){
        return labelDao.findAll();
    }
    public Label findById(String id){
        return labelDao.findById(id).get();
    }
    public void save (Label label){
        label.setId(idWorker.nextId()+"");
        labelDao.save(label);
    }
    public void update (Label label){
        labelDao.save(label);
    }
    public void deleteById(String  id){
        labelDao.deleteById(id);
    }
}

四、dao层代码

package com.tensquare.base.dao;

import com.tensquare.base.pojo.Label;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

/**
 * <Lable,String> 第一个参数是lable实体,第二个参数是id 更新删除查找要用到
 * 这里继承的JpaSpecificationExecutor用来分页等复杂操作
 */
public interface LabelDao extends JpaRepository <Label,String>,JpaSpecificationExecutor<Label>{
}

五、配置文件yml文件   application.yml

server:
  port: 9001
spring:
  application:
    name: tensquare-base
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/tensquare_base?characterEncoding=utf-8&useSSL=false
    password: root
    username: root

  jpa:
    database: mysql
    show-sql: true




 

原文地址:https://www.cnblogs.com/inbeijing/p/10268209.html