在使用普通的JDBC數據庫時,就會很麻煩的寫沒必要要的代碼來處理異常,打開和關閉數據庫鏈接等。但Spring JDBC框架負責全部的低層細節,從開始打開鏈接,準備和執行SQL語句,處理異常,處理事務,到最後關閉鏈接。java
JdbcTemplate類執行SQL查詢,更新語句和存儲過程調用,執行迭代結果集和提取返回參數值。它也捕獲JDBC異常並轉換它們到org.springframework.dao包中定義的通用類,更多的信息,異常層次結構。mysql
JdbcTemplate類的實例是線程安全配置的。因此你能夠配置JdbcTemplate的單個實例,而後將這個共享的引用安全地注入到多個DAOs中。spring
使用JdbcTemplate類時常見的作法是在你的Spring配置文件中配置數據源,而後共享數據源bean依賴注入到DAO類中,並在數據源的設值函數中建立了JdbcTemplate。sql
如今,咱們須要提供一個數據源到JdbcTemplate中,因此它能夠配置自己來得到數據庫訪問。你能夠在XML文件中配置數據源,其中一段代碼以下所示:數據庫
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/TEST"/> <property name="username" value="root"/> <property name="password" value="password"/> </bean>
DAO表明經常使用的數據庫交互的數據訪問對象。DAO提供一種方法來讀取數據並將數據寫入到數據庫中,它們應該經過一個接口顯示此功能,應用程序的其他部分將訪問它們。安全
在Spring中,數據訪問對象(DAO)支持很容易用統一的方法使用數據訪問技術,如JDBC,Hibernate,JPA或者JDO。app
咱們看看如何使用SQL和jdbcTemplate對象在數據庫表中執行CRUD(建立,讀取,更新和刪除)操做。框架
public class StudentJDBCTemplate { private DataSource dataSource; private JdbcTemplate jdbcTemplateObject; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; this.jdbcTemplateObject = new JdbcTemplate(dataSource); } public void create(String name, Integer age) { String SQL = "insert into Student (name, age) values (?, ?)"; jdbcTemplateObject.update( SQL, name, age); System.out.println("Created Record Name = " + name + " Age = " + age); return; } public Student getStudent(Integer id) { String SQL = "select * from Student where id = ?"; Student student = jdbcTemplateObject.queryForObject(SQL, new Object[]{id}, new StudentMapper()); return student; } public List<Student> listStudents() { String SQL = "select * from Student"; List <Student> students = jdbcTemplateObject.query(SQL, new StudentMapper()); return students; } public void delete(Integer id){ String SQL = "delete from Student where id = ?"; jdbcTemplateObject.update(SQL, id); System.out.println("Deleted Record with ID = " + id ); return; } public void update(Integer id, Integer age){ String SQL = "update Student set age = ? where id = ?"; jdbcTemplateObject.update(SQL, age, id); System.out.println("Updated Record with ID = " + id ); return; } }
public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); StudentJDBCTemplate studentJDBCTemplate = (StudentJDBCTemplate)context.getBean("studentJDBCTemplate"); System.out.println("------Records Creation--------" ); studentJDBCTemplate.create("Zara", 11); studentJDBCTemplate.create("Nuha", 2); studentJDBCTemplate.create("Ayan", 15); System.out.println("------Listing Multiple Records--------" ); List<Student> students = studentJDBCTemplate.listStudents(); for (Student record : students) { System.out.print("ID : " + record.getId() ); System.out.print(", Name : " + record.getName() ); System.out.println(", Age : " + record.getAge()); } System.out.println("----Updating Record with ID = 2 -----" ); studentJDBCTemplate.update(2, 20); System.out.println("----Listing Record with ID = 2 -----" ); Student student = studentJDBCTemplate.getStudent(2); System.out.print("ID : " + student.getId() ); System.out.print(", Name : " + student.getName() ); System.out.println(", Age : " + student.getAge()); } }
<!-- Initialization for data source --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/TEST"/> <property name="username" value="root"/> <property name="password" value="password"/> </bean> <!-- Definition for studentJDBCTemplate bean --> <bean id="studentJDBCTemplate" class="com.tutorialspoint.StudentJDBCTemplate"> <property name="dataSource" ref="dataSource" /> </bean>