springboot管理系统[基于员工角色和文件权限的分级的后台管理系统源码]
文章目录
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。


评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论