Skip to content

Commit ce1fdea

Browse files
committedAug 19, 2020
Add a Code Example to the README
1 parent e3159cc commit ce1fdea

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed
 

‎README.md

+34-15
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,44 @@
44
[![Coverage Status](https://coveralls.io/repos/github/mybatis/mybatis-dynamic-sql/badge.svg?branch=master)](https://coveralls.io/github/mybatis/mybatis-dynamic-sql?branch=master)
55
[![Maven central](https://maven-badges.herokuapp.com/maven-central/org.mybatis.dynamic-sql/mybatis-dynamic-sql/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.mybatis.dynamic-sql/mybatis-dynamic-sql)
66
[![Sonatype Nexus (Snapshots)](https://img.shields.io/nexus/s/https/oss.sonatype.org/org.mybatis.dynamic-sql/mybatis-dynamic-sql.svg)](https://oss.sonatype.org/content/repositories/snapshots/org/mybatis/dynamic-sql/mybatis-dynamic-sql/)
7-
[![License](http://img.shields.io/:license-apache-brightgreen.svg)](http://www.apache.org/licenses/LICENSE-2.0.html)
7+
[![License](https://img.shields.io/:license-apache-brightgreen.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)
88
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=mybatis_mybatis-dynamic-sql&metric=alert_status)](https://sonarcloud.io/dashboard?id=mybatis_mybatis-dynamic-sql)
99
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=mybatis_mybatis-dynamic-sql&metric=security_rating)](https://sonarcloud.io/dashboard?id=mybatis_mybatis-dynamic-sql)
1010

1111
## What Is This?
12-
This library is a framework for generating dynamic SQL statements. Think of it as a typesafe SQL templating library,
13-
with additional support for MyBatis3 and Spring JDBC Templates.
14-
15-
The library will generate full DELETE, INSERT, SELECT, and UPDATE statements formatted for use by MyBatis or Spring.
16-
The most common use case is to generate statements, and a matching set of parameters, that can be directly used
17-
by MyBatis. The library will also generate statements and parameter objects that are compatible with Spring JDBC
18-
templates.
19-
20-
The library works by implementing an SQL-like DSL that creates an object containing a full SQL statement and any
21-
parameters required for that statement. The SQL statement object can be used directly by MyBatis as a parameter to a mapper method.
22-
23-
The library also contains extensions for Kotlin that enable an idiomatic Kotlin DSL.
24-
25-
See the following pages for further information:
12+
This library is a general purpose SQL generator. Think of it as a typesafe and expressive SQL DSL (domain specific language),
13+
with support for rendering SQL formatted properly for MyBatis3 and Spring's NamedParameterJDBCTemplate.
14+
15+
The library also contains extensions for Kotlin that enable an idiomatic Kotlin DSL for SQL.
16+
17+
The library will generate full DELETE, INSERT, SELECT, and UPDATE statements. The DSL implementd by the
18+
library is very similar to native SQL but it includes many functions that allow for very dynamic SQL statements.
19+
For example, a typical search can be coded with a query like this (the following code is Kotlin, but Java code is very similar):
20+
21+
```kotlin
22+
fun search(id: String?, firstName: String?, lastName: String?) =
23+
select(Customer.id, Customer.firstName, Customer.lastName).from(Customer) {
24+
where(Customer.active, isEqualTo(true))
25+
and(Customer.id, isEqualToWhenPresent(id).then{ it?.padStart(5, '0') })
26+
and(Customer.firstName, isLikeCaseInsensitiveWhenPresent(firstName).then{ "%" + it.trim() + "%" })
27+
and(Customer.lastName, isLikeCaseInsensitiveWhenPresent(lastName).then{ "%" + it.trim() + "%" })
28+
orderBy(Customer.lastName, Customer.firstName)
29+
limit(500)
30+
}
31+
```
32+
33+
This query does quite a lot...
34+
35+
1. It is a search with three search criteria - any combination of search criteria can be used
36+
1. Only records with an active status will be returned
37+
1. If `id` is specified, it will be padded to length 5 with '0' at the beginning of the string
38+
1. If `firstName` is specified, it will be used in a case insensitive search and SQL wildcards will be appended
39+
1. If `lastName` is specified, it will be used in a case insensitive search and SQL wildcards will be appended
40+
1. The query results are limited to 500 rows
41+
42+
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.
43+
44+
See the following pages for detailed information:
2645

2746
| Page | Comments|
2847
|------|---------|

0 commit comments

Comments
 (0)
Please sign in to comment.