2015-02-10 16:02:24 +00:00
|
|
|
|
### 28.2. 使用JdbcTemplate
|
|
|
|
|
|
|
|
|
|
Spring的JdbcTemplate和NamedParameterJdbcTemplate类是被自动配置的,你可以在自己的beans中通过@Autowire直接注入它们。
|
|
|
|
|
```java
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
@Component
|
|
|
|
|
public class MyBean {
|
|
|
|
|
|
|
|
|
|
private final JdbcTemplate jdbcTemplate;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
public MyBean(JdbcTemplate jdbcTemplate) {
|
|
|
|
|
this.jdbcTemplate = jdbcTemplate;
|
|
|
|
|
}
|
|
|
|
|
// ...
|
|
|
|
|
}
|
|
|
|
|
```
|