SpringDB is a project aimed at learning REST API using the Spring framework. It's linked to a MySQL database so to enable data permanence and uses JWT to allow stateless sessions.
The database setup can be split in two parts. The fist part is to download, install and set up the MySQL server (MySQL Community Server). Take note of the URL, username and password during the configuration process.
For the second part you can choose one of two ways.
Method 1
- Download and install MySQL Workbench
- Connect to your database server you set up before
- Create a new table in the database:
- Go to "Schemas" on the left pane
- Right click on "Tables" -> Create table (or you can just manually type in a query if you want to have some fun)
- Use the following parameters:*
- Table name: Auto
- Add a single column "id" where "id" is also the primary key
* Note that for the purpose of this project I used this configuration. You can skip this step if you have other needs. Also note that if you have a different configuration of the database tables you'll need to adapt the corresponding classes accordingly.
- Create a new file in the
/src/main/resourcesdirectory - Name the file as
application.properties - Paste (and edit accordingly) the following:
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=<your_db_URL>(it looks something like this: jdbc:mysql://127.0.0.1:3306/dbName)
spring.datasource.username=<your_username>
spring.datasource.password=<your_password>
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Method 2
If you don't want to install the MySQL Workbench client you can follow these steps to let Java do all the work.
- Create a new file in the
/src/main/resourcesdirectory - Name the file as
application.properties - Paste (and edit accordingly) the following:
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=<your_db_URL>(it looks something like this: jdbc:mysql://127.0.0.1:3306/dbName)
spring.datasource.username=<your_username>
spring.datasource.password=<your_password>
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Java will now create a table (or tables) based on the repository classes in the project.
After the first run, the table has been created, and you can change create to none.
If you are having issues with this configuration try using update instead of create,
and change it to none after the table has been created.
Make sure to add a class under com.alessio.springdb.constants with a static property KEY
to hold your key.
To run the program execute the main function in the SpringDbApplication class.