Spring JDBC

##Spring JDBC

  *Spring框架对JDBC的简单封装。提供了一个JDBCTemplate对象简化JDBC开发。


*步骤:
1.导入jar包


2.创建JdbcTemplate对象。依赖于数据源DataSource

JdbcTemplate template = new JdbcTemplate(ds);

3.调用JdbcTemplate的方法完成增删改查的操作
  方法:
    1.update():执行增删改语句(DML)   

  /*改*/
  String sql = "update user set username = ? where username = ?";
  int count = template.update(sql,
"test","test1");
  System.out.println(count);

    

    2.queryForMap():查询结果将结果集封装为map集合

  Map map = template.queryForMap("select * from user where username = 'test'");
   System.out.println(map);

      注意:此方法查询的结果集只能是1,将列作为key,值作为value封装为map集合


    3.queryForList():查询结果将结果集封装为list集合

List list = template.queryForList("select * from user");
System.out.println(list);

        注意:将每一条记录封装为一个map集合,再将多个map集合封装为一个list结合


    4.query( , ):查询结果将结果集封装为JavaBean对象

List list3 = template.query("select * from user",new BeanPropertyRowMapper<User>(User.class));
System.out.println(list3);

      注意:重写RowMapper接口个,或BeanProperytRowMapper<类型>(类型.class)


    5.queryForObject:查询结果将结果集封装为对象
      *一般用于聚合函数的查询

posted @ 2020-02-16 10:32  不是正经鱼  阅读(126)  评论(0编辑  收藏  举报