`

SimpleJdbcTemple

 
阅读更多

有表

create table user(

userId int auto_increment,

userName varchar(20),

password varchar(20),

primary key(userId)

)

对应的实体

 

class user{
     private int userId;
     private String userName;
     private String password;
}

 

 

private SimpleJdbcTemplate mainJdbc
 

1.queryForObject(...)

String sql="select * from user where id = ? limit 1;";
RowMapper<SpecialService> rm = ParameterizedBeanPropertyRowMapper.newInstance(User.class);
try {
	User user = mainJdbc.queryForObject(sql, rm, id);
	return specialService;
     } catch(Exception e) {
     }
 

 

2.query(...)

String sql="select * from user;";
RowMapper<User> rm = ParameterizedBeanPropertyRowMapper.newInstance(User.class);
List<User> users = mainJdbc.query(sql.toString(),rm);

 

 

3.getNamedParameterJdbcOperations().update(..)

User user=new User();
user.setUserName("linjia");
user.setPassword("linjia");

String sql="insert into user (userName,password) values (:userName,:password);";
KeyHolder keyHolder = new GeneratedKeyHolder();
mainJdbc.getNamedParameterJdbcOperations().update(sql, new BeanPropertySqlParameterSource(user), keyHolder);
int userId=keyHolder.getKey().intValue();

 

4.update

 

String sql="replace into user(id,username.password) value(:id,:username,:password)";
mainJdbc.update(sql, new BeanPropertySqlParameterSource(dating));
 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics