בניית פרוייקט עם Spring MVC, Hibernate, Maven

בפוסט זה אדגים כיצד לבנות פרוייקט end-to-end בטכנולוגיות: Spring MVC, Hibernate קומפילציה באמצעות Maven והרצה כWAR על Tomcat או כל שרת אפליקציות ג'אווה אחר. הפרוייקט יכלול בסיס נתונים MySQL עם טבלה השומרת פרטי לקוחות, דף JSP שמציג נתונים מהטבלה, שרות REST מבוסס Spring MVC לקבלה ושליחת נתונים ופעולות מול הDatabase באמצעות Hibernate.

ראשית נקים את התשתיות באמצעות הגדרת הpom.xml של הMaven. למען הנוחות, הפרוייקט יכלול מודול יחיד עם כל הקוד. מומלץ לפצל פרוייקטים אמיתיים למספר מודולים כמו: persistence, dao, web וכו'.

לגרסה האנגלית: Maven, Spring, Hibernate Web Tutorial

להלן הקוד של הpom.xml:


<!--?xml version="1.0"?-->
<project xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelversion>4.0.0</modelversion>

    <repositories>
        <repository>
            <id>jboss-public-repository-group</id>
            <name>JBoss Public Repository Group</name>
            <url>https://repository.jboss.org/nexus/content/repositories/releases/</url>
        </repository>

        <repository>
            <id>SpringSource Enterprise Bundle Repository – External Bundle Milestones</id>
            <url>http://repository.springsource.com/maven/bundles/milestone</url>
        </repository>
        <repository>
            <id>SpringSource Enterprise Bundle Repository – SpringSource Bundle Releases</id>
            <url>http://repository.springsource.com/maven/bundles/release</url>
        </repository>
        <repository>
            <id>SpringSource Enterprise Bundle Repository – External Bundle Releases</id>
            <url>http://repository.springsource.com/maven/bundles/external</url>
        </repository>

        <repository>
            <id>Maven</id>
            <url>http://repo1.maven.org/maven2/</url>
        </repository>

    </repositories>

    <groupid>il.co.megasoft</groupid>
    <artifactid>spring-seminar</artifactid>
    <packaging>war</packaging>
    <version>app</version>
    <name>spring-seminar</name>
    <url>http://maven.apache.org</url>
    <properties>
        <maven.compiler.source>1.5</maven.compiler.source>
        <maven.compiler.target>1.5</maven.compiler.target>
        <webappdirectory>${project.build.directory}/${project.build.finalName}</webappdirectory>
        <project.build.sourceencoding>UTF-8</project.build.sourceencoding>
        <hibernate.version>3.6.0.Final</hibernate.version>
        <org.springframework.version>3.0.5.RELEASE</org.springframework.version>
    </properties>
    <dependencies>
        <dependency>
            <groupid>junit</groupid>
            <artifactid>junit</artifactid>
            <version>4.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupid>org.apache.openejb</groupid>
            <artifactid>commons-dbcp-all</artifactid>
            <version>1.3</version>
        </dependency>

        <dependency>
            <groupid>taglibs</groupid>
            <artifactid>standard</artifactid>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupid>javax.servlet</groupid>
            <artifactid>jstl</artifactid>
            <version>1.1.2</version>
        </dependency>

        <dependency>
            <groupid>net.sf.ehcache</groupid>
            <artifactid>ehcache</artifactid>
            <version>[2.0.0,]</version>
            <type>pom</type>
        </dependency>

        <dependency>
            <groupid>org.hibernate</groupid>
            <artifactid>hibernate-c3p0</artifactid>
            <version>3.3.1.GA</version>
        </dependency>

        <dependency>
            <groupid>com.google.gwt</groupid>
            <artifactid>gwt-servlet</artifactid>
            <version>2.2.0</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupid>com.google.gwt</groupid>
            <artifactid>gwt-user</artifactid>
            <version>2.2.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupid>commons-lang</groupid>
            <artifactid>commons-lang</artifactid>
            <version>2.6</version>
        </dependency>

        <dependency>
            <groupid>org.hibernate</groupid>
            <artifactid>hibernate-core</artifactid>
            <version>${hibernate.version}</version>
        </dependency>

        <dependency>
            <groupid>org.hibernate</groupid>
            <artifactid>hibernate-entitymanager</artifactid>
            <version>${hibernate.version}</version>
        </dependency>

        <dependency>
            <groupid>org.hibernate</groupid>
            <artifactid>hibernate-validator</artifactid>
            <version>4.1.0.Final</version>
        </dependency>

        <dependency>
            <groupid>mysql</groupid>
            <artifactid>mysql-connector-java</artifactid>
            <version>5.1.16</version>
        </dependency>

        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-context</artifactid>
            <version>3.0.0.RELEASE</version>
            <scope>runtime</scope>
            <exclusions>
                <exclusion>
                    <groupid>commons-logging</groupid>
                    <artifactid>commons-logging</artifactid>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupid>org.slf4j</groupid>
            <artifactid>jcl-over-slf4j</artifactid>
            <version>1.5.8</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupid>org.slf4j</groupid>
            <artifactid>slf4j-api</artifactid>
            <version>1.5.8</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupid>org.slf4j</groupid>
            <artifactid>slf4j-log4j12</artifactid>
            <version>1.5.8</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupid>log4j</groupid>
            <artifactid>log4j</artifactid>
            <version>1.2.14</version>
        </dependency>

        <!-- Hibernate uses slf4j for logging, for our purposes here use the simple
            backend -->

        <dependency>
            <groupid>org.slf4j</groupid>
            <artifactid>slf4j-simple</artifactid>
            <version>1.5.3</version>
        </dependency>

        <!-- Hibernate gives you a choice of bytecode providers between cglib and
            javassist -->
        <dependency>
            <groupid>javassist</groupid>
            <artifactid>javassist</artifactid>
            <version>3.8.0.GA</version>
        </dependency>

        <dependency>
            <groupid>javax.transaction</groupid>
            <artifactid>jta</artifactid>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupid>javax.transaction</groupid>
            <artifactid>transaction-api</artifactid>
            <version>1.1</version>
        </dependency>

        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-core</artifactid>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-beans</artifactid>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-aop</artifactid>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-context</artifactid>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>org.springframework.oxm</artifactid>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>org.springframework.web.servlet</artifactid>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-context-support</artifactid>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupid>com.google.collections</groupid>
            <artifactid>google-collections</artifactid>
            <version>1.0-rc4</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-tx</artifactid>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-jdbc</artifactid>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-orm</artifactid>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-web</artifactid>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-webmvc</artifactid>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-test</artifactid>
            <version>${org.springframework.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupid>org.springframework.security</groupid>
            <artifactid>spring-security-parent</artifactid>
            <version>${org.springframework.version}</version>
            <type>pom</type>
        </dependency>

        <dependency>
            <groupid>org.springframework.security</groupid>
            <artifactid>spring-security-config</artifactid>
            <version>${org.springframework.version}</version>
        </dependency>

        <dependency>
            <groupid>org.springframework.security</groupid>
            <artifactid>org.springframework.security.web</artifactid>
            <version>3.0.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupid>commons-fileupload</groupid>
            <artifactid>commons-fileupload</artifactid>
            <version>1.2.2</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupid>commons-io</groupid>
            <artifactid>commons-io</artifactid>
            <version>1.4</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupid>org.apache.poi</groupid>
            <artifactid>poi</artifactid>
            <version>3.8-beta3</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupid>org.apache.poi</groupid>
            <artifactid>poi</artifactid>
            <version>3.8-beta4</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupid>org.apache.maven.plugins</groupid>
                <artifactid>maven-war-plugin</artifactid>
                <version>2.1.1</version>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>exploded</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <webappdirectory>${webappDirectory}</webappdirectory>
                    <warsourcedirectory>WebContent</warsourcedirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>