Я пытаюсь разработать небольшой API PO C при весенней загрузке, но при последовательном получении таблицы не существует ошибки. Я считаю, что hibernate автоматически создает таблицу в базе данных.
вот мой код ...... .................................................. ......
Приложение. java
package FirstPackage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
}
}
........................ .................................................. ..............
MainController. java
package FirstPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller //This refers as this is main controller class
@RequestMapping("/main") //This means request will be mapping on localhost:8081/main
public class MainController {
@Autowired //This means Spring will take care of this object creation
private StudentRepository studentRepository;
@PostMapping(path = "/add")
public @ResponseBody String addNewStudent(@RequestParam String name)/*, @RequestParam Integer roll_Number, @RequestParam String standerd)*/
{
Student s = new Student();
s.setName(name);
s.setRoll_Number(101);
s.setStanderd("I");
studentRepository.save(s);
return "Data Entered";
}
@GetMapping(path = "/all")
public Iterable<Student> getAllStudent()
{
return studentRepository.findAll();
}
}
................ .................................................. .........................
Студент. java
package FirstPackage;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/*this is going to be entity Model this tell Hibernate to make a table out of it */
@Entity
@Table(name = "students_table")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private Integer roll_Number;
private String standerd;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getRoll_Number() {
return roll_Number;
}
public void setRoll_Number(Integer roll_Number) {
this.roll_Number = roll_Number;
}
public String getStanderd() {
return standerd;
}
public void setStanderd(String standerd) {
this.standerd = standerd;
}
}
..... .................................................. ..............................
package FirstPackage;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import FirstPackage.Student;
//This will be AUTO IMPLEMENTED by Spring into a Bean called StudentRepository
//CRUD refers Create, Read, Update, Delete
@Repository
public interface StudentRepository extends CrudRepository<Student, Integer>
{
}
........... .................................................. ......................... application.properties
spring.jpa.hibernate.dll-auto = create
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3303/db_student
spring.datasource.username=springuser
spring.datasource.password=Root@1234