Tuesday, October 6, 2015

Tutoriel sur Spring Data :intéraction avec la base de donnée

Cet article s'intéresse à « Spring Data ». Il permet l'accès aux données et d'avoir une couche d'abstraction commune à de multiples sources de données.

voici notre classe entité
package com.formation.model;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="compte")
public class Compte {
@Id
private String numero;
private String proprietaire;
public Compte(String numero, String proprietaire, float solde) {
super();
this.numero = numero;
this.proprietaire = proprietaire;
this.solde = solde;
}
public Compte() {
// TODO Auto-generated constructor stub
}
private float solde;
public String getNumero() {
return numero;
}
public void setNumero(String numero) {
this.numero = numero;
}
public String getProprietaire() {
return proprietaire;
}
public void setProprietaire(String proprietaire) {
this.proprietaire = proprietaire;
}
public float getSolde() {
return solde;
}
public void setSolde(float solde) {
this.solde = solde;
}
}
view raw Compte.java hosted with ❤ by GitHub




On doit créer l'interface,



package com.formation.dataccess;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import com.formation.model.Compte;
public interface CompteRepositoy extends JpaRepository<Compte, String>{
public List<Compte> findByProprietaireLike(String nom);
}


dans le fichier database.properties on doit mettre ces lignes:

jdbc.url=jdbc:mysql://localhost:3306/Banque_SpringData_DB?createDatabaseIfNotExist=true
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=

et ceci le fichier de configuration de spring

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<context:property-placeholder location="database.properties" />
<jpa:repositories base-package="com.formation.dataccess" />
<!-- DataSource avec un pool de connection Hikari CP -->
<bean id="datasource_pool_hikari" class="com.zaxxer.hikari.HikariDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClassName" value="${jdbc.driverClass}"></property>
<!-- See Configuration Hikari https://github.com/brettwooldridge/HikariCP -->
<property name="minimumIdle" value="5"></property>
<property name="maximumPoolSize" value="20"></property>
</bean>
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="database" value="MYSQL" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="datasource_pool_hikari"></property>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"></property>
<property name="packagesToScan" value="com.formation.model"></property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"></bean>
</beans>
view raw beans.xml hosted with ❤ by GitHub


La classe de test est :
package com.formation.springdata;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.formation.dataccess.CompteRepositoy;
import com.formation.model.Compte;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
"beans.xml");
CompteRepositoy compterep=context.getBean(CompteRepositoy.class);
compterep.save(new Compte("1", "ramzi", 2000));
}
}
view raw App.java hosted with ❤ by GitHub


Voici le video de l'exemple:








1 comment:

  1. Vous trouvez autres videos ici

    https://www.youtube.com/channel/UCtpoCoqCZawknFWhHpJ-YkQ

    ReplyDelete