Skip to content

Quick Start

Kazuki Shimizu edited this page Apr 12, 2017 · 14 revisions

Let's create a MyBatis Spring Boot Application quickly using the SPRING INITIALIZR.

Create a project

Create a Spring Boot standalone application for MyBatis + H2 Database using following command (or the SPRING INITIALIZR UI).

$ curl -s https://start.spring.io/starter.tgz\
       -d name=mybatis-sample\
       -d artifactId=mybatis-sample\
       -d dependencies=mybatis,h2\
       -d baseDir=mybatis-sample\
       | tar -xzvf -

Create sql files

Create a sql file(src/main/resources/schema.sql) to generate the city table.

CREATE TABLE city (
    id INT PRIMARY KEY auto_increment,
    name VARCHAR,
    state VARCHAR,
    country VARCHAR
);

Create a sql file(src/main/resources/data.sql) to import demo data into the city table.

INSERT INTO city (name, state, country) VALUES ('San Francisco', 'CA', 'US');

Create a domain class

Create the City class.

package com.example;

import java.io.Serializable;

public class City implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;
    private String name;
    private String state;
    private String country;

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getState() {
        return this.state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getCountry() {
        return this.country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @Override
    public String toString() {
        return getId() + "," + getName() + "," + getState() + "," + getCountry();
    }

}

Create a mapper interface

Create the CityMapper interface for annotation driven.

package com.example;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface CityMapper {

    @Select("SELECT id, name, state, country FROM city WHERE state = #{state}")
    City findByState(String state);

}

Modify a spring boot application class

Add a bean definition that implements the CommandLineRunner interface at the MybatisSampleApplication class and call a mapper method.

package com.example;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class MybatisSampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatisSampleApplication.class, args);
    }

    private final CityMapper cityMapper;

    public MybatisSampleApplication(CityMapper cityMapper) {
        this.cityMapper = cityMapper;
    }

    @Bean
    CommandLineRunner runSample() {
        return (args) -> System.out.println(this.cityMapper.findByState("CA"));
    }

}

Run a spring boot application

Run a created application using the Spring Boot Maven Plugin.

$ ./mvnw spring-boot:run
...

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (vx.x.x.RELEASE)

...
1,San Francisco,CA,US
...

Also, you can package to a jar file and run using java command as follow:

$ ./mvnw package
$ java -jar target/mybatis-sample-0.0.1-SNAPSHOT.jar
Clone this wiki locally