Skip to content

自定义 SQL

Kite ORM 允许在 Mapper 接口中使用自定义的 SQL 语句,以满足复杂查询需求。

注解

以下注解在 com.tang.kite.annotation 包中定义。

参数名类型默认值说明
@SelectString(必填)自定义的 SELECT SQL 语句
@InsertString(必填)自定义的 INSERT SQL 语句
@UpdateString(必填)自定义的 UPDATE SQL 语句
@DeleteString(必填)自定义的 DELETE SQL 语句

@Param 注解用于标记方法参数,文档请参考@Param

参数说明

参数名类型默认值说明
valueString(必填)用于 SQL 绑定的方法参数名称

使用示例

java
import com.tang.kite.annotation.Param;
import com.tang.kite.annotation.Select;
import com.tang.kite.mapper.BaseMapper;

public interface UserMapper extends BaseMapper<User> {

    @Select("select id, username from account where username = #{username}")
    Account selectByUsername(@Param("username") String username);

}
java
import com.tang.kite.annotation.id.Id;
import com.tang.kite.annotation.id.IdType;

public class Account {

    @Id(type = IdType.AUTO)
    private Long id;

    private String username;

}

多行字符串

java
import com.tang.kite.annotation.Param;
import com.tang.kite.annotation.Select;
import com.tang.kite.mapper.BaseMapper;

public interface UserMapper extends BaseMapper<User> {

    @Select("""
        select id, username
        from account
        where username = #{username}
    """)
    Account selectByUsername(@Param("username") String username);

}
java
import com.tang.kite.annotation.id.Id;
import com.tang.kite.annotation.id.IdType;

public class Account {

    @Id(type = IdType.AUTO)
    private Long id;

    private String username;

}

属性访问

通过 . 访问对象属性,[] 访问 Array、List、Map 元素,支持嵌套访问,允许在条件语句里面和 #{} 占位符中使用。

使用场景示例说明
对象属性访问account.username访问对象属性
Array/List 访问dataList[0]访问 Array 或 List 指定索引元素
Map 元素访问settings['theme']访问 Map 指定键对应的值
组合访问user.address[0].city访问用户地址列表中第一个地址的城市

运算符支持

支持算术运算、比较运算、逻辑运算以及括号优先级控制,可在条件语句和占位符中使用。

算术运算符

运算符说明示例
+加法age + 1
-减法quantity - 5
*乘法price * 2
/除法total / count
%取模day % 7
^幂运算base ^ exponent

比较运算符

运算符说明示例
>大于age > 18
<小于score < 60
>=大于等于salary >= 5000
<=小于等于quantity <= 100
==等于status == 'ACTIVE'
!=不等于role != 'ADMIN'

逻辑运算符

运算符说明示例
&&逻辑与age > 18 && status == 'ACTIVE'
||逻辑或`role == 'ADMIN'
!逻辑非!isDeleted

括号优先级

使用 () 控制运算优先级。

kotlin
// 先计算乘法,再计算加法
price * (1 + discount)

// 先计算逻辑或,再计算逻辑与
(role == 'ADMIN' || role == 'MANAGER') && status == 'ACTIVE'

条件语句

支持 ifelse ifelse 以及嵌套 if 语句:

kotlin
@Select("""
    select id, username, email
    from account
    if (username != null && username != '') {
        and username = #{username}
    }
    if (age != null) {
        if (age > 18) {
            and age > #{age}
        } else {
            and age <= #{age}
        }
    }
    if (status == 'ACTIVE') {
        and status = 'ACTIVE'
    } else if (status == 'INACTIVE') {
        and status = 'INACTIVE'
    } else {
        and status = 'PENDING'
    }
""")

方法支持

方法名功能描述支持的数据类型
length/size获取长度/大小CharSequence, Iterable, Array, Map
isEmpty检查是否为空CharSequence, Iterable, Array, Map
isNotEmpty检查是否非空CharSequence, Iterable, Array, Map
contains检查是否包含指定元素CharSequence, Iterable, Array, Map
containsIgnoreCase忽略大小写检查是否包含指定字符串CharSequence
isBlank检查是否为空白字符串CharSequence
isNotBlank检查是否非空白字符串CharSequence
toUpperCase转换为大写字母CharSequence
toLowerCase转换为小写字母CharSequence
startsWith检查是否以指定前缀开头CharSequence
endsWith检查是否以指定后缀结尾CharSequence
trim去除首尾空白字符CharSequence

使用示例

java
@Select("""
    select id, username, email
    from account
    if (username != null) {
        and username = #{username}
    }
    if (email != null && email.contains('@')) {
        and email = #{email}
    }
""")
Account selectAccount(@Param("username") username: String?, @Param("email") email: String?);

由 Tang 用 ❤️ 构建