spring boot server 구성
model
model/User.java
package com.example.springboot_vue_h2db.model;
import lombok.Data;
import javax.persistence.*;
@Entity
@Data
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
private int age;
private String ph;
private boolean active;
public User() {
}
public User(String name, int age, String ph){
this.name = name;
this.age = age;
this.ph = ph;
this.active = false;
}
}
repository
repo/UserRepository.java
package com.example.springboot_vue_h2db.repo;
import com.example.springboot_vue_h2db.model.User;
import org.springframework.data.repository.CrudRepository;
import java.util.List;
public interface UserRepository extends CrudRepository<User, Long> {
List<User> findByAge(int age);
}
controller
controller/UserController.java
package com.example.springboot_vue_h2db.controller;
import com.example.springboot_vue_h2db.model.User;
import com.example.springboot_vue_h2db.repo.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@CrossOrigin
@RestController
@RequestMapping("/_api")
public class UserController {
@Autowired
UserRepository repository;
@RequestMapping(method = RequestMethod.GET, value = "/userList")
public List<User> getAllUsers(){
List<User> users = new ArrayList<>();
repository.findAll().forEach(users::add);
return users;
}
@RequestMapping(method = RequestMethod.POST, value = "/user")
public User postUser(@RequestBody User user){
User _user = repository.save(new User(user.getName(), user.getAge(), user.getPh()));
return _user;
}
@RequestMapping(method = RequestMethod.DELETE, value = "/user/{id}")
public ResponseEntity<String> deleteUser(@PathVariable("id") long id){
repository.deleteById(id);
return new ResponseEntity<>(HttpStatus.OK);
}
@RequestMapping(method = RequestMethod.GET, value = "users/age/{age}")
public List<User> findByAge(@PathVariable int age) {
List<User> users = repository.findByAge(age);
return users;
}
@RequestMapping(method = RequestMethod.PUT, value = "/user/{id}")
public ResponseEntity<User> updateUser(@PathVariable("id") long id, @RequestBody User user){
Optional<User> userData = repository.findById(id);
if(userData.isPresent()){
User _user = userData.get();
_user.setName(user.getName());
_user.setAge(user.getAge());
_user.setPh(user.getPh());
_user.setActive(user.isActive());
return new ResponseEntity<>(repository.save(_user), HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.OK);
}
}
}
'개발 관련 지식 > SpringBoot-Vue' 카테고리의 다른 글
SpringBoot - Vue project 3. Vue 프로젝트 구성 (0) | 2020.03.27 |
---|---|
SpringBoot-Vue project 1 - 프로젝트 생성 (0) | 2020.03.24 |