• Stars
    star
    1,094
  • Rank 42,362 (Top 0.9 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created about 8 years ago
  • Updated 27 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

MyBatis Dynamic SQL

Build Status Coverage Status Maven central Sonatype Nexus (Snapshots) License Quality Gate Status Security 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 the library 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):

   fun search(id: String?, firstName: String?, lastName: String?) =
        select(Customer.id, Customer.firstName, Customer.lastName) {
            from(Customer)
            where { Customer.active.isTrue() }
            and { Customer.id (isEqualToWhenPresent(id).map{ it?.padStart(5, '0') }) }
            and { Customer.firstName (isLikeCaseInsensitiveWhenPresent(firstName)
                .map{ "%" + it.trim() + "%" }) }
            and { Customer.lastName (isLikeCaseInsensitiveWhenPresent(lastName)
                .map{ "%" + 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. If id is specified, it will be padded to length 5 with '0' at the beginning of the string
  4. If firstName is specified, it will be used in a case-insensitive search and SQL wildcards will be appended
  5. If lastName 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:

Page Comments
Quick Start Shows a complete example of building code for this library
MyBatis3 Support Information about specialized support for MyBatis3. The examples on this page are similar to the code generated by MyBatis Generator
Kotlin Support with MyBatis3 Information about the Kotlin extensions and Kotlin DSL when using MyBatis3 as the runtime
Spring Support Information about specialized support for Spring JDBC Templates
Kotlin Support with Spring Information about the Kotlin extensions and Kotlin DSL when using Spring JDBC Template as the runtime
Spring Batch Support Information about specialized support for Spring Batch using the MyBatis Spring Integration

The library test cases provide several complete examples of using the library in various different styles:

Language Runtime Comments Code Directory
Java MyBatis3 Example using Java utility classes for MyBatis in the style of MyBatis Generator ../examples/simple
Java MyBatis3 + MyBatis-Spring Example using MyBatis-Spring integration ../examples/column/comparison
Java MyBatis3 + MyBatis-Spring (Spring Batch) Example using Java utility classes for the MyBatis integration with Spring Batch ../examples/springbatch
Java Spring JDBC Example using Java utility classes for Spring JDBC Template ../examples/spring
Kotlin MyBatis3 Example using Kotlin utility classes for MyBatis in the style of MyBatis Generator ../examples/kotlin/mybatis3/canonical
Kotlin MyBatis3 + MyBatis-Spring Example using MyBatis-Spring integration in Kotlin ../examples/kotlin/mybatis3/column/comparison
Kotlin Spring JDBC Example using Kotlin utility classes for Spring JDBC Template ../examples/kotlin/spring/canonical

Requirements

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

More Repositories

1

mybatis-3

MyBatis SQL mapper framework for Java
Java
19,751
star
2

generator

A code generator for MyBatis.
Java
5,281
star
3

spring-boot-starter

MyBatis integration with Spring Boot
Java
4,132
star
4

spring

Spring integration for MyBatis 3
Java
2,822
star
5

jpetstore-6

A web application built on top of MyBatis 3, Spring 3 and Stripes
Java
578
star
6

redis-cache

MyBatis Redis Cache adapter
Java
411
star
7

mybatipse

Eclipse plugin adding support for MyBatis SQL Mapper Framework.
Java
333
star
8

migrations

A command line Database migrations tool
Java
214
star
9

parent

The MyBatis parent POM which has to be inherited by all MyBatis modules
176
star
10

guice

Google Guice integration for MyBatis 3
Java
175
star
11

scala

The Scala version of MyBatis SQL Mapper
Scala
151
star
12

ehcache-cache

MyBatis EhCache adapter
Java
149
star
13

ibatis2mybatis

Tool to convert iBATIS 2 xml files to MyBatis3
XSLT
108
star
14

ibatis-2

iBATIS 2.x
Java
98
star
15

typehandlers-jsr310

MyBatis type handlers for JSR-310
Java
95
star
16

spring-native

Java
86
star
17

memcached-cache

MyBatis Memcached adapter
Java
72
star
18

ignite-cache

Adapter for Apache Ignite
Java
62
star
19

freemarker-scripting

FreeMarker plugin for MyBatis 3.2
Java
60
star
20

velocity-scripting

Velocity scripting plugin for MyBatis
Java
48
star
21

ibatis-spring

Ibatis integration code copied from Spring ORM 3.2.x branch
Java
28
star
22

cdi

MyBatis CDI Extension
Java
25
star
23

thymeleaf-scripting

Thymeleaf scripting plugin for MyBatis
Java
23
star
24

migrations-maven-plugin

Maven plugin for the MyBatis Migrations tool
Java
22
star
25

hazelcast-cache

MyBatis Hazelcast adapter
Java
21
star
26

mybatis.github.io

MyBatis public site
HTML
19
star
27

caffeine-cache

MyBatis cache adapter for Caffeine
Java
7
star
28

oscache-cache

OSCache support for MyBatis Cache
Java
7
star
29

typehandlers-threeten-extra

MyBatis type handlers for ThreeTen Extra (http://www.threeten.org/threeten-extra/)
Java
6
star
30

logo

MyBatis logo
5
star
31

base-bundle-descriptor

A maven-assembly-plugin descriptor to create binary distributions for all MyBatis modules
4
star
32

couchbase-cache

Couchbase cache adapter for MyBatis 3
Java
3
star
33

projects-aggregator

The pom for aggregator of mybatis projects
3
star
34

old-google-code-issues

Automatically exported from code.google.com/p/mybatis
2
star
35

committers-stuff

Resources for committers, like docs, instructions and so
2
star
36

mybatis-bom

MyBatis Bill of Materials
1
star