执行SQL
作为例子要先准备一张表,并初始化一些数据
create table `test_user` (
`id` int(11) auto_increment,
`name` varchar(255),
`age` int,
`create_time` datetime,
primary key (`id`)
);
insert into `test_user` values (1, 'mali', 26, now());
insert into `test_user` values (2, 'dative', 32, now());
insert into `test_user` values (3, 'jon wes', 41, now());
insert into `test_user` values (4, 'mary', 66, now());
insert into `test_user` values (5, 'matt', 25, now());
查询并返回 List/Map
查询 age > 40
的数据,并返回 List/Map
形式
String querySql = "select * from test_user where age > 40";
List<Map<String, Object>> result = jdbcTemplate.queryForList(querySql);
数组传参
String querySql = "select * from test_user where age > ?";
Object[] queryArg = new Object[] { 40 };
List<Map<String, Object>> result = jdbcTemplate.queryForList(querySql, queryArg);
Map传参
String querySql = "select * from test_user where age > :age";
Map<String, Object> queryArg = Collections.singletonMap("age", 40);
List<Map<String, Object>> result = jdbcTemplate.queryForList(querySql, queryArg);
执行结果(列名为标题)
/--------------------------------------------\
| id | name | age | create_time |
|--------------------------------------------|
| 3 | jon wes | 41 | 2021-11-26 12:10:21.0 |
| 4 | mary | 66 | 2021-11-26 12:10:21.0 |
\--------------------------------------------/
查询并返回 List/DTO
查询 age > 40
的数据,并返回 TestUser
结果集
String querySql = "select * from test_user where age > 40";
List<TestUser> result = jdbcTemplate.queryForList(querySql, TestUser.class);
数组传参
String querySql = "select * from test_user where age > ?";
Object[] queryArg = new Object[] { 40 };
List<TestUser> result = jdbcTemplate.queryForList(querySql, queryArg, TestUser.class);
Map传参
String querySql = "select * from test_user where age > :age";
Map<String, Object> queryArg = Collections.singletonMap("age", 40);
List<TestUser> result = jdbcTemplate.queryForList(querySql, queryArg, TestUser.class);
执行结果(对象的属性名为标题)
/---------------------------------------------------\
| createTime | name | id | age |
|---------------------------------------------------|
| Fri Nov 26 12:12:03 CST 2021 | jon wes | 3 | 41 |
| Fri Nov 26 12:12:03 CST 2021 | mary | 4 | 66 |
\---------------------------------------------------/
查询并返回一条记录 Map结果
查询 age > 40
的数据,并返回 1
个结果,结果为 Map
形式。
警告
需要提示的是,执行的 SQL 语句一定要确保只会返回 1
个结果,否则会面临 dbVisitor 无法确认选择哪一个结果的问题。在这个情况下会引发异常。
String querySql = "select * from test_user where age > 40 order by age limit 1";
Map<String, Object> result = jdbcTemplate.queryForMap(querySql);
数组传参
String querySql = "select * from test_user where age > ? order by age limit 1";
Object[] queryArg = new Object[] { 40 };
Map<String, Object> result = jdbcTemplate.queryForMap(querySql, queryArg);
Map传参
String querySql = "select * from test_user where age > :age order by age limit 1";
Map<String, Object> queryArg = Collections.singletonMap("age", 40);
Map<String, Object> result = jdbcTemplate.queryForMap(querySql, queryArg);
执行结果(Map的 Key 是列名)
/--------------------------------------------\
| id | name | age | create_time |
|--------------------------------------------|
| 3 | jon wes | 41 | 2021-11-26 12:20:12.0 |
\--------------------------------------------/