Spring的JdbcTemplate使用 (三)

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class Test2 {

    @Resource(name="jdbcTemplate")
    private JdbcTemplate jdbcTemplate;


    @Test
    public void  testQuerySimple(){
        String sql="select count(*)  from student";

       int  count= jdbcTemplate.queryForObject(sql,Integer.class);

        System.out.println(count);
    }

    @Test
    public void  testQuerySimple2(){
        String sql="select name from student where sex=?";



        List<String> names= jdbcTemplate.queryForList(sql,String.class,"女");

        System.out.println(names);
    }


    @Test
    public void  testQueryEntity1(){
        String sql="select * from student where id=?";



        Student student= jdbcTemplate.queryForObject(sql, new RowMapper<Student>() {
            public Student mapRow(ResultSet resultSet, int i) throws SQLException {
                Student  student=new Student();
                student.setId(resultSet.getInt("id"));
                student.setName(resultSet.getString("name"));
                student.setSex(resultSet.getString("sex"));
                student.setBorn(resultSet.getDate("born"));

                return student;
            }
        }, 3);

        System.out.println(student);
    }


    @Test
    public void  testQueryEntity2(){
        String sql="select * from student";



        List<Student> student= jdbcTemplate.query(sql, new RowMapper<Student>() {
            public Student mapRow(ResultSet resultSet, int i) throws SQLException {
                Student  student=new Student();
                student.setId(resultSet.getInt("id"));
                student.setName(resultSet.getString("name"));
                student.setSex(resultSet.getString("sex"));
                student.setBorn(resultSet.getDate("born"));

                return student;
            }
        });

        System.out.println(student);
    }
发布了304 篇原创文章 · 获赞 179 · 访问量 9万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术工厂 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览