springboot管理系统是一种实现JPA存储库的办法,能够轻松地在应用程序中增加数据拜访层。CRUD代表创立、检索、更新、删除,这些都是能够在数据库中执行的操作。在本文中,咱们将看到一个示例,阐明如何应用spring数据JPA构建基于员工角色和文件权限的分级的后盾管理系统。
  
  源码:s.ymzan.top
  
  数据库是互相关联的数据的汇合,它有助于高效地从数据库中检索、插入和删除数据,并以表、视图、模式、报告等模式组织数据。因而,对于任何应用程序,数据库都是最重要的模块之一,须要有一种与之通信的形式。因而,为了应用Spring构架欠缺的后盾权限管理系统,须要遵循以下步骤:
  
  一、去spring initializr 并创立一个新的我的项目,其依赖关系如下:
  
  ●Spring Web
  
  ●Spring Data JPA
  
  ●MySQL Driver
  
  二、下载starter我的项目并将其导入IDE中。
  
  三、在我的项目同步之后,咱们将创立一个模型类公司与正文@ entity这意味着这个类映射到数据库中的表。增加数据类型与数据库中列雷同的数据成员,并生成构造函数和getter。增加正文@ id指向数据成员,该成员将在表和中充当主键属性@Generatedvalue(策略= generationtype.auto)以便主动减少主键属性。上面是这个类的实现:
  
  @Entity
  
  public class Company {
  
  // Primary ID which increments
  
  // automatically when new entry
  
  // is added into the database
  
  @Id
  
  @GeneratedValue(strategy
  
  = GenerationType.AUTO)
  
  int id;
  
  String name;
  
  // In months
  
  int duration;
  
  String profile;
  
  // Can be 0
  
  int stipend;
  
  boolean workFromHome;
  
  public Company()
  
  {
  
  }
  
  // Parameterized constructor
  
  public Company(String name, int duration,
  
  String profile,
  
  int stipend,
  
  boolean workFromHome)
  
  {
  
  this.name = name;
  
  this.duration = duration;
  
  this.profile = profile;
  
  this.stipend = stipend;
  
  this.workFromHome = workFromHome;
  
  }
  
  // Getters and setters of
  
  // the variables
  
  public int getId()
  
  {
  
  return id;
  
  }
  
  public String getName()
  
  {
  
  return name;
  
  }
  
  public int getDuration()
  
  {
  
  return duration;
  
  }
  
  public String getProfile()
  
  {
  
  return profile;
  
  }
  
  public int getStipend()
  
  {
  
  return stipend;
  
  }
  
  public void setId(int id)
  
  {
  
  this.id = id;
  
  }
  
  public boolean isWorkFromHome()
  
  {
  
  return workFromHome;
  
  }
  
  四、当初,创立一个接口 CompanyRepository与正文@这将实现CrudRepository. 执行CRUD操作的函数将在接口中定义,如下所示:
  
  @Repository
  
  public interface CompanyRepository
  
  extends CrudRepository<Company,
  
  Integer> {
  
  Company findById(int id);
  
  List<Company> findAll();
  
  void deleteById(int id);
  
  }
  
  留神:这些函数不会被实现,因为它们曾经在CrudRepository中实现了。
  
  五、当初,咱们将创立如下所示的REST api (GET, POST, PUT, DELETE):
  
  @RestController
  
  public class CompanyController {
  
  @Autowired
  
  private CompanyRepository repo;
  
  // Home Page
  
  @GetMapping("/")
  
  public String welcome()
  
  {
  
  return "<html><body>"
  
  + "<h1>WELCOME</h1>"
  
  + "</body></html>";
  
  }
  
  // Get All Notes
  
  @GetMapping("/company")
  
  public List<Company> getAllNotes()
  
  {
  
  return repo.findAll();
  
  }
  
  // Get the company details by
  
  // ID
  
  @GetMapping("/company/{id}")
  
  public Company getCompanyById(
  
  @PathVariable(value = "id") int id)
  
  {
  
  return repo.findById(id);
  
  }
  
  @PostMapping("/company")
  
  @ResponseStatus(HttpStatus.CREATED)
  
  public Company addCompany(
  
  @RequestBody Company company)
  
  {
  
  return repo.save(company);
  
  }
  
  @DeleteMapping("/delete/{id}")
  
  public void deleteStudent(
  
  @PathVariable(value = "id") int id)
  
  {
  
  repo.deleteById(id);
  
  }
  
  @PutMapping("/company/{id}")
  
  public ResponseEntity<Object> updateStudent(
  
  @RequestBody Company company,
  
  @PathVariable int id)
  
  {
  
  Optional<Company> companyRepo
  
  = Optional.ofNullable(
  
  repo.findById(id));
  
  if (!companyRepo.isPresent())
  
  return ResponseEntity
  
  .notFound()
  
  .build();
  
  company.setId(id);
  
  repo.save(company);
  
  return ResponseEntity
  
  .noContent()
  
  .build();
  
  }
  
  六、在管理系统后盾并增加以下代码。取代database_name与蕴含表的数据库公司,用户名mysql服务器的用户名(默认是root)和明码应用mysql明码。
  
  spring.datasource.url=jdbc:mysql://localhost:3306/database_name
  
  spring.datasource.username=username
  
  spring.datasource.password=password
  
  spring.jpa.hibernate.ddl-auto=update
  
  七、这就实现了与数据库建设连贯的过程。当初,咱们构建和运行我的项目并调用不同的api。