教程 > spring boot orm > 阅读:27

spring boot orm 更新项目——迹忆客-ag捕鱼王app官网

现在让我们向 spring 应用程序添加一个 rest api,它可以添加、编辑、删除和显示员工。

entity − employee.java

package com.spring.springbootorm.entity;
import javax.persistence.entity;
import javax.persistence.generatedvalue;
import javax.persistence.generationtype;
import javax.persistence.id;
@entity
public class employee {
   @id
   @generatedvalue(strategy = generationtype.identity)
   private int id;
   private string name;
   private int age;
   private string email;
   public int getid() {
      return id;
   }
   public void setid(int id) {
      this.id = id;
   }
   public string getname() {
      return name;
   }
   public void setname(string name) {
      this.name = name;
   }
   public int getage() {
      return age;
   }
   public void setage(int age) {
      this.age = age;
   }
   public string getemail() {
      return email;
   }
   public void setemail(string email) {
      this.email = email;
   }
}

repository − employeerepository.java

package com.spring.springbootorm.repository;
import org.springframework.data.repository.crudrepository;
import org.springframework.stereotype.repository;
import com.tutorialspoint.springbootorm.entity.employee;
@repository
public interface employeerepository extends crudrepository  {
}

service − employeeservice.java

package com.spring.springbootorm.service;
import java.util.arraylist;
import java.util.list;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;
import com.tutorialspoint.springbootorm.entity.employee;
import com.tutorialspoint.springbootorm.repository.employeerepository;
@service
public class employeeservice {
   @autowired
   employeerepository repository;
   public employee getemployeebyid(int id) {
      return repository.findbyid(id).get();
   }
   public list getallemployees(){
      list employees = new arraylist();
      repository.findall().foreach(employee -> employees.add(employee));
      return employees;
   }
   public void saveorupdate(employee employee) {
      repository.save(employee);
   }
   public void deleteemployeebyid(int id) {
      repository.deletebyid(id);
   }
}

控制器 - employeecontroller.java

package com.spring.springbootorm.controller;
import java.util.list;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.deletemapping;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.pathvariable;
import org.springframework.web.bind.annotation.postmapping;
import org.springframework.web.bind.annotation.putmapping;
import org.springframework.web.bind.annotation.requestbody;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
import com.tutorialspoint.springbootorm.entity.employee;
import com.tutorialspoint.springbootorm.service.employeeservice;
@restcontroller
@requestmapping(path = "/emp")
public class employeecontroller {
   @autowired
   employeeservice employeeservice;
   @getmapping("/employees")
   public list getallemployees(){
      return employeeservice.getallemployees();
   }
   @getmapping("/employee/{id}")
   public employee getemployee(@pathvariable("id") int id) {
      return employeeservice.getemployeebyid(id);
   }
   @deletemapping("/employee/{id}")
   public void deleteemployee(@pathvariable("id") int id) {
      employeeservice.deleteemployeebyid(id);
   }
   @postmapping("/employee")
   public void addemployee(@requestbody employee employee) {
      employeeservice.saveorupdate(employee);
   }
   @putmapping("/employee")
   public void updateemployee(@requestbody employee employee) {
      employeeservice.saveorupdate(employee);
   }    
}

springbootormapplication.java

package com.spring.springbootorm;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
@springbootapplication
public class springbootormapplication {
   public static void main(string[] args) {
      springapplication.run(springbootormapplication.class, args);
   }
}

查看笔记

扫码一下
查看教程更方便
网站地图