addheader

Tuesday, 7 November 2017

spring boot jpa

spring boot jpa example:

    JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    HIBERNATE-ANNOTATIONS     SPRING-IOC       SPRING –AOP-SCHEMABASED   

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY

     SPRING-DATA-JPA     REST-WEB-SERVICE     STRUTS2HIBERNATE    GWT.... 

poject structure:





Application.properties

spring.datasource.url= jdbc:mysql://localhost:3306/mysql
spring.datasource.username=root
spring.datasource.password=mysql1
server.port=8484
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=true

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
       <modelVersion>4.0.0</modelVersion>

       <groupId>com.example</groupId>
       <artifactId>mysqljpaex</artifactId>
       <version>0.0.1-SNAPSHOT</version>
       <packaging>jar</packaging>

       <name>mysqljpaex</name>
       <description>Demo project for Spring Boot</description>

       <parent>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-parent</artifactId>
             <version>1.5.8.RELEASE</version>
             <relativePath/> <!-- lookup parent from repository -->
       </parent>

       <properties>
             <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
             <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
             <java.version>1.8</java.version>
       </properties>

       <dependencies>
             <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-jdbc</artifactId>
             </dependency>
                    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <version>1.5.6.RELEASE</version>
</dependency>
             <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
             </dependency>

             <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <scope>runtime</scope>
             </dependency>
             <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-test</artifactId>
                    <scope>test</scope>
             </dependency>
       </dependencies>

       <build>
             <plugins>
                    <plugin>
                           <groupId>org.springframework.boot</groupId>
                           <artifactId>spring-boot-maven-plugin</artifactId>
                    </plugin>
             </plugins>
       </build>


</project>

JdbcTestAPI.java


package com.example.demo;

import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class JdbcTestAPI {
       @Autowired
       JdbcTemplate jdbcTemplate;
      
       @Autowired
       StudentRepository studentRepository;

       @GetMapping("/getDataBaseName")
public String getDatabasename() {
       try {
             Student student=new Student();
             student.setId(55);
             student.setFname("adhav");
             student.setLname("goddati");
             student.setEmail("abc@gmail.cm");
             studentRepository.save(student);
            
             //jdbcTemplate.update("insert into student values(2,'madhav','ro','abc@gmail.com')");
             return jdbcTemplate.getDataSource().getConnection().getMetaData().getDatabaseProductName();
       } catch (SQLException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
       }
       return null;
}

}

Student.java

package com.example.demo;

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

@Entity
@Table(name = "student")

public class Student {
               @Id
               @Column(name = "id")
private int id;
               @Column(name = "firstname")
private String fname;
               @Column(name = "lastname")
private String lname;
               @Column(name = "email")
private String email;
public int getId() {
               return id;
}
public void setId(int id) {
               this.id = id;
}
public String getFname() {
               return fname;
}
public void setFname(String fname) {
               this.fname = fname;
}
public String getLname() {
               return lname;
}
public void setLname(String lname) {
               this.lname = lname;
}
public String getEmail() {
               return email;
}
public void setEmail(String email) {
               this.email = email;
}


}

StudentRepository.java

package com.example.demo;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

public interface StudentRepository extends JpaRepository<Student, Long>,
JpaSpecificationExecutor<Student>{

}



TUTIORALS FROM MADHAV:

     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    HIBERNATE-ANNOTATIONS     SPRING-IOC       SPRING –AOP-SCHEMABASED   

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC    

     SPRING-DATA-JPA     REST-WEB-SERVICE     STRUTS2HIBERNATE    GWT....