歡迎您光臨本站 註冊首頁

JDBC Template基本使用方法詳解

←手機掃碼閱讀     zhang3221994 @ 2020-06-12 , reply:0

1.使用maven引用依賴

  junitjunit4.11org.springframeworkspring-context5.2.5.RELEASEorg.springframeworkspring-core5.2.5.RELEASEorg.springframeworkspring-beans5.2.5.RELEASEorg.springframeworkspring-aop5.2.5.RELEASEorg.springframeworkspring-jdbc5.2.5.RELEASEorg.springframeworkspring-tx5.2.5.RELEASEmysqlmysql-connector-java8.0.19

 

2.編寫spring的xml檔案

  

 

3.ddl操作(建表之類)

  import org.junit.Test;  import org.springframework.context.ApplicationContext;  import org.springframework.context.support.ClassPathXmlApplicationContext;  import org.springframework.jdbc.core.JdbcTemplate;  public class test {  @Test    public void test(){      ApplicationContext applicationContext=new ClassPathXmlApplicationContext("myApplication.xml");      JdbcTemplate springTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");      springTemplate.execute("create table test(id int,username varchar(10))");    }  }

 

4.增刪改:

對應的使用例子

  public void testUpdate(){      String sql = "insert into student(name,sex) values(?,?)";      jdbcTemplate.update(sql,new Object[]{"張飛","男"});    }      public void testUpdate2(){      String sql = "update student set sex=? where id=?";      jdbcTemplate.update(sql,"女",1003);    }      public void testBatchUpdate(){      String[] sqls={          "insert into student(name,sex) values('關羽','女')",          "insert into student(name,sex) values('劉備','男')",          "update student set sex='女' where id=2001"      };      jdbcTemplate.batchUpdate(sqls);    }      public void testBatchUpdate2(){      String sql = "insert into selection(student,course) values(?,?)";      Listlist = new ArrayList();      list.add(new Object[]{1005,1001});      list.add(new Object[]{1005,1003});      jdbcTemplate.batchUpdate(sql,list);    }

 

5.查詢

  public void testQuerySimple1(){      String sql = "select count(*) from student";      int count = jdbcTemplate.queryForObject(sql,Integer.class);      System.out.println(count);    }      public void testQuerySimple2(){      String sql = "select name from student where sex=?";      Listnames = jdbcTemplate.queryForList(sql,String.class,"女");      System.out.println(names);    }      public void testQueryMap1(){      String sql = "select * from student where id = ?";      Mapstu = jdbcTemplate.queryForMap(sql,1003);      System.out.println(stu);    }      public void testQueryMap2(){      String sql = "select * from student";      List stus = jdbcTemplate.queryForList(sql);      System.out.println(stus);    }      public void testQueryEntity1(){      String sql = "select * from student where id = ?";      Student stu = jdbcTemplate.queryForObject(sql, new StudentRowMapper(), 1004);      System.out.println(stu);    }    @org.junit.Test    public void testQueryEntity2(){      String sql = "select * from student";      Liststus = jdbcTemplate.query(sql,new StudentRowMapper());      System.out.println(stus);    }      private class StudentRowMapper implements RowMapper{      public Student mapRow(ResultSet resultSet, int i) throws SQLException {        Student stu = new Student();        stu.setId(resultSet.getInt("id"));        stu.setName(resultSet.getString("name"));        stu.setSex(resultSet.getString("sex"));        stu.setBorn(resultSet.getDate("born"));        return stu;      }    }

 

                                                       

   


[zhang3221994 ] JDBC Template基本使用方法詳解已經有256次圍觀

http://coctec.com/docs/mysql/show-post-238303.html