![]() It’s better to abstract out these common fields in a separate base class, and that’s what we’ll be doing in this post. Both these domain models will have some common auditing related fields like createdAt and updatedAt. We have two main domain models in our application - Question and Answer. The domain models are the classes that are mapped to the corresponding tables in the database. The ddl-auto property is used to automatically create the tables based on the entity classes in the application. It is the default JPA vendor that comes with spring-data-jpa.Īlthough Hibernate is database agnostic, we can specify the current database dialect to let it generate better SQL queries for that database. The last two properties in the above file are for Hibernate. # Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties) = jdbc:postgresql://localhost:5432/postgres_demo = rajeevkumarsingh = # The SQL dialect makes Hibernate generate better SQL for the chosen database .dialect = # Hibernate ddl auto (create, create-drop, validate, update) -auto = update You can do that simply by adding PostgreSQL database url, username, and password in the src/main/resources/application.properties file. Let’s configure Spring Boot to use PostgreSQL as our data source. We’ll be creating the required packages and classes one by one in the coming sections.įirst thing First. I am including the complete directory structure of the project here for your reference. That’s it! You may now import the project into your favorite IDE and start working. Click Generate to download the project.Add Web, JPA, PostgreSQL, and Validation in the dependencies section.Enter postgres-demo in the Artifact field.Package import .annotation.Autowired import import import .annotation.$ spring init -name =postgres-demo -dependencies =web,jpa,postgresql,validation postgres-demoĪlternatively, you can generate the project using Spring Initializr web tool by following the instructions below. Open the application.properties file in the src/main/resources folder (it should be empty). Create the User entity, the UserRepository and the UserService.There are two steps to code the application: We are done with the creation of the project. Now click the button at the bottom right of the screen to open the project in a new window. Select a folder and clock on "Generate into this folder". Then you should select the folder where you want to create the project. PostgreSQL Driver (dependency to connect to Postgres).Spring Data JPA (dependency to use Hibernate). ![]() Spring Web (dependency to create a Rest API).This will open a prompt at the top of the screen. Open VS Code and click on the "Create Java Project" button: To do this, you need to have the Java Extension Pack and the Spring Boot Extension Pack installed. There are many ways to create a new Kotlin project, but I will use the Spring Initializr in VS Code. Optional: VS Code with the following extensions: (Optional): Postman and Tableplus to follow along, but any testing tool will work.We will go with a step-by-step guide, so you can follow along. Test the application with Postman and Tableplus.Create docker-compose.yml to run the database and the application.Create User.kt, UserRepository.kt and UserService.kt.Create a Spring Boot project using Spring Initializr.We will create 5 endpoints for basic CRUD operations: Here is a schema of the architecture of the application we are going to create: Let's create a CRUD Rest API in Kotlin, using:Īll the code is available in the GitHub repository (link in the video description):
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |