mybatis/mybatis-dynamic-sql
关于
面向 Kotlin 和 Java 的 SQL DSL(领域特定语言)。支持针对 MyBatis 或 Spring JDBC Templates 的渲染
创作与媒体实测星标增长
- 24小时
- 0
- 7天
- +1
- 15天
- +1
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文MyBatis Dynamic SQL
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):
data class SearchParameters(val id: String?, val firstName: String?, val lastName: String?)
fun search(searchParameters: SearchParameters) =
select(id, firstName, lastName) {
from(Customer)
where {
active isEqualTo true
and { id isEqualToWhenPresent searchParameters.id }
and {
firstName(isLikeCaseInsensitiveWhenPresent(searchParameters.firstName)
.map { "%" + it.trim() + "%" })
}
and {
lastName(isLikeCaseInsensitiveWhenPresent(searchParameters.lastName)
.map { "%" + it.trim() + "%" })
}
}
orderBy(lastName, firstName)
limit(500)
}
This query does quite a lot...
- It is a search with three search criteria - any combination of search criteria can be used
- Only records with an active status will be returned
- If
idis specified, it will be used as a filter - If
firstNameis specified, it will be used in a case-insensitive search and SQL wildcards will be appended - If
lastNameis specified, it will be used in a case-insensitive search and SQL wildcards will be appended - 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 and Dependencies
Version 2.x requires Java 17 and has a required runtime dependency on JSpecify (https://jspecify.dev/). Version 1.x requires Java 8 and has no required runtime dependencies.
All versions have support for MyBatis3, Spring Framework, and Kotlin - all those dependencies are optional. The library should work in those environments as the dependencies will be made available at runtime.
探索相关项目
加载中…