一、JdbcTemplate操作数据库-单条数据
1、调用 JdbcTemplate 方法实现查询表 记录数
// 查询
@Override
public int selectCount() {
// 1创建sql语句
String sql = "select count(*) from t_user";
int count = jdbcTemplate.queryForObject(sql, Integer.class);
return count;
}
测试
// 查询
@Test
public void query() {
ApplicationContext context =
new ClassPathXmlApplicationContext("bean1.xml");
UserService userService = context.getBean("userService", UserService.class);
int count = userService.findCount();
System.out.println("表记录数"+count);
}
2、调用 JdbcTemplate 方法实现查询返回对象
UserDaoImpl.java
@Override
public User findUserInfo(String id) {
// 1创建sql语句
String sql = "select * from t_user where userid=?";
User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class),id);
return user;
}
// 查询