Wednesday, 7 October 2020

How to Run Springboot Database Application As a Java Standalone Application



CommandLineRunner Interface
 
Springboot Frameowrk provide import org.springframework.boot.CommandLineRunner interface which allow override Springboot Application run method in which you write code the interreact with database . 

Springboot Application contain follwoing java classes and pom.xml

package in.jk.springbootapplication.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="springboot.java_developer")
public class JavaDeveloper {
@Id
@Column(name="java_developer_id")
private Integer javaDeveloperId;
@Column(name="name")
private String name;
@Column(name="company")
private String comapny;
      //Getters and Setters
        @Override
public String toString() {
return "JavaDeveloper [javaDeveloperId=" + javaDeveloperId + ", name=" + name + ", comapny=" + comapny + "]";
}
}

JavaRepositry.java

package in.jk.springbootapplication.repository;

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

import in.jk.springbootapplication.entity.JavaDeveloper;

public interface JavaRepositry extends JpaRepository<JavaDeveloper, Integer> {

}

JavaService.java

package in.jk.springbootapplication.service;

import in.jk.springbootapplication.entity.JavaDeveloper;

public interface JavaService {
public JavaDeveloper save(JavaDeveloper javaDeveloper);
public JavaDeveloper findById(Integer javaDeveloperId);

}

JavaServiceImpl.java
package in.jk.springbootapplication.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import in.jk.springbootapplication.entity.JavaDeveloper;
import in.jk.springbootapplication.repository.JavaRepositry;
import in.jk.springbootapplication.service.JavaService;

@Service
public class JavaServiceImpl implements JavaService {

@Autowired
private JavaRepositry javaRepositry;
@Override
public JavaDeveloper save(JavaDeveloper javaDeveloper) {
// TODO Auto-generated method stub
return javaRepositry.save(javaDeveloper);
}

@Override
public JavaDeveloper findById(Integer javaDeveloperId) {
// TODO Auto-generated method stub
return javaRepositry.findById(javaDeveloperId).get();
}

}

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>in.jk</groupId>
<artifactId>springbootapplication</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootapplication</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--  Jackson Dependency for json conversion -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>     
</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>

<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

</project>

SpringbootDatabaseApplication.java

package in.jk.springbootapplication;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import in.jk.springbootapplication.entity.JavaDeveloper;
import in.jk.springbootapplication.service.JavaService;

@SpringBootApplication
public class SpringbootDatabaseApplication implements CommandLineRunner {

@Autowired
private JavaService javaService;

public static void main(String[] args) {
System.out.println("Springboot Database Application ....\n");
        SpringApplication.run(SpringbootDatabaseApplication.class, args);

}

@Override
public void run(String... args) throws Exception {

JavaDeveloper javaDeveloper = new JavaDeveloper();
javaDeveloper.setJavaDeveloperId(100000);
javaDeveloper.setName("J K");
javaDeveloper.setComapny("Google");

javaService.save(javaDeveloper);
                System.out.println("Saved Succussfully ..");
JavaDeveloper javaDeveloper1 = javaService.findById(10000);
System.out.println("JavaDeveloper1 :: "+javaDeveloper1);

}

}

Output in Console .....

Hibernate: select javadevelo0_.java_developer_id as java_dev1_1_0_, javadevelo0_.company as company2_1_0_, javadevelo0_.name as name3_1_0_ from springboot_java_developer javadevelo0_ where javadevelo0_.java_developer_id=?
Hibernate: insert into springboot_java_developer (company, name, java_developer_id) values (?, ?, ?)
Saved Succussfully ..
Hibernate: select javadevelo0_.java_developer_id as java_dev1_1_0_, javadevelo0_.company as company2_1_0_, javadevelo0_.name as name3_1_0_ from springboot_java_developer javadevelo0_ where javadevelo0_.java_developer_id=?
JavaDeveloper1 :: JavaDeveloper [javaDeveloperId=10000, name=J K, comapny=Google]
  




No comments:

Post a Comment