Voici la structure de notre projet.
Fichier pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>spring.tx</groupId> | |
<artifactId>spring-tx</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>spring-tx</name> | |
<url>http://maven.apache.org</url> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<spring.version>3.2.0.RELEASE</spring.version> | |
<!-- JDK --> | |
<maven.compiler.source>1.7</maven.compiler.source> | |
<maven.compiler.target>1.7</maven.compiler.target> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-core</artifactId> | |
<version>${spring.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-context</artifactId> | |
<version>${spring.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-tx</artifactId> | |
<version>${spring.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-orm</artifactId> | |
<version>${spring.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>commons-dbcp</groupId> | |
<artifactId>commons-dbcp</artifactId> | |
<version>1.2.2</version> | |
</dependency> | |
<dependency> | |
<groupId>mysql</groupId> | |
<artifactId>mysql-connector-java</artifactId> | |
<version>5.1.26</version> | |
</dependency> | |
<dependency> | |
<groupId>javax.persistence</groupId> | |
<artifactId>persistence-api</artifactId> | |
<version>1.0</version> | |
</dependency> | |
<dependency> | |
<groupId>org.hibernate</groupId> | |
<artifactId>hibernate-core</artifactId> | |
<version>4.1.9.Final</version> | |
</dependency> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>3.8.1</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
</project> |
La classe User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.formation.spring.tx.model; | |
import javax.persistence.Column; | |
import javax.persistence.Entity; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.GenerationType; | |
import javax.persistence.Id; | |
import javax.persistence.Table; | |
@Entity | |
@Table(name="USER") | |
public class User { | |
@Id | |
@Column(name="ID", nullable = false) | |
private int id; | |
@Column(name="USERNAME", nullable = false) | |
private String username; | |
@Column(name="NAME", nullable = false) | |
private String name; | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public String getUsername() { | |
return username; | |
} | |
public void setUsername(String username) { | |
this.username = username; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
} |
L'interface UserDAO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.formation.spring.tx.dao; | |
import com.formation.spring.tx.model.User; | |
public interface UserDAO { | |
void insertUser(User user); | |
} |
L'implementation de l'interface est la classe suivante:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.formation.spring.tx.dao.impl; | |
import org.hibernate.SessionFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Service; | |
import org.springframework.transaction.annotation.Transactional; | |
import com.formation.spring.tx.dao.UserDAO; | |
import com.formation.spring.tx.model.User; | |
@Service(value="userDAOImpl") | |
@Transactional | |
public class UserDAOImpl implements UserDAO { | |
@Autowired | |
private SessionFactory sessionFactory; | |
public void insertUser(User user) { | |
sessionFactory.getCurrentSession().save(user); | |
} | |
} |
La classe de test Main.java:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.formation.spring.tx; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.context.support.ClassPathXmlApplicationContext; | |
import com.formation.spring.tx.dao.impl.UserDAOImpl; | |
import com.formation.spring.tx.model.User; | |
public class Main | |
{ | |
public static void main( String[] args ) | |
{ | |
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml"); | |
UserDAOImpl testBean = (UserDAOImpl) ctx.getBean("userDAOImpl"); | |
User user = new User(); | |
user.setUsername("ramzi"); | |
user.setName("dridi"); | |
testBean.insertUser(user); | |
} | |
} |
L'annotation @Service permet de déclarer un bean de service, c'est à dire de la couche métier.L'annotation @Transactional peut être utilisée pour indiquer au conteneur les méthodes qui doivent s'exécuter dans un contexte transactionnel.
Et finalement voici le fichier de configuration:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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:context="http://www.springframework.org/schema/context" | |
xmlns:tx="http://www.springframework.org/schema/tx" | |
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 | |
http://www.springframework.org/schema/context | |
http://www.springframework.org/schema/context/spring-context-3.0.xsd | |
http://www.springframework.org/schema/tx | |
http://www.springframework.org/schema/tx/spring-tx.xsd"> | |
<tx:annotation-driven proxy-target-class="true"/> | |
<context:component-scan | |
base-package="com.formation.spring.tx.dao.impl" /> | |
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > | |
<property name="driverClassName" value="com.mysql.jdbc.Driver"/> | |
<property name="url" value="jdbc:mysql://localhost/test"/> | |
<property name="username" value="root"/> | |
<property name="password" value=""/> | |
</bean> | |
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> | |
<property name="dataSource" ref="dataSource"></property> | |
<property name="hibernateProperties"> | |
<props> | |
<prop | |
key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> | |
<prop key="hibernate.show_sql">true</prop> | |
<prop key="hibernate.hbm2ddl.auto">update</prop> | |
</props> | |
</property> | |
<property name="packagesToScan" value="com.formation.spring.tx.model" /> | |
</bean> | |
<bean id="transactionManager" | |
class="org.springframework.orm.hibernate4.HibernateTransactionManager"> | |
<property name="sessionFactory" ref="sessionFactory"/> | |
</bean> | |
</beans> |
Vous trouvez en video ici
ReplyDeletehttps://www.youtube.com/channel/UCtpoCoqCZawknFWhHpJ-YkQ
bonne continuation
ReplyDelete