pvt-containers/public/database.html

173 lines
6.9 KiB
HTML

<html>
<head>
<title>PVT course Jenkins instructions</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="./style.css"/>
</head>
<body>
<h1>SpringBoot and Database</h1>
<p>
<strong>What you will build</strong><br/>
You will create a database, build a Spring application, and connect it
to the newly created database.
</p>
<p>
<strong>What you will need</strong><br/>
The database is provided on the devtools website, and we are going to
use Spring Tool suite but you can use another IDE such as IntelliJ.
</p>
<p>
<strong>A note on MySQL/MariaDB</strong><br/>
We will be using MariaDB, which is an almost identical fork of MySQL.
The interested can find details about the relationship between the two
on <a href="https://en.wikipedia.org/wiki/MariaDB">Wikipedia</a>, but
the short version is that they are mostly interchangeable.
We will be using the names interchangably, except in cases where there
is a difference that matters, in which case it will be clearly stated
that the difference is important. Similarly, most MySQL resources on the
web will apply equally to MariaDB and vice versa.
</p>
<p>
A nice thing that we can do with spring boot JPA and MySQL are that we
can start with an empty database (by that I mean there are no tables
inside of it) and then we can use some annotations to transfer our data
transfer object (DTO) and we can have spring boot automatically map the
Data transfer object to the database.
</p>
<h2>Create a database</h2>
<ul>
<li>
Create mariaDB database available on the university server at
<a href="https://devtools.dsv.su.se">https://devtools.dsv.su.se</a>
</li>
<li>
After you create your database you will get the following information
as shown in the picture below to connect with your database:<br/>
<img src="img/db-1.png" />
</li>
<li>
Use a database management application to connect to your database.
Some suggested options for database management are
<a href="https://dbeaver.io/">DBeaver</a> (cross-platform, <strong>
must use the MariaDB database driver for connections</strong>) or
<a href="https://sequelpro.com">Sequel Pro</a> (for mac).
Create a new connection in your management application and fill in the
details of the database you created.<br/>
<img src="img/db-2.png" />
</li>
<li>
After that click on <em>Test connection</em>, and when it connects
successfully, <em>Save changes</em>. This step is to make sure that
you can connect to your database successfully.<br/>
<img src="img/db-3.png" />
</li>
</ul>
<h2>Configure your application to access your database</h2>
<p>
After you have created the database, then you need to setup a little bit
of configuration information in your application properties file in
spring boot application. All you need to do is to say what is that URL
to your database, the username, the password, and the driver.
</p>
<img src="img/db-4.png"/>
<p>
We are going to follow the example on Spring guides:
<a href="https://spring.io/guides/gs/accessing-data-mysql/#scratch">
https://spring.io/guides/gs/accessing-data-mysql/#scratch</a>
</p>
<ul>
<li>
Now you need to add the dependencies for JPA and MySql drivers to your
pom file, and make sure that the web dependency is added:
<pre>
&lt;!-- Spring web --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;!-- Spring data JPA --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;!-- MySQL driver --&gt;
&lt;dependency&gt;
&lt;groupId&gt;mysql&lt;/groupId&gt;
&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
&lt;scope&gt;runtime&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;javax.xml.bind&lt;/groupId&gt;
&lt;artifactId&gt;jaxb-api&lt;/artifactId&gt;
&lt;version&gt;2.3.0&lt;/version&gt;
&lt;/dependency&gt;
</pre>
</li>
<li>
Create the @Entity Model (DTO)<br/>
Hibernate automatically translates the entity into a table. You need
to have DTO (Data transfer object), where you use some simple
annotations on that DTO.
<ul>
<li>
<em>@Entity</em> says that this is a Data transfer object or java
bean, and I want this DTO to persist to a database
</li>
<li>
<em>@id</em> and <em>@generatedvalue</em> say that this is the
primary key and I want it to be autogenerated
</li>
</ul>
<img src="img/db-5.png"/>
</li>
<li>
Create a repository that holds user records, by extending an
interface called crudrepository.<br/>
<img src="img/db-6.png"/>
</li>
<li>
Create a controller using the <em>@controller</em> annotation, which
will handle HTTP requests to your application.<br/>
<img src="img/db-7.png"/>
</li>
<li>
This is the application architecture<br/>
<img src="img/db-8.png"/><br/>
<img src="img/db-9.png"/>
</li>
</ul>
<h2>Test the application</h2>
<ul>
<li>
Run your application. You should now be able to send a request to add
an entry to the database or retrieve all records from the database.
</li>
<li>
You can use send a request using any API for testing such as postman:
<a href="https://www.postman.com/downloads/">
https://www.postman.com/downloads/</a>
</li>
<li>
Add a record to database by sending a post request to the server, in
this example it is localhost:
<pre>localhost:8080/demo/add?name=john&email=john@su.se</pre><br/>
<img src="img/db-10.png"/>
</li>
<li>
You can check that the user has been added if you go to your database
manager and look at the table contents there.<br/>
<img src="img/db-11.png"/>
</li>
<li>
You can get all the records from database by sending a get request to
the server: <pre>localhost:8080/demo/all</pre><br/>
<img src="img/db-12.png"/>
</li>
</ul>
</body>
</html>