<dependencies>
<!-- jdbcTemplate 使用的 jar 包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.42</version>
</dependency>
</dependencies>
select代码
public class DataSource {
public static void main(String[] args) {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///hello");
dataSource.setUsername("test");
dataSource.setPassword("test");
// ---------------
String sql="select * from user";
BeanPropertyRowMapper<User> mapper = new BeanPropertyRowMapper<>(User.class);
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
List<User> query = jdbcTemplate.query(sql, mapper);
for (User user : query) {
System.out.println(user.getUsername());
}
}
}
网友评论