English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

مثال ResultSetExtractor|الحصول على السجلات من خلال Spring JdbcTemplate

نستطيع استخدام JdbcTemplate الصف query()يمكن للمتدخل بسهولة الحصول على سجلات من قاعدة البيانات،نحتاج إلى نقل مثال على ResultSetExtractor.

جملة لغة طريقة استعلام باستخدام ResultSetExtractor

public T query(String sql,ResultSetExtractor<T> rse)

واجهة ResultSetExtractor

ResultSetExtractor يمكن استخدام الواجهة لتسجيل الحصول على سجلات من قاعدة البيانات. تقبل ResultSet وتعيد قائمة.

طرق واجهة ResultSetExtractor

يحدد فقط طريقة واحدة هي ResultResult كمعامل يدخل. جملة اللغة كما يلي:

public T extractData(ResultSet rs)throws SQLException,DataAccessException

مثال على واجهة ResultSetExtractor لعرض جميع سجلات الجدول

نفترض أنك قد أنشأت الجداول التالية في قاعدة بيانات Oracle10g.

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

Employee.java

يحتوي هذا الكائن على 3 خصائص تحتوي على بناء،setter وgetter. يحدد طريقة toString() إضافية.

مكتبة com.w3codebox;
public class Employee {
private int id;
private String name;
private float salary;
//no-arg and parameterized constructors
//getters and setters
public String toString(){
    return id+" "+name+" "+salary;
}
}

EmployeeDao.java

يحتوي على الخاصية jdbcTemplate وواحدة من طرق getAllEmployees.

مكتبة com.w3codebox;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
إدراج java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
public class EmployeeDao {
private JdbcTemplate template;
public void setTemplate(JdbcTemplate template) {
    this.template = template;
}
public List<Employee> getAllEmployees(){
 return template.query("select * from employee",new ResultSetExtractor<List<Employee>>(){
    @Override
     public List<Employee> extractData(ResultSet rs) throws SQLException,
            DataAccessException {
        List<Employee> list=new ArrayList<Employee>();
        while(rs.next()){
        Employee e=new Employee();
        e.setId(rs.getInt(1));
        e.setName(rs.getString(2));
        e.setSalary(rs.getInt(3));
        list.add(e);
        }
        return list;
        }
    });
  }
}

applicationContext.xml

DriverManagerDataSource للإعداد معلومات قاعدة البيانات مثل اسم فئة محرك التشغيل، عنوان الاتصال، اسم المستخدم وكلمة المرور.

يوجد في فئة JdbcTemplate من نوع DriverManagerDataSource اسم datasource لذلك، نحتاج إلى تقديم استدلال DriverManagerDataSource لسمة DataSource في فئة JdbcTemplate.

في هذا المكان، نستخدم في فئة EmployeeDao كائن JdbcTemplate، لذا نرسله عبر طريقة setter، ولكن يمكنك أيضًا استخدامه في بناء الكائن.

<?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.w3codebox.EmployeeDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
</beans>

Test.java

يأخذ هذا النوع Bean من ملف applicationContext.xml ويقوم بتشغيل طريقة getAllEmployees() من كلاس EmployeeDao.

مكتبة com.w3codebox;
إدراج java.util.List;
إدراج org.springframework.context.ApplicationContext;
إدراج 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");
    List<Employee> list=dao.getAllEmployees();
    لـ for(Employee e:list)
        System.out.println(e);
    }
}