Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

SQL DSL (Domain Specific Language) for Kotlin and Java. Supports rendering for MyBatis or Spring JDBC Templates

License

NotificationsYou must be signed in to change notification settings

mybatis/mybatis-dynamic-sql

Repository files navigation

Build StatusCoverage StatusMaven centralSonatype Nexus (Snapshots)LicenseQuality Gate StatusSecurity Rating

What Is This?

This library is a general purpose SQL generator. Think of it as a typesafe and expressive SQL DSL (domain specific language),with support for rendering SQL formatted properly for MyBatis3 and Spring's NamedParameterJDBCTemplate.

The library also contains extensions for Kotlin that enable an idiomatic Kotlin DSL for SQL.

The library will generate full DELETE, INSERT, SELECT, and UPDATE statements. The DSL implemented by thelibrary is very similar to native SQL but it includes many functions that allow for very dynamic SQL statements.For example, a typical search can be coded with a query like this (the following code is Kotlin, but Java code is very similar):

funsearch(id:String?,firstName:String?,lastName:String?)=        select(Customer.id,Customer.firstName,Customer.lastName).from(Customer) {            where(Customer.active, isEqualTo(true))and(Customer.id, isEqualToWhenPresent(id).then{ it?.padStart(5,'0') })and(Customer.firstName, isLikeCaseInsensitiveWhenPresent(firstName)                .then{"%"+ it.trim()+"%" })and(Customer.lastName, isLikeCaseInsensitiveWhenPresent(lastName)                .then{"%"+ it.trim()+"%" })            orderBy(Customer.lastName,Customer.firstName)            limit(500)        }

This query does quite a lot...

  1. It is a search with three search criteria - any combination of search criteria can be used
  2. Only records with an active status will be returned
  3. Ifid is specified, it will be padded to length 5 with '0' at the beginning of the string
  4. IffirstName is specified, it will be used in a case-insensitive search and SQL wildcards will be appended
  5. IflastName is specified, it will be used in a case-insensitive search and SQL wildcards will be appended
  6. The query results are limited to 500 rows

Using the dynamic SQL features of the library eliminates a lot of code that would be required for checking nulls, adding wild cards, etc. This query clearly expresses the intent of the search in just a few lines.

See the following pages for detailed information:

PageComments
Quick StartShows a complete example of building code for this library
MyBatis3 SupportInformation about specialized support forMyBatis3. The examples on this page are similar to the code generated byMyBatis Generator
Kotlin Support with MyBatis3Information about the Kotlin extensions and Kotlin DSL when using MyBatis3 as the runtime
Spring SupportInformation about specialized support for Spring JDBC Templates
Kotlin Support with SpringInformation about the Kotlin extensions and Kotlin DSL when using Spring JDBC Template as the runtime
Spring Batch SupportInformation about specialized support for Spring Batch using theMyBatis Spring Integration

Requirements

The library has no dependencies. Java 8 or higher is required.


[8]ページ先頭

©2009-2025 Movatter.jp