h2 database spring boot
For explanation watch video:
Directory Structure
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.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.app</groupId>
<artifactId>SpringBootH2DB</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootH2DB</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties
#server port
server.port=3031
spring.h2.console.enabled=true
spring.jpa.show-sql=true
spring.datasource.url=jdbc:h2:mem:testdb
#http://localhost:3031/h2-console
SpringBootH2DbApplication
package com.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootH2DbApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootH2DbApplication.class, args);
}
}
Employee
package com.app.entity;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "emptab")
public class Employee {
@Id
@GeneratedValue
@Column(name = "eid")
private Integer empId;
@Column(name = "ename")
private String empName;
@Column(name = "esal")
private Double empSal;
private String dept;
public Employee(String empName, Double empSal, String dept) {
super();
this.empName = empName;
this.empSal = empSal;
this.dept = dept;
}
}
EmployeeRepository
package com.app.repo;
import org.springframework.data.jpa.repository.JpaRepository;
import com.app.entity.Employee;
public interface EmployeeRepository extends JpaRepository<Employee, Integer>{
}
TestRunner
package com.app.runner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import com.app.entity.Employee;
import com.app.repo.EmployeeRepository;
@Component
public class TestRunner implements CommandLineRunner{
@Autowired
private EmployeeRepository repo;
@Override
public void run(String... args) throws Exception {
//save the data
repo.save(new Employee("SAM",5000.0,"DEV"));
repo.save(new Employee("BROCK",6000.0,"QA"));
repo.save(new Employee("ANGELA",7000.0,"BA"));
repo.save(new Employee("Carla",8000.0,"DEV"));
repo.save(new Employee("Ram",9000.0,"QA"));
repo.save(new Employee("Shyam",10000.0,"BA"));
repo.save(new Employee("Raja",11000.0,"DEV"));
repo.save(new Employee("Rani",12000.0,"QA"));
repo.save(new Employee("Ramesh",13000.0,"BA"));
repo.save(new Employee("Sai",14000.0,"DEV"));
//print data
//repo.findAll().forEach(System.out::println);
System.out.println(repo.findById(3));
}
}
Comments
Post a Comment