Posts

Install Doker and install MySql inside Docker image container

Image
  After Install and Launched Docker than Run  > docker version  now Install Launch MySql as Docker Container by using given Below Command  Break Down the Command 1. docker run -- detach 2. -- env MYSQL_ROOT_PASSWORD = dummypassword 3. -- env MYSQL_USER = social - media - user 4. -- env MYSQL_PASSWORD = dummypassword 5. -- env MYSQL_DATABASE = social - media - database 6. -- name mysql 7. -- publish 3306 : 3306 8. mysql : 8 - oracle docker run -- detach -- env MYSQL_ROOT_PASSWORD = dummypassword -- env MYSQL_USER = social - media - user -- env MYSQL_PASSWORD = dummypassword -- env MYSQL_DATABASE = social - media - database -- name mysql -- publish 3306 : 3306 mysql : 8 - oracle Now we have My Sql container up and Running Docker Desktop App Now Connect this MySql to your Spring Boot Application application.properties spring.jpa.show-sql= true spring.datasource.url= jdbc : mysql :// localhost :3306/social-media-database spring.datasource.username= social-media-user...

JDBC VS Spring JDBC VS JPA VS Spring Data JPA

 JDBC: write lot of sql queries(delete form table where id=?) And write lot of java code as well Spring JDBC: write lot of sql queries(delete form table where id=?) But Lesser Java Code JPA: Do not Wory about the Queries Just Map Enties to tables Spring Data JPA: Lets Make JPA even more simple Spring Data JPA will take care of everything Differance between Hibernate and JPA JPA Defines a Specification. JPA is an API JPA is Like Interface JPA define how you can define an entities By Using an @Entity Annotation to define any class as Entity or as table mapping By Using an @Id Annotation to define primary key By Using an @Column Annocation to define attribute of the class Hibernate is one of the popular implementation of JPA we can use JPA rather than to lock in to hibernate or other implementation of JPA like toplink etc

Getting Started with JPA and EntityManager

 so with the help of Query in previous tutorial were easy but over the period of time while we working on some complex query than it would be difficult to maintain the flow of query and understanding would be difficult than  So Thats where JPA users have different apporoach instead of Course Bean class we create Entity class with @Entity Annotation which will make that calss to Eneity Class Step 1 @Entity public class Course { @Id private long id ; @Column (name= "name" ) private String name ; @Column (name= "author" ) private String author ; // constructor, getter/setter, toString } Step 2 @Component public class CourseCommandLineRunner implements CommandLineRunner { @Autowired CourseJPARepository courseJPARepository ; @Override public void run(String... args ) throws Exception { courseJPARepository .insert( new Course(1, "Learn Java" , "Khan Dev" )); courseJPARepository .insert( new Course(2, ...

Select or Retrieve data using Spring JDBC Template

Image
 Step 1  package com.in28min.springboot.learnjpaandhibernate.course; public class Course { private long id ; private String name ; private String author ; public Course() { super (); } public Course( long id , String name , String author ) { super (); this . id = id ; this . name = name ; this . author = author ; } public long getId() { return id ; } public String getName() { return name ; } public String getAuthor() { return author ; } public void setId( long id ) { this . id = id ; } public void setName(String name ) { this . name = name ; } public void setAuthor(String author ) { this . author = author ; } @Override public String toString() { return "Course [id=" + id + ", name=" + name + ", author=" + author + "]" ; } } Step 2 package com.in28min.springboot.learnjpaandhibernate.course.jdbc; import org.springframework.beans.factor...

Insert Data using Object and Delete row using Id

Image
 Step 1: public class Course { private long id ; private String name ; private String author ; public Course() { super (); } public Course( long id , String name , String author ) { super (); this . id = id ; this . name = name ; this . author = author ; } public long getId() { return id ; } public String getName() { return name ; } public String getAuthor() { return author ; } @ Override public String toString() { return "Course [id=" + id + ", name=" + name + ", author=" + author + "]" ; } } Step 2: public class CourseJdbcRepository { @ Autowired private JdbcTemplate springjdbcTemplate ; //""" will be known as text block //(this allows us to retain the formatting for your queries which allow to easy to understand) private static String INSERT_QUERY_WITH_PARAM = """ insert into course (id,name,author) va...

Insert Data using Spring Command line interace (while initialize your application first time)

 Step 1:  Create  CourseJdbcRepository.java to insert your dummy hardcoded data into DB @Repository public class CourseJdbcRepository { @Autowired private JdbcTemplate springjdbcTemplate ; //""" will be known as text block //(this allows us to retain the formatting for your queries which allow to easy to understand) private static String INSERT_QUERY = """ insert into course (id,name,author) values(1,'Learn Java', 'khanDev') """ ; public void insert() { springjdbcTemplate .update( INSERT_QUERY ); } } Step 2: CourseJdbcCommandLineRunner.java to call the insert() method while first time load the application @Component public class CourseJdbcCommandLineRunner implements CommandLineRunner { @ Autowired CourseJdbcRepository courseJdbcRepository ; @Override public void run(String... args ) throws Exception { courseJdbcRepository .insert(); } }

Learn JPA and Hibernate

Image
https://start.spring.io/ in given above URL can   User Given Below Configuration to Create Spring Boot Project with given dependencies to enable JPA and Hibernate Spring Web Spring Data JDBC Spring Data JPA H2 Database < dependencies >      < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-data-jdbc</ artifactId >      </ dependency >      < 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...