DB Connection with App

Methods for each CRUD operations have been created in the Controller and spring annotations such as @RestController and @RequestMapping have been used to bind each method to appropriate HTTP method.


Make them accept request parameters or JSON payload and work properly to CRUD users




Enable an app to communicate with  MySQL Database


* Update pom.xml

Spring Data JPA is used to store information into MySQL Database (store Java object into MySQL)
MySQL Connector is needed to connect an application to database.


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

Once building the project with above two additional dependencies, Maven will go into its repository and take those two dependencies. It will download the corresponding libraries and add them to 'Maven Dependencies' folder in the project.

(In case you get xml script from Maven Repository, make sure to remove version as Spring Boot manages version ex. following the version of Parent..)




*Configure MySQL Database access details

Open a file 'application.properties' which is under src/main/resources .


spring.datasource.username=
spring.datasource.password=
spring.datasource.url=jdbc:mysql://localhost:3306/test

(Connection url to database / 'test' is the name of schema used for the app / name should be correct, otherwise DB is not connected)

spring.jpa.hibernate.ddl-auto=update

( When application starts, hibernate will

- create : destroy the previous schema if it exists, and  create new (be careful)
- update : (create db) automatically update the mapped table in the database when there is change in the domain model, making sure that the database tables and the domain models in the app are in sync. )








Comments

Popular posts from this blog

Portlet Edit Mode (Portlet preference) to replace Portlet configuration

GoGo Shell & What can be deleted in Liferay Instance