Setup a simplest Spring Project Demo Project

A Spring Demo project. See:

Where to start

  1. Spring initializr (recommended)
  2. Create project from your IDE: IntelliJ, or Eclipse, etc..

Spring Boot & JPA with H2

0. Gradle dependency


implementation 'org.springframework.boot:spring-boot-starter'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
compile 'com.h2database:h2'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
compile 'org.joda:joda-money:1.0.1'
implementation 'org.jadira.usertype:usertype.core:6.0.1.GA'

Note: from offical website, joda money gradle dependency uses compile 'org.joda:joda-money:1.0.2-SNAP-SHOT' which is wrong.

1. Entity

  1. Base Entity
    • Abstraction of multiple entites
    • @MappedSuperclass
    • no toString() method
    • createTime is not updatable
     public class BaseEntity implements Serializable {
         private Long id;
         @Column(updatable = false)
         private LocalDateTime createTime;
         private LocalDateTime updateTime;
  2. Enum

     public enum OrderState {
  3. Entity
    • Be careful of the Money joda.PersistentMoneyAmount
    • Extends base entity
    • in toString, uses callSuper = true
    • @Enumerated
     @Table(name = "T_MENU")
     @ToString(callSuper = true)
     public class Coffee extends BaseEntity implements Serializable {
         private String name;
         @Type(type = "org.jadira.usertype.moneyandcurrency.joda.PersistentMoneyAmount",
                 parameters = {@org.hibernate.annotations.Parameter(name = "currencyCode", value = "CNY")})
         private Money price;
     @Table(name = "T_ORDER")
     @ToString(callSuper = true)
     public class CoffeeOrder extends BaseEntity implements Serializable {
         private String customer;
         @JoinTable(name = "T_ORDER_COFFEE")
         private List<Coffee> items;
         @Column(nullable = false)
         private Integer state;

2. Repository

  1. Base Repository

    • @NoRepositoryBean
     public interface BaseRepository<T, Long> extends PagingAndSortingRepository<T, Long> {
         List<T> findTop3ByOrderByUpdateTimeDescIdAsc();
  2. Regular repositories

    • extends base repository
     public interface CoffeeRepository extends BaseRepository<Coffee, Long> {}
     public interface CoffeeOrderRepository extends BaseRepository<CoffeeOrder, Long> {
         List<CoffeeOrder> findByCustomerOrderById(String customer);
         List<CoffeeOrder> findByItems_Name(String name);

3. Main method

  • @SpringBootApplication
  • @EnableJpaRepositories