Hey people! Let’s build a simple example that demonstrates the ease of Spring Boot use when building applications that need to connect to, in this case – MySQL database. The final output will be a single – standalone jar file that can be executed as Java -jar my-boot-artifact.jar and will be able to process HTTP requests. The application is intentionally simple and there’s no big architecture inside it – you can see the database code inside Controller method, which you wouldn’t normally use for real world projects.

Let’s get started:

  • Create directory my-sample-boot-app
  • Inside it create pom.xml file with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.test</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.5.RELEASE</version>
        <relativePath/>
        <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

We’re including spring-boot-starter-parent to get all the intelligent defaults usually required. In order to get Spring MVC controllers and database access related code (Spring JDBC Template in this case), we include spring-boot-starter-web, spring-boot-starter-jdbc, mysql-connector-java dependencies.

In order to package the application as standalone jar file, we’re including spring-boot-maven-plugin. After that, you can import this as project into your own IDE. You should create a directory structure such as the one given below:

.
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   └── demo
│   │   │       ├── DemoApplication.java
│   │   │       └── web
│   │   │           ├── Person.java
│   │   │           └── PersonController.java
│   │   └── resources
│   │       ├── application.properties
│   │       ├── static
│   │       └── templates
│   └── test
│       └── java
│           └── demo
│               └── DemoApplicationTests.java

Having Maven installed on your machine, you can navigate to the root of your directory, and issue: mvn spring-boot:run , which should start your application, running a Tomcat instance inside ready to accept HTTP requests.

DemoApplication is an application entry class (contains main method). It is annotated with SpringBootApplication which bootstraps application and tries to guess the intelligent defaults your app might want to use based on what dependencies you listed in pom.xml file.

Our request handler can be accessed via http://localhost:8099/person?id=1. It responds to HTTP request with JSON response. You can use either browser or any HTTP client, such as CURL. Our Spring MVC controller:

<pre>package demo.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class PersonController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping(value = "/person")
    public
    @ResponseBody
    Person dailyStats(@RequestParam Integer id) {
        String query = "SELECT first_name, last_name, age" +
                " from person where person.id = " + id;

        return jdbcTemplate.queryForObject(query, (resultSet, i) -&gt; {
            return new Person(resultSet.getString(1), resultSet.getString(2), resultSet.getInt(3));
        });

    }
}

You can see that actual JDBC code is quite lightweight. Our example requires database table schema such as:

<pre>CREATE TABLE `person` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(30) NOT NULL,
  `last_name` varchar(40) NOT NULL,
  `age` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `unique_id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1

Once the database is set up, we can run our application via already mentioned mvn spring-boot:run

The initial call takes a bit longer. Maven downloads require dependencies whilst it produces the JAR, which you can ran afterwards. This project requires Java 8 JDK. You can download sample application code (Download ZIP button to download source code).

 

Spring Boot and Spring JdbcTemplate Example

| Java Language| 1,297 views | 0 Comments
About The Author
-

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>