Spring Boot – University Book Keeping App , Part – 2

1.logo_spring

In this section i will create the base project structure of our application and the controller classes and domain objects. So, gear up for a Spring Boot experience. Lets create the project. I will use STS to create our application. Its better to note some preliminary points before we start coding.

Bullets:-

  • Technology – Spring Boot , Spring REST, Spring Data JPA with Hibernate
  • DB – MySQL & H2 ( for testing purpose)
  • Build & Dependency Management tool – Maven
  • Application Server – Tomcat

DB Script:-

create database technovillage;
>use technovillage;
>create table region (region_id integer(10) primary key,region_name varchar(255));
>create table location (location_id integer(10) ,location_name varchar(255),region_id integer(10), foreign key (region_id) references region(region_id));

You can open STS IDE –> Create a new “Spring Starter Project” . You can follow the below screen shots i have taken to ease your task.

1.SpringBoot-UniversityBK_1 1.SpringBoot-UniversityBK_2
The next step is to create the packages and classes.So we have here different packages which are meant for different purpose.
1)package structure:-
endpoint – contains all the controller classes
beans- contains all the entities of our application
repository- contains the db repository interfaces viz. JPA repositories.
service- contains the service classes.

2)Entity beans:-
As our application is jpa based so we have to create the entities for each table. So We have different entities for each table. Each entity class is annotated with @Entity and @Table. In each entity class we have the primary key column is annotated with @Id and other columns are with @Column. If the table column names and the class properties names are different that we can specify with @Column(name=””) annotation.

Note that when there is foreign key relationship between the tables, eg between location and region table, we have a @ManyToOne mapping in the location table.The annotation here specifies that in the relationship , location is the owner and region is at the inverse side of the relationship. And there is a M-1 mapping between location and region. Which means that each region can contain multiple location.
@JoinColumn annotation states that location column contains the foreign key column and this column name is region_id in location table.

@ManyToOne
@JoinColumn(name=”region_id”)
private Region region;

3)Controllers:-
The next step is to create the controller classes. Note that for each domain object i have a separate RestController class. I can also club all the rest api inside a single  RestController ,but that will make the class too noisy. So, to avoid that i have taken a different approach. Depends on you how you want to put your code. Each controller class is annotated with @RestController which denotes that this is Spring REST based class and contains all the Request mapping.

@RestController
public class LocationController {

@Autowired
@Qualifier(value=”locationService”)
GenericService service;

@PostMapping(path=”/v1/location”)
public void create(@RequestBody Location loc){
System.out.println(“Creating location “+loc);
service.create(loc);
}
}

4)Repository:-
Repository is the abstraction provided by Spring Data Projects. To create a JpaRepository we have to create an interface and then extend the JpaRepository interface. Then in the interface declaration we have to mention the entity bean for which we are creating the repository.

public interface LocationRepository extends JpaRepository<Location, Integer>{

}

 

Our project structure :-

1.SpringBoot-UniversityBK_3

 

 

 

 

Related Post

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu Title