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
Map<String, Object> args = CollectionUtils.asMap(
"id", 2,
"Dave", true
);
jdbcTemplate.queryForList("select * from users where id > :id and name = :name", args);
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
{
"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.
select * from user_table where age > :p.cfg_id.array[1].age order by id
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].
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.
insert into users (id, name, email) values (#{id}, #{name}, #{name})
Argument Options
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}
{call proc_bigint(#{out,mode=out,jdbcType=bigint})}
- The
:nameand&namesyntaxes 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 usecolumn::integersyntax. - 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.