Spring Framework - Spring Boot - Spring boot + Spring Data JPA
Start
Spring Boot and Spring Data JPA integration basic example
We're going to build an application that stores POJOs in a database. The application uses Spring Data JPA to store and retrieve data in a relational database. Its most compelling feature is the ability to create repository implementations automatically, at runtime, from a repository interface.
The main() method uses Spring Boot’s SpringApplication.run() method to launch an application. Please notice that there isn’t a single line of XML. No web.xml file either. This web application is 100% pure Java and you don’t have to deal with configuring any plumbing or infrastructure.
Entity Class
packageorg.springboot.model;importjavax.persistence.Entity;importjavax.persistence.GeneratedValue;importjavax.persistence.GenerationType;importjavax.persistence.Id;@EntitypublicclassGreeting{@Id@GeneratedValue(strategy =GenerationType.AUTO)privateLong id;privateString text;publicGreeting(){super();}publicGreeting(String text){super();this.text = text;}/* In this example, the typical getters and setters have been left out for brevity. */}
Here you have a Greeting class with two attributes, the id, and the text. You also have two constructors. The default constructor only exists for the sake of JPA. You won’t use it directly, so it can be designated as protected. The other constructor is the one you’ll use to create instances of Greeting to be saved to the database.
The Greeting class is annotated with @Entity, indicating that it is a JPA entity. For lack of a @Table annotation, it is assumed that this entity will be mapped to a table named 'Greeting'.
The Greeting’s id property is annotated with @Id so that JPA will recognize it as the object’s ID. The id property is also annotated with @GeneratedValue to indicate that the ID should be generated automatically.
The other property, text is left unannotated. It is assumed that it’ll be mapped to a column that share the same name as the property itself.
Transient Properties
In an entity class similar to the one above, we can have properties that we don't want to be persisted in the database or created as columns in our database maybe because we just want to set them at runtime and use them in our application, hence we can have that property annotated with the @Transient annotation.
packageorg.springboot.model;importjavax.persistence.Entity;importjavax.persistence.GeneratedValue;importjavax.persistence.GenerationType;importjavax.persistence.Id;importjavax.persistence.Transient;@EntitypublicclassGreeting{@Id@GeneratedValue(strategy =GenerationType.AUTO)privateLong id;privateString text;@TransientprivateString textInSomeLanguage;publicGreeting(){super();}publicGreeting(String text){super();this.text = text;this.textInSomeLanguage =getTextTranslationInSpecifiedLanguage(text);}/* In this example, the typical getters and setters have been left out for brevity. */}
Here you have the same Greeting class that now has a transient property textInSomeLanguage that can be initialized and used at runtime and won't be persisted in the database.
Spring Data JPA also allows you to define other query methods by simply declaring their method signature. In the case of GreetingRepository, this is shown with a findByText() method.
In a typical Java application, you’d expect to write a class that implements GreetingRepository. But that’s what makes Spring Data JPA so powerful: You don’t have to write an implementation of the repository interface. Spring Data JPA creates an implementation on the fly when you run the application.
You can run the application from the command line with Maven. Or you can build a single executable JAR file that contains all the necessary dependencies, classes, and resources, and run that. This makes it easy to ship, version, and deploy the service as an application throughout the development lifecycle, across different environments, and so forth.
Run the application using ./mvnw spring-boot:run. Or you can build the JAR file with ./mvnw clean package. Then you can run the JAR file:
java -jar target/springboot-0.0.1-SNAPSHOT.jar
Remarks
Annotations
@Repository: Indicates that an annotated class is a "Repository", a mechanism for encapsulating storage, retrieval, and search behavior which emulates a collection of objects. Teams implementing traditional J2EE patterns such as "Data Access Object" may also apply this stereotype to DAO classes, though care should be taken to understand the distinction between Data Access Object and DDD-style repositories before doing so. This annotation is a general-purpose stereotype and individual teams may narrow their semantics and use as appropriate.
@RestController: A convenience annotation that is itself annotated with @Controller and @ResponseBody.Types that carry this annotation are treated as controllers where @RequestMapping methods assume @ResponseBody semantics by default.
@Service: Indicates that an annotated class is a "Service" (e.g. a business service facade). This annotation serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning.
@SpringBootApplication: Many Spring Boot developers always have their main class annotated with @Configuration, @EnableAutoConfiguration and @ComponentScan. Since these annotations are so frequently used together (especially if you follow the best practices above), Spring Boot provides a convenient @SpringBootApplication alternative.
@Entity: Specifies that the class is an entity. This annotation is applied to the entity class.
Official Documentation
Pivotal Software has provided a pretty extensive documentation on Spring Framework, and it can be found at