Skip to content

Custom SQL

Kite ORM allows using custom SQL statements in the Mapper interface to meet complex query requirements.

Annotations

The following annotations are defined in the com.tang.kite.annotation package.

AnnotationTypeDefaultDescription
@SelectString(Required)Custom SELECT SQL statement
@InsertString(Required)Custom INSERT SQL statement
@UpdateString(Required)Custom UPDATE SQL statement
@DeleteString(Required)Custom DELETE SQL statement

The @Param annotation is used to mark method parameters. Please refer to @Param for documentation.

Parameter Description

ParameterTypeDefaultDescription
valueString(Required)Method parameter name used for SQL binding

Usage Example

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;

}

Multi-line Strings

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;

}

Property Access

Access object properties via ., access Array, List, Map elements via [], support nested access, and can be used in conditional statements and #{} placeholders.

Usage ScenarioExampleDescription
Object Property Accessaccount.usernameAccess object properties
Array/List AccessdataList[0]Access Array or List element by index
Map Element Accesssettings['theme']Access Map value by key
Combined Accessuser.address[0].cityAccess city of first address in user's address list

Operator Support

Supports arithmetic operations, comparison operations, logical operations, and parentheses for priority control, which can be used in conditional statements and placeholders.

Arithmetic Operators

OperatorDescriptionExample
+Additionage + 1
-Subtractionquantity - 5
*Multiplicationprice * 2
/Divisiontotal / count
%Modulusday % 7
^Exponentiationbase ^ exponent

Comparison Operators

OperatorDescriptionExample
>Greater thanage > 18
<Less thanscore < 60
>=Greater than or equalsalary >= 5000
<=Less than or equalquantity <= 100
==Equal tostatus == 'ACTIVE'
!=Not equal torole != 'ADMIN'

Logical Operators

OperatorDescriptionExample
&&Logical ANDage > 18 && status == 'ACTIVE'
||Logical OR`role == 'ADMIN'
!Logical NOT!isDeleted

Parentheses Priority

Use () to control operation priority.

kotlin
// Calculate multiplication first, then addition
price * (1 + discount)

// Calculate logical OR first, then logical AND
(role == 'ADMIN' || role == 'MANAGER') && status == 'ACTIVE'

Conditional Statements

Supports if, else if, else and nested if statements:

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'
    }
""")

Supported Methods

Method NameFunction DescriptionSupported Data Types
length/sizeGet length/sizeCharSequence, Iterable, Array, Map
isEmptyCheck if emptyCharSequence, Iterable, Array, Map
isNotEmptyCheck if not emptyCharSequence, Iterable, Array, Map
containsCheck if contains specified elementCharSequence, Iterable, Array, Map
containsIgnoreCaseCheck if contains specified string ignoring caseCharSequence
isBlankCheck if is blank stringCharSequence
isNotBlankCheck if is not blank stringCharSequence
toUpperCaseConvert to uppercaseCharSequence
toLowerCaseConvert to lowercaseCharSequence
startsWithCheck if starts with specified prefixCharSequence
endsWithCheck if ends with specified suffixCharSequence
trimRemove leading and trailing whitespaceCharSequence

Usage Examples

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") String username, @Param("email") String email);

Built with ❤️ by Tang