Showing posts with label JPA. Show all posts
Showing posts with label JPA. Show all posts

Saturday, February 10, 2018

How to connect Spring Boot to Mysql using JPA SOLVED


application.properties

  
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?autoReconnect=true
spring.datasource.username=springboot
spring.datasource.password=springboot
spring.jpa.generate-ddl=true

  
  


Student Entity in the Model


  package com.example.demo.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;



@Entity
@Table(name="student")
public class Student implements Serializable{
 
 private static final long serialVersionUID = -6899110682583120414L;

 @Id
 
    @Column(name="Stid")
 private int Stid;
 
    @Column(name="Name")
 private String Name;
    
    @Column(name="Programme")
 private String Programme;
 
 public Student() {
  //super();
 }

 
 public Student(int stid, String name, String programme) {
  super();
  Stid = stid;
  Name = name;
  Programme = programme;
 }

 

 @Override
 public String toString() {
  return "Student [Stid=" + Stid + ", Name=" + Name + ", Programme=" + Programme + "]";
 }
 
    
}

  


Student Repository


package com.example.demo.repository;

import org.springframework.data.repository.CrudRepository;

import com.example.demo.model.Student;

public interface StudentRepository extends CrudRepository {
   
}
  


Student Controller


   package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.model.Student;
import com.example.demo.repository.StudentRepository;

@RestController
public class StudentController {

 @Autowired
 StudentRepository repository;
 
 @RequestMapping("/findall")
  @ResponseBody
 public String findall(){
  String result = "";
          for(Student student:repository.findAll()){
        result += " --- " +student.toString();
       }
 return result  ;
 }
 
 @RequestMapping("/findbystid")
  @ResponseBody
 public String findByStid(@RequestParam("stid") int stid){
  String result="";
           result=repository.findOne(stid).toString();
 return result  ;
 }
 
 @RequestMapping("/register")
 String register(Student student) {
  repository.save(new Student(10,"JPA","JPA Tut"));
  return "Student Registered";
 }

 
 
}
  


Main Class


package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloJpaMySql1Application {

 public static void main(String[] args) {
  SpringApplication.run(HelloJpaMySql1Application.class, args);
 }
}

  


This is just a trick, hope it paved your way out.