关于springboot:springboot管理系统基于员工角色和文件权限的分级的后台管理系统源码

7次阅读

共计 3510 个字符,预计需要花费 9 分钟才能阅读完成。

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。

正文完
 0