drop database if exists AppleDb;
create database AppleDb;
use AppleDb;
CREATE TABLE apple ( apple_id BIGINT NOT NULL, apple_name VARCHAR(255) DEFAULT NULL,taste VARCHAR(255) DEFAULT NULL, expiry_date datetime(6) DEFAULT NULL, PRIMARY KEY (apple_id)) ENGINE=INNODB;
CREATE TABLE hibernate_sequence ( next_val BIGINT) ENGINE=INNODB;
insert into hibernate_sequence values ( 6 );
insert into apple (apple_id, apple_name , taste, expiry_date) values (1, "Macintosh", "tangy", "2020-01-01");
insert into apple (apple_id, apple_name , taste, expiry_date) values (2, "Fuji", "sweet", "2020-02-02");
insert into apple (apple_id, apple_name , taste, expiry_date) values (3, "Gala", "juicy", "2020-03-03");
insert into apple (apple_id, apple_name , taste, expiry_date) values (4, "Jonagold", "tart", "2020-04-04");
insert into apple (apple_id, apple_name , taste, expiry_date) values (5, "GrannySmith", "tangy", "2020-05-05");
package com.sujan.example.jpa.controller;
import com.sujan.example.jpa.entity.Apple;
import com.sujan.example.jpa.repository.AppleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@RestController
public class AppleController {
@Autowired
private AppleRepository appleRepository;
@GetMapping
List<Apple> findByExpiryDateBetween() throws ParseException {
Date sellDate = new SimpleDateFormat("yyyy-MM-dd").parse("2020-01-01");
Date expiryDate = new SimpleDateFormat("yyyy-MM-dd").parse("2020-06-01");
List<Apple> findByExpiryDateBetween = appleRepository.findByExpiryDateBetween(sellDate, expiryDate);
System.out.println("findByExpiryDateBetween = " + findByExpiryDateBetween);
return findByExpiryDateBetween;
}
}
package com.sujan.example.jpa.entity;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Date;
@Data
@Entity
@NoArgsConstructor
public class Apple {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long appleId;
private String appleName;
private String taste;
private Date expiryDate;
}
package com.sujan.example.jpa.repository;
import com.sujan.example.jpa.entity.Apple;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Date;
import java.util.List;
public interface AppleRepository extends JpaRepository<Apple, Long> {
List<Apple> findByExpiryDateBetween(Date start, Date end);
}
package com.sujan.example.jpa;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class JpaApplication {
public static void main(String[] args) {
SpringApplication.run(JpaApplication.class, args);
}
}
spring.datasource.url=jdbc:mysql://localhost:3306/AppleDb
spring.datasource.username=root
spring.datasource.password=root
plugins {
id 'org.springframework.boot' version '2.3.3.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
}
group = 'com.sujan'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
annotationProcessor 'org.projectlombok:lombok'
compileOnly 'org.projectlombok:lombok'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'mysql:mysql-connector-java'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
curl --location --request GET 'http://localhost:8080/'
select apple0_.apple_id as apple_id1_0_, apple0_.apple_name as apple_na2_0_, apple0_.expiry_date as expiry_d3_0_, apple0_.taste as taste4_0_ from apple apple0_ where apple0_.expiry_date between ? and ?
findByExpiryDateBetween = [Apple(appleId=1, appleName=Macintosh, taste=tangy, expiryDate=2020-01-01 05:30:00.0), Apple(appleId=2, appleName=Fuji, taste=sweet, expiryDate=2020-02-02 05:30:00.0), Apple(appleId=3, appleName=Gala, taste=juicy, expiryDate=2020-03-03 05:30:00.0), Apple(appleId=4, appleName=Jonagold, taste=tart, expiryDate=2020-04-04 05:30:00.0), Apple(appleId=5, appleName=GrannySmith, taste=tangy, expiryDate=2020-05-05 05:30:00.0)]
Spring Data Jpa sql Date Between
sql date dont have time.
drop database if exists AppleDb;
create database AppleDb;
use AppleDb;
CREATE TABLE apple ( apple_id BIGINT NOT NULL, apple_name VARCHAR(255) DEFAULT NULL,taste VARCHAR(255) DEFAULT NULL, expiry_date datetime(6) DEFAULT NULL, PRIMARY KEY (apple_id)) ENGINE=INNODB;
CREATE TABLE hibernate_sequence ( next_val BIGINT) ENGINE=INNODB;
insert into hibernate_sequence values ( 6 );
insert into apple (apple_id, apple_name , taste, expiry_date) values (1, "Macintosh", "tangy", "2020-01-01");
insert into apple (apple_id, apple_name , taste, expiry_date) values (2, "Fuji", "sweet", "2020-02-02");
insert into apple (apple_id, apple_name , taste, expiry_date) values (3, "Gala", "juicy", "2020-03-03");
insert into apple (apple_id, apple_name , taste, expiry_date) values (4, "Jonagold", "tart", "2020-04-04");
insert into apple (apple_id, apple_name , taste, expiry_date) values (5, "GrannySmith", "tangy", "2020-05-05");
package com.sujan.example.jpa.controller;
import com.sujan.example.jpa.entity.Apple;
import com.sujan.example.jpa.repository.AppleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@RestController
public class AppleController {
@Autowired
private AppleRepository appleRepository;
@GetMapping
List<Apple> findByExpiryDateBetween() throws ParseException {
Date sellDate = new SimpleDateFormat("yyyy-MM-dd").parse("2020-01-01");
Date expiryDate = new SimpleDateFormat("yyyy-MM-dd").parse("2020-06-01");
List<Apple> findByExpiryDateBetween = appleRepository.findByExpiryDateBetween(sellDate, expiryDate);
System.out.println("findByExpiryDateBetween = " + findByExpiryDateBetween);
return findByExpiryDateBetween;
}
}
package com.sujan.example.jpa.entity;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.sql.Date;
@Data
@Entity
@NoArgsConstructor
public class Apple {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long appleId;
private String appleName;
private String taste;
private Date expiryDate;
}
package com.sujan.example.jpa.repository;
import com.sujan.example.jpa.entity.Apple;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Date;
import java.util.List;
public interface AppleRepository extends JpaRepository<Apple, Long> {
List<Apple> findByExpiryDateBetween(Date start, Date end);
}
package com.sujan.example.jpa;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class JpaApplication {
public static void main(String[] args) {
SpringApplication.run(JpaApplication.class, args);
}
}
spring.datasource.url=jdbc:mysql://localhost:3306/AppleDb
spring.datasource.username=root
spring.datasource.password=root
plugins {
id 'org.springframework.boot' version '2.3.3.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
}
group = 'com.sujan'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
annotationProcessor 'org.projectlombok:lombok'
compileOnly 'org.projectlombok:lombok'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'mysql:mysql-connector-java'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
curl --location --request GET 'http://localhost:8080/'
select apple0_.apple_id as apple_id1_0_, apple0_.apple_name as apple_na2_0_, apple0_.expiry_date as expiry_d3_0_, apple0_.taste as taste4_0_ from apple apple0_ where apple0_.expiry_date between ? and ?
findByExpiryDateBetween = [Apple(appleId=1, appleName=Macintosh, taste=tangy, expiryDate=2020-01-01), Apple(appleId=2, appleName=Fuji, taste=sweet, expiryDate=2020-02-02), Apple(appleId=3, appleName=Gala, taste=juicy, expiryDate=2020-03-03), Apple(appleId=4, appleName=Jonagold, taste=tart, expiryDate=2020-04-04), Apple(appleId=5, appleName=GrannySmith, taste=tangy, expiryDate=2020-05-05)]