How to easily configure MySQL database?

Once we create a packaging structure next step is uh we need to configure MySQL database, uh you know details in our spring boot project so we typically configure all you know database related details in application.properties file so before configuring MySQL uh database details in our project first we need to create a database in MySQL server.

Let’s head over to MySQL workbench so make sure that you have installed MySQL server and MySQL workbench on your machine, so MySQL workbench is basically a client which we used to interact with the MySQL server so to create a database just type create a database and followed by name of the database let’s say SMS that is a student management system and just execute this SQL statement and repress the schemas and there we go SMS database is successfully created.

Now let’s go back to our SDS ID and go-to application.properties file and here we configure MySQL you know details for example just configure JDBC URL now spring dot data source dot URL and just provide a property something like jobs’ colon MySQL, so this is a standard judge URL, uh you know format followed by localhost so here I’m giving localhost because our MySQL server is located on the local machine.

If your MySQL server is located on another machine then you have to provide IP address up that much in here or local you know our host of that machine here okay and followed by the port of this MySQL database followed by name of the database that is SMS and then we add a few more attributes like use SSL equal to false we are going to disable SSL and let’s add a few more properties to this URL that is server time zone so server times on UTC and we disable legacy Daytime code okay pretty simple similarly let’s go ahead and let’s configure database username and password spring.datasource.js username.

In my case, the username is the root, and let’s similarly configure password dot password root so in my case my database username is root and password is also root but make sure that you will replace username password as per your mice for my simple server installation on your machine okay don’t forget to replace username and password as per your MySQL you know setup now we’re going to configure hibernate properties here so headband basically requires a dilated, uh to generate SQL SQL queries for a chosen database in our case we have chosen MySQL database so we’re going to add hibernate dilate for MySQL database.

Let me quickly add here spring.jpg.properties.hibernate.toilet and followed by the name of the dialect that is MySQL for you in node b direct all right great so make sure that whenever you use a database with hibernate and make sure that you will add hibernate dilate for that particular database in our case we are using MySQL database.

Hence, I have added MySQL I burn it direct over here similarly let’s add one more hibernate property that is auto DDL so we are going to generate our database tables automatically by using hibernate uh upper hibernate you know feature so lets one let’s configure that property here spring.JPA.hibernate.ddlr2 I’m going to provide a value as an update so here I have provided a value as an update because we are going to create a table in a database if they do not exist and we are going to update the existing tables.

There are few more values for this property like create hyphen drop or create or welded was this but this updated value makes sense for me that’s why I have given an update here and we are going to also set a logging level to see the hybrid intended queries for that just type the property logging dot label and then org dot hibernate dot SQL equal to let’s queue debug it’s pretty simple isn’t it so once we configure all the details in application.properties file let’s create a JP entity for student management application this you know JP entity in our case is a student so right-click on entity package new and then choose a class and let’s use JP entity name as a student.

Let’s define the properties inside this class that is private long ID and then private string first name private string last name and one more is private string email so to keep it simple I had only these four feeds but make sure that you will add you know uh the fields you know student class as per your requirement now let’s go and let’s generate data center methods to access these private fields right click source and generate getter status select all and hit generate button here now we are going to create a parameters’ constructor right click source and generate constructor using fields deselect ID here and click on generate.

Whenever you create a parameter as a constructor then make sure that you will also create a default constructor because hibernate internally uses proxies to create objects dynamically and it needs a default constructor so whenever you create a parameter constructor make sure that you will also create a default constructor in JP entity okay so now look at here a student is a simple java class so let’s make this simple java class as a JP entity by using JPA annotations that is at entity JP annotation and then we are going to map this entity to database table so for that we are going to use add table annotation here just to provide a table related details.

Let’s provide a table name here so table name in our case students now we need to add a primary key for this table so for that we’re going to use at ID annotation from java x dot persistence package and we are going to also use you know primary case generation strategy here so to provide a primary generation strategy we are going to use that generated value annotation here and it has a strategy attribute and we can specify primary generation strategy uh here okay so we are going to use identity as a primary key generation strategy all right perfect now what we’re going to do is we’re going to map a column name for these fields to do.

We’re going to use add column annotation so add column annotation has an attribute called name and we can use a name to specify column name here first underscore name and you can define this column as a not-null by using the label attribute the label goes to false similarly let’s add a column annotation for the last name as well name equals to last underscore name similarly let’s add for email name equals to email okay great so just remember if you don’t add table annotation to JP entity class then JPS smart enough to provide a table name as the name of the class and if you don’t add at column annotation to map a column name then JPS smart enough to provide a column name as a field name.

If you don’t add column annotation to the first name field then JPI will give a column name as the name of the field like this all right perfect now what I’m going to do is we’re going to create a GPA repository so go ahead and right-click on repository package and then choose the interface and just give interface name as student repository hit enter and let’s extend this interface from JPA repository so look at here JP repository has two parameters the first parameter is the type of the JP entity so in our case we have a student JP entity and the second parameter is a type of the primary key.

Leave a Comment