<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Awdesh sharma</title>
    <description>The latest articles on DEV Community by Awdesh sharma (@awdesh).</description>
    <link>https://dev.to/awdesh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F581731%2F93e6d841-2b0a-4955-bd45-7c50b8dd015a.jpeg</url>
      <title>DEV Community: Awdesh sharma</title>
      <link>https://dev.to/awdesh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/awdesh"/>
    <language>en</language>
    <item>
      <title>Database Access with Spring Data JPA</title>
      <dc:creator>Awdesh sharma</dc:creator>
      <pubDate>Tue, 02 Mar 2021 05:09:51 +0000</pubDate>
      <link>https://dev.to/awdesh/database-access-with-spring-data-jpa-2ocm</link>
      <guid>https://dev.to/awdesh/database-access-with-spring-data-jpa-2ocm</guid>
      <description>&lt;p&gt;JPA is a standard for ORM. It is an API layer that maps Java objects to the database tables. All you need is to annotate your Domain object with @Entity. &lt;/p&gt;

&lt;p&gt;In Spring Boot, Spring Data JPA brings the goodness of Spring Data and JPA together. &lt;/p&gt;

&lt;p&gt;In the JDBC world we have to embed sql queries inside the codebase to access the database, JPA hides all that complexity by providing many CRUD related methods via JpaRepository.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Let's take a look&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1614642552926%2Fvqae0Mb9C.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1614642552926%2Fvqae0Mb9C.png" alt="carbon(2).png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Above code is mapping User Object to User table in the database. User table consists two columns "first_name" and "last_name".  @Column annotation is defining the column inside the User table.&lt;/p&gt;

&lt;p&gt;JPA supports many annotations e.g. @Table annotation can define the indexes on the table. Take a look at the sample code below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1614644104258%2FjBZ1KFZKp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1614644104258%2FjBZ1KFZKp.png" alt="carbon(3).png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Above we are creating an index name “name_index” on the first_name column.&lt;/p&gt;

&lt;h3&gt;
  
  
  Data access layer without JPA:
&lt;/h3&gt;

&lt;p&gt;JDBC is the prominent method to access the database. JDBC requires one to write sql queries to communicate with the database, it makes use of Statement, ResultSet etc. to prepare a sql statement and gets the result out of database respectively.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's take a look below&lt;/strong&gt;-:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT first_name FROM Customers WHERE last_name = 'Doe'");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we create a statement object and call executeQuery on it with supplying the SQL query. ResultSet object will contain all the returned result from the query. We can now use an iterator to iterate over items inside result set.&lt;/p&gt;

&lt;p&gt;Similarly an insert statement could look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Statement stmt = connection.createStatement();
stmt.execute("INSERT INTO user (FIRST_NAME,LAST_NAME) VALUES (1,'John','Dow')");

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Data access layer with JPA
&lt;/h3&gt;

&lt;p&gt;Let's see how JPA does above two database operations.&lt;/p&gt;

&lt;p&gt;Spring Data Jpa expose JpaRepository which is comprises on all the CRUD methods. Once we define a repository for the domain object we are ready to call these methods.&lt;/p&gt;

&lt;p&gt;Let's create a repository interface for User.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1614644818460%2FfmRbLKzye.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1614644818460%2FfmRbLKzye.png" alt="carbon(5).png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;@Repository annotation tells Spring Boot to scan interface and make it available at startup. You'll notice that we haven't create any method inside our interface because we don't need to, atleast for the simple CRUD use cases. &lt;br&gt;
All those operations are supported by JpaRepository out of the box and we are extending it.&lt;/p&gt;

&lt;p&gt;Now let's compare same select and insert operation of Jdbc here in Jpa.&lt;/p&gt;

&lt;p&gt;Like Select if we have to retrieve all the objects we can do&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User user = userRepository.findAll();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Like Insert if we have to insert a user we can do&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;userRepository.save(user);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Much simpler than the JDBC implementation. &lt;/p&gt;

&lt;p&gt;Let's take a look at another example. &lt;/p&gt;

&lt;p&gt;Finding all the users with the given first name. &lt;/p&gt;

&lt;p&gt;Use “findBy” to search on a particular column.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// find rows by the first name.
userRepository.findByFirstName(String firstName);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// find rows by the last name.
userRepository.findByLastName(String lastName);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'll stop here for this post. In the next post I'll write how to handle more complex sql queries using JPA.&lt;/p&gt;

&lt;p&gt;If you like to walk through video tutorial rather I have a Spring Boot playlist on YouTube 👉 &lt;a href="https://youtu.be/fiKYlmTdG2g" rel="noopener noreferrer"&gt;Spring Boot Tutorial Series&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I post daily about programming on different platforms with the intention of writing good tech content and keep them less spammy.&lt;/p&gt;

&lt;p&gt;You can also find me 👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/channel/UC7Ze67ISJCgtHZBXCYcBLIg?sub_confirmation=1" rel="noopener noreferrer"&gt;Youtube&lt;/a&gt;  |  &lt;a href="https://twitter.com/S_AWDESH" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;  |  &lt;a href="https://instagram.com/awdeshcodes" rel="noopener noreferrer"&gt;Instagram&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Let’s learn together 💪💪&lt;/p&gt;

&lt;p&gt;Happy Coding 💻&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Quick walkthrough of creating an API with Spring Boot.</title>
      <dc:creator>Awdesh sharma</dc:creator>
      <pubDate>Thu, 25 Feb 2021 07:03:40 +0000</pubDate>
      <link>https://dev.to/awdesh/quick-walkthrough-of-creating-an-api-with-spring-boot-3096</link>
      <guid>https://dev.to/awdesh/quick-walkthrough-of-creating-an-api-with-spring-boot-3096</guid>
      <description>&lt;p&gt;In this post, I am discussing how we can create a simple API with Spring Boot. This post discusses about Repository, Controller/Resource and Model layer.&lt;/p&gt;

&lt;p&gt;Let's get to it. 🚀&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisite&lt;/strong&gt;: Make sure you have Java 11 and maven installed.&lt;/p&gt;

&lt;p&gt;Head over to &lt;em&gt;start.spring.io&lt;/em&gt; from your favourite browser and create a starter project. Add these three dependency from the Add Dependency tab on right pane.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JPA&lt;/strong&gt; : JAVA Persistence API. Provides lot of methods to interact with database.&lt;br&gt;
&lt;strong&gt;H2&lt;/strong&gt; : In memory relational database. &lt;br&gt;
&lt;strong&gt;Spring Web&lt;/strong&gt; : Uses Apache Tomcat as default embedded container.&lt;/p&gt;

&lt;p&gt;Click on Generate at the bottom to download the zipped folder. Extract it and open it in IntelliJ or any other favourite editor of your choice.&lt;/p&gt;

&lt;p&gt;Your pom.xml with dependencies should look like below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F49iof8y308haovzmkwav.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F49iof8y308haovzmkwav.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just three dependencies that we added from the website. Simple and clean.&lt;/p&gt;

&lt;p&gt;At this point we can run the service and it'll start the tomcat but it won't be very useful to us since we haven't defined any endpoints.&lt;/p&gt;

&lt;p&gt;So let’s jump into the action and start creating classes. I'll start from bottom-up i.e. creating classes that are closer to the database and then move up to utilize those classes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Model layer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The first thing I like to think about is the object that I want to store in the database. In this post I am using &lt;strong&gt;Video&lt;/strong&gt; object. A video can contain a title and a description.&lt;/p&gt;

&lt;p&gt;Let's create a class named Video.java.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd0i0u5u5g7g2p8ehvv1d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd0i0u5u5g7g2p8ehvv1d.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is the quick summary of the annotations used in the class.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;a class="mentioned-user" href="https://dev.to/id"&gt;@id&lt;/a&gt;&lt;/em&gt; : Annotation tells that the field is a primary key.&lt;br&gt;
&lt;em&gt;@GeneratedValue&lt;/em&gt; : JPA will take care of auto generating this value for us. We don't need to manually send it with the object.&lt;br&gt;
&lt;em&gt;@Entity&lt;/em&gt; : Represents that the POJO can be persisted in the database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Repository layer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Remember JPA that we added while generating the project. It exposes many methods that can be utilized to interact with the database. Methods like save(), findAll(), delete for regular CRUD operations.&lt;/p&gt;

&lt;p&gt;No more hassle of constructing JDBC queries.👍&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F047a0a26c1h2qqkx935j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F047a0a26c1h2qqkx935j.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;@Repository : Helps Spring Boot to scan the repository layer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Controller/ Resource layer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Layer that expose object from data to the outside world and brings days from outside world.&lt;/p&gt;

&lt;p&gt;In other words, it is where we define GET, POST and other REST mappings to create API endpoints.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fctycpukv7vzhny452pih.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fctycpukv7vzhny452pih.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;@RestController : Signifies that the class is a controller class and contains REST endpoints. &lt;/p&gt;

&lt;p&gt;That's it. 🎉🎉&lt;/p&gt;

&lt;p&gt;Now you are ready to run the service. Look for the class ending with Application in your project. It should contain main method. &lt;/p&gt;

&lt;p&gt;You can simply run the main method that will scan all the components based on the annotations that we have supplied and bootup the service.&lt;/p&gt;

&lt;p&gt;If everything went well, you should be able to see tomcat running at port 8080. Fireup postman and go to &lt;a href="http://localhost:8080" rel="noopener noreferrer"&gt;http://localhost:8080&lt;/a&gt; and GET, POST a Video object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="http://localhost:8080/h2-console" rel="noopener noreferrer"&gt;http://localhost:8080/h2-console&lt;/a&gt;&lt;/strong&gt; will open the window to connect to in memory database. You don't need to change anything other than Jdbc_url which can be find from the logs inside the terminal window.&lt;/p&gt;

&lt;p&gt;This is a very basic and simple implementation of creating an API with Spring Boot. There are many features of SB that comes out of the box which I will write about in the next few posts.&lt;/p&gt;

&lt;p&gt;If you like to walk through video tutorial rather I have a Spring Boot playlist on YouTube 👉 &lt;a href="https://www.youtube.com/channel/UC7Ze67ISJCgtHZBXCYcBLIg" rel="noopener noreferrer"&gt;Spring Boot Tutorial Series&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I post daily about programming. You can find me 👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/channel/UC7Ze67ISJCgtHZBXCYcBLIg?sub_confirmation=1" rel="noopener noreferrer"&gt;Youtube&lt;/a&gt; | &lt;a href="https://twitter.com/S_AWDESH" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; | &lt;a href="https://www.instagram.com/awdeshcodes/" rel="noopener noreferrer"&gt;Instagram&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s learn together 💪💪&lt;/p&gt;

&lt;p&gt;Happy Coding 💻&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
