jdbcTemplate常用操作

本篇文章主要介紹使用jdbcTemplate來進行增刪改查

那麼其實在使用的過程中呢,主要也就是使用query 和  update 操作。(直接列出詳細的例子,特殊點會備註說明)

(一)Query

(1)  public <T> T queryForObject(String sql, Class<T> requiredType)

public void queryForCount() {
		String sql = "select count(1) from person";
		Integer num = jdbcTemplate.queryForObject(sql, Integer.class);
		System.out.println(num);
	}

(2) public <T> List<T> queryForList(String sql, Class<T> elementType)

public void queryAllId() {
		String sql = "select id from person";
		List<Integer> ids = jdbcTemplate.queryForList(sql, Integer.class);
		System.out.println(Arrays.asList(ids));
	}

這裏特殊說明一下,queryForList後面的這個elementType是隻支持基礎類型,不支持自定義類型,詳細可以看我的上一篇文章

queryForList的坑

(3)  public <T> List<T> query(String sql, RowMapper<T> rowMapper)

public void queryAll() {
		String sql = "select * from person";
		List<Person> persons = jdbcTemplate.query(sql, new PersonRowMapper());
		System.out.println(JSONArray.toJSONString(persons));
	}

這裏重點說一下,上面提到了就是queryForList是不支持自定義bean的,那麼如果支持自定義bean呢?就可以藉助rowMapper來實現了

public class PersonRowMapper implements RowMapper<Person>{

	@Override
	public Person mapRow(ResultSet rs, int rowNum) throws SQLException {
		Person person = new Person();
		person.setId(rs.getInt("id"));
		person.setAge(rs.getInt("age"));
		person.setName(rs.getString("name"));
		person.setPassword(rs.getString("password"));
		return person;
	}

}

(4) public <T> List<T> query(String sql, @Nullable Object[] args, RowMapper<T> rowMapper)

public void queryById() {
		String sql = "select * from person where id = ?";
		List<Person> personList = jdbcTemplate.query(sql, new Object[] {1}, new PersonRowMapper());
		if(!CollectionUtils.isEmpty(personList)) {
			System.out.println(personList.get(0).toString());
		}
	}

(二) Update

(1) public int update(final PreparedStatementCreator psc, final KeyHolder generatedKeyHolder)

public void add() {
		String sql = "insert into person values(null,?,?,?)";
		KeyHolder keyHolder = new GeneratedKeyHolder();
		jdbcTemplate.update(new PreparedStatementCreator() {
			
			@Override
			public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
				PreparedStatement ps = con.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
				ps.setString(1, "xiongda");
				ps.setInt(2, 28);
				ps.setString(3, "666666");
				return ps;
			}
		},keyHolder);
		System.out.println("id="+keyHolder.getKey().toString());
	}

這裏需要提一下keyHolder,如果你想新增後返回新增記錄的主鍵id,那麼就可以使用以上的方式,注意加上Statement.RETURN_GENERATED_KEYS

(2) public int update(String sql, @Nullable Object... args)

public void deleteById() {
		String sql = "delete from person where id = ?";
		jdbcTemplate.update(sql, 19);
	}

(3) public int update(String sql, @Nullable PreparedStatementSetter pss)

public void updateById() {
		String sql = "update person set name=?, password=? where id=?";
		jdbcTemplate.update(sql, new PreparedStatementSetter() {
			
			@Override
			public void setValues(PreparedStatement ps) throws SQLException {
				ps.setString(1, "xiong");
				ps.setString(2, "root");
				ps.setInt(3, 18);
			}
		});
	}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章