spring JDBC增删改查


新增:

String sql="insert into table (a,b) values (?,?)";
jdbcTemplate.update(sql,"aid","aname");

删除:

String sql="delete from table where a=?";
int a=jdbcTemplate.update(sql,"aid");

修改:

String sql="update table set a=?,b=? where c=?";
jdbcTemplate.update(sql,"aid","aname","pswd");

建表等。。
jdbcTemplate.execute("CREATE TABLE USER (user_id integer, name varchar(100))"); 

查询:

1.List
String sql="select * from table";
List> list=jdbcTemplate.queryForList(sql);
List list=jdbcTemplate.query(sql, new BeanPropertyRowMapper(Faemp.class));

List list = jdbcTemplate.query(sql, new MyRowMapper());
public class MyRowMapper implements RowMapper{
    @Override
	public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			Admin admin=new Admin();
			admin.setAid(rs.getInt("aid"));
			admin.setAname(rs.getString("aname"));
			admin.setApswd(rs.getString("apswd"));
			return admin;
	}	
}

2.Map
String sql="select gid from table";
Map m=jdbcTemplate.queryForMap(sql);

3.Object
String a=jdbcTemplate.queryForObject(sql, String.class);
int a=jdbcTemplate.queryForObject(sql, Integer.class);
Lang a=jdbcTemplate.queryForObject(sql, Lang.class);
4.批量操作
String sql="insert into user (name,deptid) values (?,?)";

List batchArgs=new ArrayList();
batchArgs.add(new Object[]{"caoyc",6});
batchArgs.add(new Object[]{"zhh",8});
batchArgs.add(new Object[]{"cjx",8});

jdbcTemplate.batchUpdate(sql, batchArgs);

 

你可能感兴趣的:(初学,CURD)