Tutorial duration estimation: 15 minutes.
In this tutorial you are going to learn how to generate your first Java/Spring/Hibernate based project using Springfuse. All in all, you should not type more than 3 commands from your console to have your first project running.
Anybody can follow this tutorial and discover Springfuse. All you need is Java and Maven2, you do not need to have a database to follow the tutorial. We provide an example database.
Springfuse reverses your database schema and uses the result of the extraction as an entry point to generate your Spring-based Java Project.
Obviously, Springfuse service cannot access your database. Instead Springfuse relies on you to:
You might be wondering what kind of information is dumped into this file. Is the data dumped ?
The clear answer is no. We dump only its structure such as the table names, the column names,
the indexes, the primary key, etc.
To keep this tutorial simple, we are not going to ask you to use your database, instead we will use an embedded database.
You can download it for free from http://java.sun.com.
You can download it for free from Apache's Maven site.
If you are behind an http proxy, you must tell maven how to go through your proxy.
For those who are new to Maven, simply make sure that your Maven installation is clean. For example, type from a console
c:\tutorial>mvn -version
You should have an output similar to this:
c:\tutorial> mvn -version Apache Maven 2.1.0 (r755702; 2009-03-18 20:10:27+0100) Java version: 1.5.0_15 OS name: "windows vista" version: "6.0" arch: "x86" Family: "windows"
c:\tutorial\springfuse-example>dir 20/01/2009 15:27 5 743 pom.xml 21/01/2009 21:54 3 815 README.txt 27/01/2009 15:41src
Notice, the src folder, it contains the H2 database SQL scripts to create the embedded database that we are going to reverse. You can take a look at the scripts, even modify them. However, we recommend to keep them as is if this is your first attempt.
The graph below represents the database schema that we are going to create.
There are 3 important tables in this schema, not required though. These tables are ACCOUNT, ROLE and ACCOUNT_ROLE. By convention when Springfuse encounters such tables it assumes they should be used for authentication/authorization and thus generates some glue code for Spring Security. The ACCOUNT table has other implications in relation with Hibernate filters that are beyond the scope of this tutorial.
The fourth table, DOCUMENT, is here to illustrate one of Springfuse's great feature, the file upload/download support in Spring MVC. Since this table's columns follow some conventions in their naming and type, Springfuse assumes that it should generate some additional source code so you can upload and download some files!
Type 'mvn initialize' to create and reverse the database schema shown above.
c:\tutorial\springfuse-example>mvn initialize
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building springfuse-example
[INFO] task-segment: [initialize]
[INFO] ------------------------------------------------------------------------
[INFO] [sql:execute {execution: Create and initialize the database}]
[INFO] Executing file: c:\tutorial\springfuse-example\src\main\sql\h2\drop.sql
[INFO] Executing file: c:\tutorial\springfuse-example\src\main\sql\h2\create.sql
[INFO] Executing file: c:\tutorial\springfuse-example\src\main\sql\h2\comment.sql
[INFO] Executing file: c:\tutorial\springfuse-example\src\main\sql\h2\init.sql
[INFO] 45 of 45 SQL statements executed successfully
[INFO] [springfuse:extract {execution: Extract database Meta Data}]
[INFO] Ready to extract the database schema
[INFO] Connecting to database jdbcUrl=jdbc:h2:~/.h2/quickstartdb
[INFO] Connected OK
[INFO] database Product Name: H2
[INFO] database schema extracted
[INFO] File data-model.springfuse passed reverse conversion OK
[INFO] File data-model.springfuse created successfully
[INFO] You are now ready to upload data-model.springfuse to http://www.springfuse.com/
and generate your project!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Tue Jan 27 16:10:07 CET 2009
[INFO] Final Memory: 4M/8M
[INFO] ------------------------------------------------------------------------
This simple command has done 2 things:
Congratulations! you just did the hardest part, the next step should be a piece of cake for you.
Here is a tip: give your project the same name as your database, that is quickstartdb, this will allow you to run your project out-of-the-box without any additional JDBC configuration!
Choose any Java package, for example: com.yourock
Select the file data-model.springfuse
Click on Generate, in less than 5 seconds, your project is ready for download!
Download your project, unzip it, open a console and go to the root folder of the unzipped project. Here are the files it contains:
c:\tutorial\quickstartdb\quickstartdb>dir 02/02/2009 18:07 633 .classpath 02/02/2009 18:07 630 .project 02/02/2009 21:16 REP .settings 02/02/2009 18:07 11 479 license.txt 02/02/2009 18:07 892 notice.txt 02/02/2009 18:07 41 469 pom.xml 02/02/2009 18:07 1 222 README.txt 02/02/2009 21:16 REP src
From this folder, run the following command:
c:\tutorial\quickstartdb>mvn -Ph2 jetty:run
[...
Skip Maven2 output. Be patient, it may take some time to download all the jar dependencies such as
spring, spring-mvc, spring-security, hibernate, etc.
...]
2009-01-27 16:50:03.043::INFO: Started SelectChannelConnector@0.0.0.0:8080
[INFO] Started Jetty Server
The '-Ph2' argument tells Maven to use the 'h2' database profile defined in the pom.xml.
If you followed all the previous steps, your project is now up and running !
Open http://localhost:8080/ and sign in using either user/user or admin/admin. As an admin, you have more privileges, try to create a new Document entry, this involves the upload of a file. Then you can try to download it...
You can now import the project into Eclipse as a Maven project.
You will need to install the Maven2 plugin for Eclipse.
The screenshot below represents the package dependencies of the generated code, as you can see, there is no cyclic dependencies and the layers are well separated.
The graph is generated by the ispace plugin, it is not part of the generated project, however you can install it easily.
Thank you for following this tutorial.