English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Spring propose une autre méthode d'insertion de données via des paramètres nommés. De cette manière, nous utilisons des noms à la place de ? (point d'interrogation). Par conséquent, il est préférable de se souvenir des données des colonnes.
insert into employee values (:id,:name,:salary)
Dans cet exemple, nous appellerons simplement la méthode execute de la classe NamedParameterJdbcTemplate. La syntaxe de cette méthode est la suivante:
public T execute(String sql, Map map, PreparedStatementCallback psc)
Nous supposons que vous avez déjà installé Oracle10le tableau suivant a été créé dans la base de données g.
create table employee( number d'id(10), nom varchar2(100), number de salaire(10) );
Employee.java
Cette classe contient3une propriété avec constructeur, setter et getter.
package com.w3codebox; public class Employee { private int id; private String name; private float salary; //non-constructeurs par arguments et paramétrés //getters and setters }
EmployeeDao.java
Il contient une propriété jdbcTemplate et une méthode de sauvegarde.
package com.w3codebox; import java.sql.PreparedStatement; import java.sql.SQLException; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.PreparedStatementCallback; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import java.util.*; public class EmpDao { NamedParameterJdbcTemplate template; public EmpDao(NamedParameterJdbcTemplate template) { this.template = template; } public void save (Emp e){ String query="insert into employee values (:id,:name,:salary)"; Map<String,Object> map=new HashMap<String,Object>(); map.put("id",e.getId()); map.put("name",e.getName()); map.put("salary",e.getSalary()); template.execute(query,map,new PreparedStatementCallback() { @Override public Object doInPreparedStatement(PreparedStatement ps) lève SQLException, DataAccessException { return ps.executeUpdate(); } }); } }
applicationContext.xml
DriverManagerDataSource Utilisé pour inclure des informations sur la base de données, par exemple le nom de la classe du pilote, l'URL de connexion, le nom d'utilisateur et le mot de passe.
Dans la classe NamedParameterJdbcTemplate de type DriverManagerDataSource, il y a un nom
datasource
Les propriétés. Par conséquent, nous devons fournir une référence à l'objet DriverManagerDataSource pour les propriétés du data source dans la classe NamedParameterJdbcTemplate.
Ici, nous utilisons l'objet NamedParameterJdbcTemplate dans la classe EmployeeDao, donc nous le passons par le constructeur, mais vous pouvez également utiliser la méthode 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="jtemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> <constructor-arg ref="ds"></constructor-arg> </bean> <bean id="edao" class="com.w3codebox.EmpDao"> <constructor-arg> <ref bean="jtemplate"/> </constructor-arg> </bean> </beans>
SimpleTest.java
Ce type récupère un Bean à partir du fichier applicationContext.xml et appelle la méthode save.
package com.w3codebox; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class SimpleTest { public static void main(String[] args) { Resource r=new ClassPathResource("applicationContext.xml"); BeanFactory factory=new XmlBeanFactory(r); EmpDao dao=(EmpDao)factory.getBean("edao"); dao.save(new Emp(23,"sonoo",50000)); } }