美文网首页
jdbcTemplate helloworld

jdbcTemplate helloworld

作者: simplerandom | 来源:发表于2020-06-09 14:20 被阅读0次
<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());
        }

    }
}

相关文章

网友评论

      本文标题:jdbcTemplate helloworld

      本文链接:https://www.haomeiwen.com/subject/rwvstktx.html