Setup

Spring Boot Setup

Setups

  • Open IntelliJ IDEA and click on the "Create New Project" button.

  • In the "New Project" window, select "Spring Initializer" under the "Spring Boot" section. Click on the "Next" button.

  • Select the version of Spring Boot that you want to use and the dependencies that you need for your project. Click on the "Next" button.

  • Select the Name -> Location of your project then choose -> Language(java) -> Type(Maven) -> Group(com.codexam.backend) -> Artifact(backend) -> Package Name(com.codexam.backend) then -> JDk (Openjdk-19) -> Java (19) then -> Packaging (jar) then click on the "Finish" button.

  • Now select the Spring Web dependency then we need to add the Spring Data JPA dependency then we need MySQL Driver then Lombok dependency then Spring Boot DevTools dependency then click on the "Finish" button.

Structure Explanation

Structure

  • .mvn

    • .mvn/wrapper/maven-wrapper.jar: This is the Maven Wrapper JAR file, which is used to download and run the correct version of Maven for your project.

    • .mvn/wrapper/maven-wrapper.properties: This file contains configuration properties for the Maven Wrapper, such as the version of Maven to use and the URL of the Maven repository to download it from.

You can use the Maven Wrapper by running the ./mvnw script (on Unix-based systems) or the mvnw.cmd script (on Windows systems) from the command line, followed by the desired Maven command. For example, to build your project, you can run ./mvnw clean install. The Maven Wrapper will automatically download the correct version of Maven and run the command with that version.

  • src/main/java:

    • This is the main source code directory for your project.
    • It contains the Java code that makes up your application.
    • The code in this directory is organized into packages, and each package contains one or more Java classes.
  • src/main/resources:

    • This is the directory where you can put resource files such as property files and configuration files.
    • Resource files in this directory are typically used to configure your application or provide additional data that it needs at runtime.
    • These files are usually added to the classpath when the application is run, so they can be accessed by the code in your application.
  • src/test/java:

    • This is the directory where you can put test code for your application.
    • Test code is used to verify that your application is working correctly and to catch any bugs that might be present in the code.
    • Tests are usually written using a testing framework such as JUnit or TestNG, and they are typically run automatically when you build your project.
  • target:

    • This directory is created when you build your project.
    • It contains the compiled classes and any generated sources.
    • The compiled classes are stored in a subdirectory called classes, and any generated sources (such as code that is generated by annotation processors) are stored in a subdirectory called generated-sources.
  • .gitignore:

    • This file is used by Git to determine which files and directories should be ignored when you commit your code to a repository.
    • It contains a list of patterns that match the names of files and directories that should be ignored.
    • By default, the .gitignore file in a Spring Boot project ignores the target directory, as it is generated by the build process and should not be committed to the repository.
  • backend.iml:

    • This is an IntelliJ IDEA project file.
    • It contains information about the project's structure and dependencies.
    • This file is used by IntelliJ IDEA to manage the project and its dependencies, and it should not be edited manually.
  • help.md:

    • This file contains helpful information about the project, such as how to build and run it.
    • It might contain instructions on how to set up the development environment, how to build and test the code, and how to deploy the application.
  • mvnw and mvnw.cmd:

    • These are Maven wrapper scripts that allow you to run Maven commands without installing Maven on your machine.
    • Maven is a build and dependency management tool for Java projects, and it is used to build and manage the dependencies of a Spring Boot project.
    • The mvnw script is for Unix-based systems (such as Linux and macOS), and the mvnw.cmd script is for Windows systems.
  • pom.xml:

    • This is the Maven project object model (POM) file.
    • It defines the dependencies and build configuration for your project.
    • The POM file specifies which dependencies your project needs in order to build and run, and it also specifies
├── .gitignore
├── backend.iml
├── help.md
├── mvnw
├── mvnw.cmd
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── example
│   │   │           └── App.java
│   │   └── resources
│   │       ├── application.properties
│   │       └── log4j2.xml
│   └── test
│       └── java
│           └── com
│               └── example
│                  └── AppTest.java

├── target
│   ├── classes
│   │   └── com
│   │       └── example
│   │           └── App.class
│   └── generated-sources

└── .mvn
      └── wrapper
            └── maven_wrapper.jar
            └── maven-wrapper.properties
 

Src Folder Setup

Src Folder Setup

 src
  └── main
       └── java
             └── com.manage.backend.codexam
                       └── config
                       └── controllers
                       └── exception
                       └── payloads
                       └── repository
                       └── services
                       └── utils
                       └── BackendApplication.java
                       └── entities
 
BackendAppIication.java
package com.manage.backend.codexam;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
    @SpringBootApplication
    public class BackendApplication {
 
    public static void main(String[] args) {
    SpringApplication.run(BackendApplication.class, args);
}
 
}

In this directory structure, the src/main/java directory is the main source code directory for our project. It contains the Java code that makes up our application, and it is organized into packages based on the package names that we have chosen for our classes.

The com.manage.backend.codexam package contains several subpackages:

  • config: This package contains configuration classes for your application.
  • controllers: This package contains classes that define the controllers of your application. Controllers handle incoming HTTP requests and return responses to the client.
  • exception: This package contains classes that define custom exceptions for your application.
  • payloads: This package contains classes that define request and response payloads for your application.
  • repository: This package contains classes that define repositories for your application. Repositories are used to access data stored in a database or other persistent storage.
  • services: This package contains classes that define business logic and services for your application.
  • utils: This package contains utility classes that provide common functions or constants that are used throughout the application.
  • BackendApplication: This is the main class of your application, which contains the main method that is used to start the application.

MySQL Setup

Workbench Setup

  • Open MySQL Workbench and click on the plus sign to create a new connection.

  • Enter the following details:

    • Connection Name: codexam
    • Hostname: localhost
    • Port: 3306
  • Now click on the connection you made

  • Click on the schemas tab (under the database tab) then right click on the schemas tab and click on create schema

  • Enter the following details:

    • Schema Name: codexam
  • Now apply the changes and you are done with the setup of the database and click on finish

ℹ️

We want our application to create and manage its own databases in a MySQL database server, and store data in these databases.

Steps For Database Connection

Step 1: Details Check

  • First you need to know the username and password of your MySQL server.
  • After that open MySQL 8.0 Command Line Client and enter your password.
  • Now enter the following command to see the details
    \s

it will show you the details of your MySQL server.

  • now check you tcp port number.

Step 2: Connection

  • Now open Application and open src/main/resources/application.properties file.

  • Now enter the following details:

server.port=9090
spring.datasource.url=jdbc:mysql://localhost:3306/codexam-database
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
Explanation
  • server.port: This property specifies the port number that the application's web server should listen on. In this case, the web server will listen on port 9090.

  • spring.datasource.url: This property specifies the URL of the MySQL database that the application should use. The URL has the following format: jdbc:mysql://<host>:<port>/<database>. In this case, the database is located at localhost on port 3306, and the database name is codexam-database.

  • spring.datasource.username: This property specifies the username to use to connect to the MySQL database. In this case, the username is root.

  • spring.datasource.password: This property specifies the password to use to connect to the MySQL database. In this case, the password is root.

  • spring.datasource.driver-class-name: The spring.datasource.driver-class-name property specifies the fully-qualified name of the JDBC driver class that the application should use to connect to the MySQL database.

JDBC (Java Database Connectivity) is a Java API that allows Java programs to access and manipulate databases. JDBC drivers are libraries that implement the JDBC API and provide the necessary functionality to connect to a specific database.

In this case, the JDBC driver class is com.mysql.cj.jdbc.Driver, which is the driver class for MySQL Connector/J, a JDBC driver for MySQL.

  • spring.jpa.properties.hibernate.dialect: The property being set is hibernate.dialect, which specifies the type of SQL dialect that the application should use when communicating with the database. In this case, the dialect is set to org.hibernate.dialect.MySQL8Dialect, which is a specific dialect for communicating with a MySQL database.

A SQL dialect is a set of rules that specifies how the application should structure SQL statements (such as SELECT, INSERT, UPDATE, DELETE) for a particular database management system (DBMS). Different DBMSs have their own variations on the standard SQL language, and the dialect helps the application translate its SQL statements into a form that the DBMS can understand. For example, suppose the application needs to retrieve some data from a table called "customers" in the database. The application might construct a SELECT statement like this:

SELECT * FROM customers WHERE name = 'Alice';

However, this statement might not work on all DBMSs. For example, the MySQL database might require that the table name be enclosed in backticks, like this:

SELECT * FROM customers WHERE name = 'Alice';

The hibernate.dialect property tells the application which dialect to use, so that it can properly structure its SQL statements for the specific DBMS it is connecting to.

  • spring.jpa.hibernate.ddl-auto: This property specifies the mode for generating the database schema for the application. In this case, the value is update, which means that Hibernate will automatically update the schema of the database to match the entities in the application.
🚨

For finding MySQL8Dialect you can use Open Type option in your IDE.You can use this shortcut to quickly search for and open a specific type (such as a class, interface, or enum) within your project. "Ctrl+Shift+T" for STS IDE (eclips) & shift+shift for intellij"

create: Creates the database schema from scratch. Any existing data in the schema is deleted.

update: Updates the database schema to match the entity mappings in the application. Existing data is preserved.

create-drop: Creates the database schema, then drops it when the application shuts down.

validate: Validates the schema against the entity mappings. No changes are made to the schema.

none: Does not attempt to modify the schema.