No description, website, or topics provided.
Java Shell Batchfile
Latest commit 14546f6 Feb 12, 2017 @making making committed on GitHub Merge pull request #19 from domaframework/doma-2.15
Bump to Doma 2.15.0


Spring Boot Support for Doma

Build Status

Properties file configuration

doma.dialect= # Dialect of database used by Doma. (STANDARD, SQLITE, DB2, MSSQL, MYSQL, POSTGRES, ORACLE, H2, HSQL)
doma.sql-file-repository= # Type of SqlFileRepository. (GREEDY_CACHE, NO_CACHE)
doma.exception-translation-enabled= # Whether convert JdbcException into DataAccessException.
doma.max-rows=0 # Limit for the maximum number of rows. Ignored unless this value is greater than 0.
doma.query-timeout=0 # Number of seconds the driver will wait for a Statement object to execute. Ignored unless this value is greater than 0.
doma.fetch-size=0 # Hint to the number of rows that should be fetched. Ignored unless this value is greater than 0.
doma.batch-size=0 # Size in executing PreparedStatement#addBatch(). Regarded as 1 unless this value is greater than 1. # Datasource name.
doma.exception-sql-log-type= # Type of SQL log in the exception. (RAW, FORMATTED, NONE)

Issue Tracking

GitHub Issues

Maven dependency


Add the following repository to use snapshots.

    <name>Sonatype Snapshots</name>


Create a project

Create a blank project from SPRING INITIALIZR. Enter and select "Web", "JDBC" and "H2" in「Search for dependencies」.

Then click 「Generate Project」 and will be downloaded. Extract the zip and import the Maven project into IDE. In this tutorial we will use IntelliJ IDEA. In case of IDEA, only you have to do is just open pom.xml.

Add the following dependency to pom.xml so that we can use Doma with Spring Boot.


Create an entity

Next, create the following entity

package com.example;

import org.seasar.doma.Entity;
import org.seasar.doma.GeneratedValue;
import org.seasar.doma.GenerationType;
import org.seasar.doma.Id;

public class Reservation {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer id;
    public String name;

Create a DAO interface

Then, create the following DAO interface. We will add search and insert method.

package com.example;

import org.seasar.doma.Dao;
import org.seasar.doma.Insert;
import org.seasar.doma.Select;
import org.seasar.doma.boot.ConfigAutowireable;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface ReservationDao {
    List<Reservation> selectAll();

    int insert(Reservation reservation);

@ConfigAutowireable is the annotation to annotate @Repository and @Autowired on generated DAO implematations using @AnnotateWith.

Generate the DAO imlementation class

After create a DAO interface, build by IDE or Maven, then the implementation class will be generated. However, update methods need the corresponding SQL files at the compile time. Unless SQL files exist, compilation will fail. Usually, SQL corresponding to the method will be written in src/main/resources/META-INF/(FQCN)/(class name)/(method name).sql. In this case, it's src/main/resources/META-INF/com/example/ReservationDao/selectAll.sql.

Tip: On the image above, selectAll is highlighted by red because the required SQL file does not exist. This feature is enabled by Doma Support Plugin. This is not compulsory but really useful.

With this plugin, SQL file can be created easily. Option + Enter on the method, and select 「SQLファイルを作る。」 on the menu.

Then select .../src/main/resouces and SQL file will be generated.

After that, write your query in this SQL file.

FROM reservation

Build again, then compilation will succeed and you can see ReservationDaoImpl is generated under target and compiled.

Create an application

Let's make a small application using ReservationDao in DemoApplication.

package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

public class DemoApplication {

    public static void main(String[] args) {, args);

    ReservationDao reservationDao;

    // Insert data at initailizing phase using ReservationDao#insert
    CommandLineRunner runner() {
        return args -> Arrays.asList("spring", "spring boot", "spring cloud", "doma").forEach(s -> {
            Reservation r = new Reservation();
   = s;

    @RequestMapping(path = "/")
    List<Reservation> all() {
        return reservationDao.selectAll();

Next configure the SQL dialect for Doma. In this case, we use H2 database, so set



Property values can be suggested by Ctrl + Space.

Doma does not generate schema, so DDL script is needed. Create src/main/resources/schema.sql as follows:

CREATE TABLE reservation (
  id   IDENTITY,

Tip: Executing schema.sql can be skipped by setting spring.datasource.initialize=false. This will be helpful in deploying.

Finally, run main method on DemoApplication, then the application will launch.

Access http://localhost:8080, the result of selectAll.sql will show.

Add a method

Add selectByName to try Doma's 2 way SQL.

public interface ReservationDao {
    List<Reservation> selectAll();

    List<Reservation> selectByName(String name);

    int insert(Reservation reservation);

Write the correspoinding SQL in src/main/resources/META-INF/com/example/ReservationDao/selectByName.sql.

FROM reservation
WHERE name LIKE /* @prefix(name) */'spring%' ESCAPE '$'

Check the doc for the special expression in SQL comment.

This SQL can be executed as it is!

Add the following method to Controller and call ReservationDao#selectByName.

    @RequestMapping(path = "/", params = "name")
    List<Reservation> name(@RequestParam String name) {
        return reservationDao.selectByName(name);

Source code


Licensed under the Apache License, Version 2.0.