English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
باستخدام خاصية
execute()
يمكننا استخدام Spring JdbcTemplate للتنفيذ الاستعلامات الم参数ية. لاستخدام الاستعلامات الم参数ية، نقوم بمرور
PreparedStatementCallback
النموذج.
public T execute(String sql,PreparedStatementCallback<T>);
يتعامل مع المعطيات المدخلة والنتائج الصادرة. في هذه الحالة، لا تحتاج إلى الاهتمام بالكوادرات البسيطة والكوادرات المزدوجة.
طريقة PreparedStatementCallbackيحتوي على طريقة واحدة فقط هي doInPreparedStatement. لغة هذه الطريقة هي:
public T doInPreparedStatement(PreparedStatement ps)throws SQLException, DataAccessException
نفترض أنك قد أنشأت الجدول التالي في قاعدة بيانات Oracle10g.
create table employee( id number(10), name varchar2(100), salary number(10) );
Employee.java
يحتوي هذا الكائن على 3 خصائص تحتوي على بناء (constructors)، وطرق تعيين (setters) وطرق الحصول (getters).
package com.w3codebox; public class Employee { private int id; private String name; private float salary; //no-arg and parameterized constructors //getters and setters }
EmployeeDao.java
يحتوي على خاصية jdbcTemplate وطرقة saveEmployeeByPreparedStatement. يجب أن تفهم مفهوم الصيغ الغير معروفة (anonymous classes) لفهم كود هذه الطريقة.
package com.w3codebox; import java.sql.PreparedStatement; import java.sql.SQLException; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.PreparedStatementCallback; public class EmployeeDao { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public boolean saveEmployeeByPreparedStatement(final Employee e){ String query="insert into employee values(?,?,?)"; return jdbcTemplate.execute(query, new PreparedStatementCallback<Boolean>(){ @Override public boolean doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException { ps.setInt(1, e.getId()); ps.setString(2, e.getName()); ps.setfloat(3, e.getSalary()); return ps.execute(); } ); } }
applicationContext.xml
DriverManagerDataSource لإدراج معلومات قاعدة البيانات مثل اسم فئة المحرك، عنوان الاتصال، اسم المستخدم وكلمة المرور.
في فئة JdbcTemplate من نوع DriverManagerDataSource هناك اسم
datasource
الصفات. لذلك، يجب علينا تقديم استدلال DriverManagerDataSource كمرجع لصفات المصدر في فئة 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 ويقوم بتشغيل طريقة saveEmployeeByPreparedStatement().
package com.w3codebox; 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"); dao.saveEmployeeByPreparedStatement(new Employee(108,"Amit",35000)); } }