Spring JdbcTemplate 教程

Spring JdbcTemplate 是一種強(qiáng)大的機(jī)制,可以連接到數(shù)據(jù)庫(kù)并執(zhí)行SQL查詢。它內(nèi)部使用JDBC API,但消除了JDBC API的許多問(wèn)題。

JDBC API的問(wèn)題

JDBC API的問(wèn)題如下:

在執(zhí)行查詢之前和之后,我們需要編寫很多代碼,例如創(chuàng)建連接,語(yǔ)句,關(guān)閉結(jié)果集,連接等。 我們需要在數(shù)據(jù)庫(kù)邏輯上執(zhí)行異常處理代碼。 我們需要處理交易。 將所有這些代碼從一個(gè)數(shù)據(jù)庫(kù)邏輯重復(fù)到另一個(gè)數(shù)據(jù)庫(kù)邏輯是一項(xiàng)耗時(shí)的工作。

Spring JdbcTemplate的優(yōu)點(diǎn)

Spring JdbcTemplate消除了上述JDBC API的所有問(wèn)題。它提供了直接編寫查詢的方法,從而節(jié)省了大量的工作和時(shí)間。

Spring Jdbc方法

Spring框架提供了以下用于JDBC數(shù)據(jù)庫(kù)訪問(wèn)的方法:

JdbcTemplate NamedParameterJdbcTemplate SimpleJdbcTemplate SimpleJdbcInsert和SimpleJdbcCall

JdbcTemplate類

它是Spring JDBC支持類的中心類。它負(fù)責(zé)創(chuàng)建和釋放資源,例如創(chuàng)建和關(guān)閉連接對(duì)象等。因此,如果您忘記關(guān)閉連接,不會(huì)導(dǎo)致任何問(wèn)題。

它處理異常并提供信息異常消息是通過(guò) org.springframework.dao 包中定義的異常類的幫助。

我們可以借助JdbcTemplate類執(zhí)行所有數(shù)據(jù)庫(kù)操作,例如插入,更新,從數(shù)據(jù)庫(kù)中刪除和檢索數(shù)據(jù)。

讓我們看看spring JdbcTemplate類的方法。

方法說(shuō)明
public int update(String query)用于插入,更新和刪除記錄。
public int update(String query,Object ... args)用于通過(guò)使用給定參數(shù)的PreparedStatement插入,更新和刪除記錄。
public void execute(String query)用于執(zhí)行DDL查詢。
public T execute(String sql, PreparedStatementCallback action)通過(guò)使用PreparedStatement回調(diào)執(zhí)行查詢。
public T query(String sql, ResultSetExtractor rse)用于使用ResultSetExtractor獲取記錄。
public List query(String sql, RowMapper rse)用于使用RowMapper獲取記錄。

Spring JdbcTemplate的示例

我們假設(shè)您已經(jīng)在Oracle10g數(shù)據(jù)庫(kù)中創(chuàng)建了下表。

create table employee(
id number(10),
name varchar2(100),
salary number(10)
);

Employee.java

此類包含3個(gè)帶有構(gòu)造函數(shù),setter和getter的屬性。

package com.nhooo;
public class Employee {
private int id;
private String name;
private float salary;
//無(wú)參數(shù)和參數(shù)化的構(gòu)造函數(shù)
//getters and setters
}

EmployeeDao.java

它包含一個(gè)屬性jdbcTemplate和三個(gè)方法saveEmployee(),updateEmployee和deleteEmployee()。

package com.nhooo;
import org.springframework.jdbc.core.JdbcTemplate;
public class EmployeeDao {
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
      this.jdbcTemplate = jdbcTemplate;
    }
    public int saveEmployee(Employee e){
      String query="insert into employee values(  '"+e.getId()+"','"+e.getName()+"','"+e.getSalary()+"')";
      return jdbcTemplate.update(query);
    }
    public int updateEmployee(Employee e){
      String query="update employee set 
      name='"+e.getName()+"',salary='"+e.getSalary()+"' where id='"+e.getId()+"' ";
      return jdbcTemplate.update(query);
    }
    public int deleteEmployee(Employee e){
      String query="delete from employee where id='"+e.getId()+"' ";
      return jdbcTemplate.update(query);
    }
}

applicationContext.xml

DriverManagerDataSource 用于包含有關(guān)數(shù)據(jù)庫(kù)的信息,例如驅(qū)動(dòng)程序類名稱,連接URL,用戶名和密碼。

DriverManagerDataSource類型的JdbcTemplate類中有一個(gè)名為 datasource 的屬性。因此,我們需要在JdbcTemplate類中為數(shù)據(jù)源屬性提供DriverManagerDataSource對(duì)象的引用。

在這里,我們?cè)贓mployeeDao類中使用了JdbcTemplate對(duì)象,因此我們通過(guò)setter方法傳遞了它,但是您也可以使用構(gòu)造函數(shù)。

<?xml version="1.0" encoding="UTF-8"?>
<beans
  xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="username" value="system" />
<property name="password" value="oracle" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"></property>
</bean>
<bean id="edao" class="com.nhooo.EmployeeDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
</beans>

Test.java

此類從applicationContext.xml文件獲取Bean,并調(diào)用saveEmployee()方法。您也可以通過(guò)取消注釋代碼來(lái)調(diào)用updateEmployee()和deleteEmployee()方法。

package com.nhooo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
  ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
  
  EmployeeDao dao=(EmployeeDao)ctx.getBean("edao");
  int status=dao.saveEmployee(new Employee(102,"Amit",35000));
  System.out.println(status);
    
  /*int status=dao.updateEmployee(new Employee(102,"Sonoo",15000));
  System.out.println(status);
  */
    
  /*Employee e=new Employee();
  e.setId(102);
  int status=dao.deleteEmployee(e);
  System.out.println(status);*/
  
}
}
丰满人妻一级特黄a大片,午夜无码免费福利一级,欧美亚洲精品在线,国产婷婷成人久久Av免费高清