Quick Start

Kazuki Shimizu edited this page Mar 3, 2018 · 4 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(src/main/java/com/example/mybatissample/City.java).

package com.example.mybatissample;

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(src/main/java/com/example/mybatissample/CityMapper.java) for annotation driven.

package com.example.mybatissample;

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(src/main/java/com/example/mybatissample/MybatisSampleApplication.java) and call a mapper method.

package com.example.mybatissample;

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 sampleCommandLineRunner() {
        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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.