# MyBatis Dynamic SQL
[![Build Status](https://github.com/mybatis/mybatis-dynamic-sql/workflows/Java%20CI/badge.svg?branch=master)](https://github.com/mybatis/mybatis-dynamic-sql/actions?query=workflow%3A%22Java+CI%22)
[![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)
[![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)
[![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/)
[![License](https://img.shields.io/:license-apache-brightgreen.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)
[![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)
[![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)
## 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):
```kotlin
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...
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 used as a filter
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](src/site/markdown/docs/quickStart.md) | Shows a complete example of building code for this library |
| [MyBatis3 Support](src/site/markdown/docs/mybatis3.md) | Information about specialized support for [MyBatis3](https://github.com/mybatis/mybatis-3). The examples on this page are similar to the code generated by [MyBatis Generator](https://github.com/mybatis/generator) |
| [Kotlin Support with MyBatis3](src/site/markdown/docs/kotlinMyBatis3.md) | Information about the Kotlin extensions and Kotlin DSL when using MyBatis3 as the runtime |
| [Spring Support](src/site/markdown/docs/spring.md) | Information about specialized support for Spring JDBC Templates |
| [Kotlin Support with Spring](src/site/markdown/docs/kotlinSpring.md) | Information about the Kotlin extensions and Kotlin DSL when using Spring JDBC Template as the runtime |
| [Spring Batch Support](src/site/markdown/docs/springBatch.md) | Information about specialized support for Spring Batch using the [MyBatis Spring Integration](https://github.com/mybatis/spring) |
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](src/test/java/examples/simple) |
| Java | MyBatis3 + MyBatis-Spring | Example using MyBatis-Spring integration | [../examples/column/comparison](src/test/java/examples/column/comparison) |
| Java | MyBatis3 + MyBatis-Spring (Spring Batch) | Example using Java utility classes for the MyBatis integration with Spring Batch | [../examples/springbatch](src/test/java/examples/springbatch) |
| Java | Spring JDBC | Example using Java utility classes for Spring JDBC Template | [../examples/spring](src/test/java/examples/spring) |
| Kotlin | MyBatis3 | Example using Kotlin utility classes for MyBatis in the style of MyBatis Generator | [../examples/kotlin/mybatis3/canonical](src/test/kotlin/examples/kotlin/mybatis3/canonical) |
| Kotlin | MyBatis3 + MyBatis-Spring | Example using MyBatis-Spring integration in Kotlin | [../examples/kotli
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
mybatis动态sql 1.什么是动态SQL? Mabits是一个Java持久化框架,它提供了动态SQL的功能。动态SQL是一种根据不同条件动态生成SQL语句的技术。在Mabits中,动态SQL通常是通过使用一组特殊的标签和代码块来实现的,这些标签和代码块可以根据条件包含或排除不同的部分,从而生成不同的SQL语句。动态SQL可以让开发者构建更灵活、高效的数据库操作语句,因为他们可以根据实际需要创建出更加精确和优化的SQL语句。Mabits的动态SQL还允许开发者使用参数化查询,可以防止SQL注入攻击,并增强程序的安全性 学习经常用的几个标签 2.<if>标签 <if>标签是Mabits动态SQL中最常用的一个标签之一,它的作用是根据给定条件包含或排除不同的部分,以生成不同的SQL语句。在XML文件中,<if>标签通常被嵌套在其他标签内,如<select>、<insert>和<update>等标签内,用于控制生成的SQL语句的结构和内容。 <if>标签通常包含一个test属性,该属性被用于指定条件表达式。如果表达式的结果为true,则<if>标签内的内容会被包含在生成的
资源推荐
资源详情
资源评论
收起资源包目录
这个库是生成动态 SQL 语句的框架.zip (629个子文件)
mvnw.cmd 8KB
maven.config 96B
site.css 683B
.gitattributes 80B
.gitignore 287B
FromJoinWhereTest.java 193KB
AnimalDataTest.java 104KB
JoinMapperTest.java 64KB
OptionalConditionsAnimalDataTest.java 52KB
OptionalConditionsWithPredicatesAnimalDataTest.java 51KB
CaseExpressionTest.java 41KB
PersonMapperTest.java 39KB
SqlBuilder.java 37KB
Issue105Test.java 35KB
GroupByTest.java 32KB
PersonTemplateTest.java 31KB
QueryExpressionDSL.java 22KB
ExistsTest.java 22KB
FilterAndMapTest.java 21KB
JoinSubQueryTest.java 21KB
CommonSelectMapperTest.java 20KB
CustomRenderingTest.java 17KB
SelectStatementTest.java 17KB
FromGroupByTest.java 14KB
MariaDBTest.java 14KB
CommonSelectMapper.java 14KB
GeneratedAlwaysMapperTest.java 14KB
OptionalCriterionRenderTest.java 13KB
SpringTest.java 13KB
EmptyWhereTest.java 13KB
InvalidSQLTest.java 12KB
ColumnMappingVisitorTest.java 12KB
CriterionRenderer.java 12KB
LimitAndOffsetTest.java 12KB
SupplierTest.java 12KB
Issue100Test.java 11KB
UpdateStatementTest.java 11KB
SubQueryTest.java 11KB
QueryExpressionRenderer.java 9KB
SqlColumn.java 9KB
AbstractQueryExpressionDSL.java 9KB
FetchFirstTest.java 9KB
PersonMapper.java 9KB
GroupingTest.java 9KB
MyBatis3Utils.java 9KB
Issue100StartAfterJoinTest.java 8KB
UpdateDSL.java 8KB
MyBatisMapToRowTest.java 8KB
NamedParameterJdbcTemplateExtensions.java 8KB
NoInitialConditionTest.java 8KB
SelectStatementTest.java 7KB
MapToRowTest.java 7KB
ShardingTest.java 7KB
SelectDSL.java 7KB
RenderingContext.java 7KB
ColumnComparisonTest.java 7KB
TypeConversionTest.java 6KB
StatementConfigurationTest.java 6KB
InsertStatementTest.java 6KB
RenderingStrategy.java 6KB
UpdateRenderer.java 6KB
AbstractBooleanExpressionDSL.java 6KB
DefaultConditionVisitor.java 6KB
CriterionRendererTest.java 6KB
SqlTableTest.java 6KB
GeneratedAlwaysMapper.java 6KB
Gh655Test.java 6KB
PersonMapperTest.java 6KB
CursorReaderBatchConfiguration.java 6KB
PagingReaderBatchConfiguration.java 6KB
CountDSL.java 5KB
ComplexQueryTest.java 5KB
DeleteRenderer.java 5KB
SpringMapToRowTest.java 5KB
SetPhraseVisitor.java 5KB
PersonWithAddressMapper.java 5KB
MultiSelectRenderer.java 5KB
QueryExpressionModel.java 5KB
BulkInsertConfiguration.java 5KB
ArrayTest.java 5KB
SchemaSupplierTest.java 5KB
MultiSelectDSL.java 5KB
DeleteDSL.java 5KB
SqlTable.java 5KB
BatchInsertDSL.java 5KB
JoinRenderer.java 5KB
GeneralInsertDSL.java 5KB
WhereModel.java 5KB
PersonMapper.java 4KB
GeneralInsertStatementTest.java 4KB
InsertDSL.java 4KB
NameService.java 4KB
ReusableWhereTest.java 4KB
DerivedColumn.java 4KB
BindingTest.java 4KB
InsertStatementTest.java 4KB
IsNotBetween.java 4KB
MultiRowInsertDSL.java 4KB
AbstractBooleanExpressionRenderer.java 4KB
IsBetween.java 4KB
共 629 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
野生的狒狒
- 粉丝: 2338
- 资源: 2099
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功