<?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.dineshonjava.sdjpa</groupId>
<artifactId>SpringBootJPASpringData</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringBootJPASpringData</name>
<description>SpringBootJPASpringData project for Spring Boot with Spring Data JPA implementation</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.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-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>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>
# DataSource settings: set here your own configurations for the database # connection. In this example we have "dojsb" as database name and # "root" as username and password. spring.datasource.url = jdbc:mysql://localhost:3307/dojdb spring.datasource.username = root spring.datasource.password = root # Keep the connection alive if idle for a long time (needed in production) spring.datasource.testWhileIdle = true spring.datasource.validationQuery = SELECT 1 # Show or not log for each sql query spring.jpa.show-sql = true # Hibernate ddl auto (create, create-drop, update) spring.jpa.hibernate.ddl-auto = create # Naming strategy spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy # Use spring.jpa.properties.* for Hibernate native properties (the prefix is # stripped before adding them to the entity manager) # The SQL dialect makes Hibernate generate better SQL for the chosen database spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect server.port = 8181
/**
*
*/
package com.dineshonjava.sdjpa.models;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* @author Dinesh.Rajput
*
*/
@Entity
@Table(name = "BOOKING")
public class Booking implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long bookingId;
@Column
String psngrName;
@Column
String departure;
@Column
String destination;
@Column
Date travelDate;
public Long getBookingId() {
return bookingId;
}
public void setBookingId(Long bookingId) {
this.bookingId = bookingId;
}
public String getPsngrName() {
return psngrName;
}
public void setPsngrName(String psngrName) {
this.psngrName = psngrName;
}
public String getDeparture() {
return departure;
}
public void setDeparture(String departure) {
this.departure = departure;
}
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
public Date getTravelDate() {
return travelDate;
}
public void setTravelDate(Date travelDate) {
this.travelDate = travelDate;
}
}
package com.dineshonjava.sdjpa.models; import org.springframework.data.repository.CrudRepository; import org.springframework.transaction.annotation.Transactional; @Transactional public interface BookingRepository extends CrudRepository{ /** * This method will find an Boooking instance in the database by its departure. * Note that this method is not implemented and its working code will be * automatically generated from its signature by Spring Data JPA. */ public Booking findByDeparture(String departure); }
/**
*
*/
package com.dineshonjava.sdjpa.controller;
import java.util.Date;
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.RestController;
import com.dineshonjava.sdjpa.models.Booking;
import com.dineshonjava.sdjpa.models.BookingRepository;
/**
* @author Dinesh.Rajput
*
*/
@RestController
@RequestMapping("/booking")
public class BookingController {
@Autowired
BookingRepository bookingRepository;
/**
* GET /create --> Create a new booking and save it in the database.
*/
@RequestMapping("/create")
public Booking create(Booking booking) {
booking.setTravelDate(new Date());
booking = bookingRepository.save(booking);
return booking;
}
/**
* GET /read --> Read a booking by booking id from the database.
*/
@RequestMapping("/read")
public Booking read(@RequestParam Long bookingId) {
Booking booking = bookingRepository.findOne(bookingId);
return booking;
}
/**
* GET /update --> Update a booking record and save it in the database.
*/
@RequestMapping("/update")
public Booking update(@RequestParam Long bookingId, @RequestParam String psngrName) {
Booking booking = bookingRepository.findOne(bookingId);
booking.setPsngrName(psngrName);
booking = bookingRepository.save(booking);
return booking;
}
/**
* GET /delete --> Delete a booking from the database.
*/
@RequestMapping("/delete")
public String delete(@RequestParam Long bookingId) {
bookingRepository.delete(bookingId);
return "booking #"+bookingId+" deleted successfully";
}
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootJpaSpringDataApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootJpaSpringDataApplication.class, args);
}
}

Labels: Spring Boot