需求如下:
/*
4. 查询id为1的记录,将其封装为Map集合
5. 查询所有记录,将其封装为List
6. 查询所有记录,将其封装为Emp对象的List集合
7. 查询总记录数
*/
public class MyTemplateQuery {
JdbcTemplate template = new JdbcTemplate(DruidUtils.getDataSource());
@After
public void test1(){
String sql = "select * from dept where id = ?";
Map<String, Object> som = template.queryForMap(sql,1);
System.out.println(som);
}
@After
public void test2(){
String sql = "select * from dept";
List<Map<String, Object>> maps = template.queryForList(sql);
for (Map<String, Object> map : maps) {
System.out.println(map);
}
}
@Test
public void test3(){
String sql = "select * from dept";
List<Dept> list = template.query(sql, new BeanPropertyRowMapper<Dept>(Dept.class));
for (Dept dept : list) {
System.out.println(dept);
}
}
@Test
public void test4(){
String sql = "select count(id) from dept";
Long count = template.queryForObject(sql, long.class);
System.out.println(count);
}
}
欢迎一起交流学习! |
|