秦悦明的运维笔记

spring jdbcTemplate配置

1. 创建Account类和AccountDao接口

1
2
3
4
5
6
7
public class Account {
private long id;
private String ownerName;
private double balance;
private Date accessTime;
private boolean locked;
...
1
2
3
4
5
6
7
8
9
10
public interface AccountDao {
public void insert(Account account);
public void update(Account account);
public void update(List<Account> accounts);
public void delete(long accountId);
public Account find(long accountId);
public List<Account> find(List<Long> accountIds);
public List<Account> find(String ownerName);
public List<Account> find(boolean locked);
}

2. 实现AccountDao接口的AccountDaoJdbcImpl类

1
2
3
4
5
6
7
8
9
10
11
12
public class AccountDaoJdbcImpl implements AccountDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void insert(Account account) {
// TODO Auto-generated method stub
}

3. 使用jdbcTemplate类定义jdbcTemplate Bean,并使用dataSource Bean 满足DataSource其依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:tcp://localhost/~/test");
dataSource.setUsername("sa");
dataSource.setPassword("");
return dataSource;
}
@Bean
public JdbcTemplate jdbcTemplate() {
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource());
return jdbcTemplate;
}

4. 使用AccountDaoJdbcImpl创建accountDao bean,并将jdbcTemplate Bean注入其中:

1
2
3
4
5
6
@Bean
public AccountDao accountDao() {
AccountDaoJdbcImpl accountDao = new AccountDaoJdbcImpl();
accountDao.setJdbcTemplate(jdbcTemplate());
return accountDao;
}

5. main中查找accountDao bean

1
AccountDao accountDao = applicationContext.getBean(AccountDao.class);

之后便意味着可以在accountdaojdbcimpl中使用jdbctemplate 了。

6. 使用jdbctemplate运行查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public Account find(long accountId) {
return jdbcTemplate
.queryForObject(
"select id,owner_name,balance,access_time,locked from account where id = ?",
new RowMapper<Account>() {
public Account mapRow(ResultSet rs, int rowNum)
throws SQLException {
Account account = new Account();
account.setId(rs.getLong("id"));
account.setOwnerName(rs.getString("owner_name"));
account.setBalance(rs.getDouble("balance"));
account.setAccessTime(rs
.getTimestamp("access_time"));
account.setLocked(rs.getBoolean("locked"));
return account;
}
}, accountId);
}

使用jdbctemplate的 queryforobject方法。

调用参数是

1
2
3
4
public <T> T queryForObject(String sql, RowMapper<T> rowMapper, Object... args) throws DataAccessException {
List<T> results = (List)this.query((String)sql, (Object[])args, (ResultSetExtractor)(new RowMapperResultSetExtractor(rowMapper, 1)));
return DataAccessUtils.requiredSingleResult(results);
}

第一个参数是string的sql 语句,sql语句里面用问号做了占位符。第二个是RowMapper的接口,这里创建了一个匿名类,第三个之后是可变参数列表。

1
2
3
public interface RowMapper<T> {
T mapRow(ResultSet var1, int var2) throws SQLException;
}

RowMapper 里面就一个函数,mapRow,从ResultSet对象中获取值填充属性。比如这里的,new了一个Account对象,然后把ResultSet里面的属性填充一下就行了。

在main中这样调用即可:

1
2
AccountDao accountDao = applicationContext.getBean(AccountDao.class);
Account account = accountDao.find(100L);