Java/SpringBoot

[Spring] JPA Select

고기고기물고기 2022. 10. 17. 11:24

 

해당 소스는 JPA로 기본 SELECT 하는 예제를 구현하는 예제이다.

 

 

Entity

@Data
@Entity
@Table(name = "DeptMaster")
public class DeptMAsterEntity {

	@Column(nullable = false, length 3, unique = true)
    @Id
    private String serviceDept;
    @Column(length = 100)
    private String deptName;
}

 

controller

public class AdminMasterController {

    @Autowired
    private AdminMasterService service;

    @CrossOrigin(origins = "*")
    @GetMapping("/deptSelect")
    public List<DeptMasterEntity> deptSelect() {
    
    List<DeptMasterEntity> deptMasterList = new ArrayList<>();
    deptMasterList = service.getDeptMasterList();
    
    return deptMasterList

    }

}

 

Repository

 

public interface DeptMasterRepository extends CrudRepository<DeptMasterEntity, Long> {

}

 

Service

 

public interface AdminMasterService {

	public List<DeptMasterEntity> getDeptMasterList();

}

 

@Service
public class AdminMasterServiceImpl implements AdminMasterService {

	@Autowired
    private DeptMasterRepository deptMaster;
    
    @Override
    public List<DeptMasterEntity> getDeptMasterList() {
    	return (List<DeptMasterEntity>) deptMaster.findAll();
    
    }

}

 

 

작성 후 localhost:(포트)/deptSelect 브라우저에서 URL 입력시 JSON 데이터 출력이 된다!!