0 Project Overview

TinyBlog is a blogging system, as Tiny as its name is, it is an extremely Tiny Tiny Tiny 👌 blogging system, designed as a Demo application for learning various technologies 😀.

This article focuses on the implementation of its most basic back-end interface (CRUD), support to add blog, query blog, modify blog and delete blog. Follow-up Interface functions Added on demand ~ This is a project separated from the front and back ends, so this article only covers the back-end interface part.

1 Project Design

1.1 Table structure design

At present, consider the most basic blog function, only designed the blog table, as well as a few basic fields.

1.2 Interface Design

The basic RESTFul INTERFACE of CRUD should be provided first, which needs to be supplemented later.

request URL Interface specification
POST /api/blog Create a blog
GET /api/blog Get all blogs
GET /api/blog/:id Get id for:idThe blog
PUT /api/blog/:id Update the id for:idThe blog
DELETE /api/blog/:id Delete the id for:idThe blog

1.3 technology stack

  • Java 8
  • Spring Boot, Spring MVC, Spring Data JDBC
  • MySQL

1.4 Development Tools

  • IDEA, Postman, Navicat, VS Code

2 Environment Construction

2.1 Preparing the Database Environment

Create the TinyBlog library, and then create the Blog table.

CREATE DATABASE 'tinyblog' CHARACTER SET 'utF8' COLLATE 'utf8_general_ci'; CREATE TABLE 'blog' (' id 'bigint(0) NOT NULL AUTO_INCREMENT,' title 'varchar(255) NULL, `description` varchar(255) NULL, `status` int(255) NULL, `content` varchar(255) NULL, PRIMARY KEY (`id`) );Copy the code

2.2 Creating project projects

Create a new IDEA project and choose to initialize the project through Spring Initializr.

Fill in the project information.

Select project dependencies.

2.3 Project Configuration

Check the Maven configuration (Maven is advised to replace your Maven mirror with your own Maven mirror).

Then open pom.xml and check to make sure you have these dependencies.

Open application. Properties to configure our database information.

3 Writing Code

With the development environment ready, it’s time to write the code. We wrote the code from bottom up, Entity -> DAO -> Service -> Controller.

3.1 Creating a data model

First, we’ll create a new Model package and add our entity class Blog.

3.2 create a DAO

Next, we create the Repository package, add DAO(Data Access Object) interface BlogDao, and its implementation class JdbcBlogDao.

It defines the CRUD operations on the database that our interface will use.

3.3 create a Service

The Service layer is usually where we implement our business logic. Although the business logic of TinyBlog is very simple at the moment, we will create the Service package, the BlogService interface and its implementation class BlogServiceImpl, where we will call the Dao we just created to complete the database operation. Provide a complete add delete change check service.

3.4 create a Controller

Finally, create a Controller that provides a RESTFul interface to the outside world.

The complete project structure is as follows.

The complete code can be obtained in Gitee https://gitee.com/noissues/tinyblog-springboot-jdbctemplate.git.

4 Run & Test

Run the project and open Postman to test the interface we developed.

4.1 Creating a Blog interface

Create a blog using the Postman call interface.

Check the database and record the insertion success.

4.2 Query blog interface

1 Query all blogs

The query interface, without any parameters, will query all data.

2 Query blogs by ID

Attention should also be test ID query less than, then found, JdbcTemplate queryForObject method, do not check when data can throw EmptyResultDataAccessException anomalies. Therefore, exception handling is required for this query.

3 Search blogs by title

4.3 Update blog interface

The Update Blog interface supports incremental updates, which means you can change only the status or title of a blog.

One thing to note here is that you started with a copy of Spring’s BeanUtil. This is problematic because it does not filter out attributes with a value of NULL, whereas incremental updates are needed.

So reference online practice, their own implementation of a copy method to ignore the Null value.

Then call the update interface, which is also OK.

4.4 Deleting the Blog Interface

Delete the blog with the specified ID.

5 concludes

This paper mainly adopts Spring Boot + JdbcTemplate to achieve a simple blog system, provides RESTFul INTERFACE of CRUD, uses MySQL as persistent storage, and realizes the most basic function of add, delete, change and check.

Finally, if you have any thoughts or suggestions, please leave them in the comments section.