Skip to main content
Hint

This article is generated by AI translation.

Named Arguments

Use :name, &name, or #{...} to name arguments in SQL.

select * from users where id > :id   and name = :name
select * from users where id > &id and name = &name
select * from users where id > #{id} and name = #{name}

Basic Usage

Example 1: Use a Map as the argument container
Map<String, Object> args = CollectionUtils.asMap(
"id", 2,
"Dave", true
);
jdbcTemplate.queryForList("select * from users where id > :id and name = :name", args);
Example 2: Use a Bean as the argument container
public class User {
private int id;
private String name;

public User(int id, String name) {
this.id = id;
this.name = name;
}
// getters and setters omitted
}

User args = new User(2, "Dave");
jdbcTemplate.queryForList("select * from users where id > :id and name = :name", args);

OGNL Evaluation

All three named argument syntaxes (:name, &name, #{...}) support OGNL expressions, allowing nested property access, array/collection indexing, and method calls.

Nested Properties

Example: Extract a nested value
{
"p": {
"name": "Dave",
"cfg_id": {
"array": [
{"age": 10},
{"age": 40} <<< use this value
]
}
}
}

Use the OGNL expression p.cfg_id.array[1].age to extract the argument from the above structure.

Syntax 1: Colon syntax
select * from user_table where age > :p.cfg_id.array[1].age order by id
Syntax 2: Curly brace syntax
select * from user_table where age > #{p.cfg_id.array[1].age} order by id

Array and Collection Indexing

OGNL expressions can access List and array elements directly via square brackets [index].

Example: Collection index access
Map<String, Object> params = new HashMap<>();
params.put("ages", Arrays.asList(30, 35, 40));
params.put("names", new String[] { "Alice", "Bob" });

// Using colon syntax
jdbcTemplate.queryForList(
"select * from users where age = :ages[0] and name = :names[0]", params);

// Using curly brace syntax
jdbcTemplate.queryForList(
"select * from users where age = #{ages[0]} and name = #{names[0]}", params);

Argument Reuse

The same named argument can appear multiple times in SQL. dbVisitor automatically binds it to the same value.

Example: Using the same argument name twice
insert into users (id, name, email) values (#{id}, #{name}, #{name})

Argument Options

Example 1: Specify a TypeHandler
select * from users where
id > #{id,typeHandler=net.hasor.dbvisitor.types.handler.number.LongTypeHandler}
and
name = #{name,typeHandler=net.hasor.dbvisitor.types.handler.string.StringTypeHandler}
Example 2: Receive a stored procedure OUT argument
{call proc_bigint(#{out,mode=out,jdbcType=bigint})}
More info
  • The :name and &name syntaxes do not support argument options.
  • For more argument options, see Argument Options.

Ignore Rule

If a colon : is followed immediately by whitespace (space, newline, tab, etc.), dbVisitor ignores argument parsing for that colon and treats it as a normal character. This makes it easy to write JSON or MongoDB-like queries directly in SQL without escaping colons.

-- The colon is followed by a space, so it is not treated as an argument
select * from table where config = '{ "key": "value" }'

Additional special handling to note:

  • PostgreSQL type cast :: is not recognized as an argument, so you can safely use column::integer syntax.
  • Quoted content Argument syntax inside single quotes '...' and double quotes "..." is skipped.
  • SQL comments Argument syntax inside /* ... */ and -- ... is skipped.
  • Escaping Use \#{...}, \${...}, \@{...} to escape them as plain text.