Reputation: 1
I used mysql and created employeemanager table.
That's the content of each file :
EmployeeManagerApplication.java :
package tn.poulina.EmployeeManager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/*@ComponentScan(basePackages={"tn.poulina.EmployeeManager.model"} )*/
@SpringBootApplication
public class EmployeeManagerApplication {
public static void main(String[] args) {
SpringApplication.run(EmployeeManagerApplication.class, args);
}
}
Employee.java :
package tn.poulina.EmployeeManager.model;
import java.io.Serializable;
import javax.persistence.*;
@Entity
public class Employee implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(nullable = false,updatable = false)
private Long id;
private String name;
private String email;
private String jobTitle;
private String phone;
private String imageUrl;
@Column(nullable = false,updatable = false)
private String employeeCode;
public Employee()
{}
public Employee(String name,String email,String jobTitle ,String phone ,String imageUrl,String
employeeCode)
{
this.name=name;
this.email=email;
this.jobTitle=jobTitle;
this.phone=phone;
this.imageUrl=imageUrl;
this.employeeCode=employeeCode;
}
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id=id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name=name;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email=email;
}
public String getJobTitle()
{
return jobTitle;
}
public void setJobTitle(String jobTitle)
{
this.jobTitle=jobTitle;
}
public String getPhone()
{
return phone;
}
public void setPhone(String phone)
{
this.phone=phone;
}
public String getImageUrl()
{
return imageUrl;
}
public void setImageUrl(String imageUrl)
{
this.imageUrl=imageUrl;
}
public String getEmployeeCode()
{
return employeeCode;
}
public void setEmployeeCode(String employeeCode)
{
this.employeeCode=employeeCode;
}
@Override
public String toString(){
return "Employee{"+"id="+id+"/name="+name+"/email="+email+"/jobTitle="+jobTitle+"/phone="+phone+"/imageUrl="+imageUrl+"}";
}
application.properties :
# MySQL Configuration
spring.datasource.url=jdbc:mysql://localhost:3306/employeemanager
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
while running ,it gets stuck in here
i tried typing localhost:8080 on browser and it shows:
i have also tried @ComponentScan(basePackages={"tn.poulina.EmployeeManager.model"} ) in 'EmployeeManagerApplication.java ' and tried moving 'EmployeeManagerApplication.java' to package 'model' but the result is always the same .
Upvotes: 0
Views: 914
Reputation: 56
From your project structure, you only have model class. You need to add Controller class with handler method that will handle "/" path. To add more:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
you can then create repository interface as:
public interface EmployeeRepository extends JpaRepository<Employee,Long> {}
@Service
public class EmployeeService{
private final EmployeeRepository empRepo;
public EmployeeService(EmployeeRepository empRepo) {
this.empRepo = empRepo;
}
public List<Employee> getAllEmp(){
return empRepo.findAll();
}
//there could many other methods.....
}
@RestController
public class EmployeeController {
private final EmployeeService empServ;
public EmployeeController(final EmployeeService empServ){
this.empServ = empServ;
}
@GetMapping("/")
public List<Employee> getAllEmployees(){
return empServ.getAllEmp();
}
}
Then you will see something if you have it in your database.
Upvotes: 1