Other ApplicationsStruts Resume
Set your name in
About this tutorialThis tutorial will show you how to create a new table in the database, and how the create Java code to access this table.
You will create an object and then some more classes to persist (save/retrieve/delete) that object from the database. In Java speak, this object is called a Plain Old Java Object (a.k.a. a POJO). This object basically represents a database table. The other classes will be:
AppFuse uses Hibernate for its default persistence layer. However, you can install iBATIS and use it as an alternative. You can also use them both side-by-side if you so desire. To install iBATIS in AppFuse, view the README.txt in extras/ibatis (or simply cd into this directory and run ant install). The Hibernate version of this tutorial is available here.
Font Conventions (work in progress)
Let's get started on creating a new Object, DAO and Test in AppFuse's project structure.
Table of Contents
Create a new POJO, table and SQLMap [#1]
The first thing you need to do is create an object to persist. Let's create a simple "Person" object (in the src/dao/**/model directory) that has an id, a firstName and a lastName (as properties).NOTE: Copying the Java code in these tutorials doesn't work in Firefox. A workaround is to CTRL+Click (Command+Click on OS X) the code block and then copy it.
This class should extend BaseObject, which has 3 abstract methods: (equals(), hashCode() and toString()) that you will need to implement in the Person class. The first two are required by Hibernate. The easiest way to do this is using Commonclipse. More information on using this tool can be found on Lee Grey's site. Another Eclipse Plugin you can use is Commons4E. I haven't used it, so I can't comment on its functionality.
Create a new database table and mapping file [#2]Now that you have this POJO created, you need to create a "person" database table that your POJO will map to. Add the following to metadata/sql/mysql-create-tables.sql:
CREATE TABLE person ( person_id int(8) auto_increment, first_name varchar(50) NOT NULL, last_name varchar(50) NOT NULL, PRIMARY KEY (person_id) );
Then run "ant setup-db". This will create the person table for you. To create the SQL-to-Java mapping file, create PersonSQL.xml in src/dao/**/ibatis/sql. It should contain the following XML:
Now you'll need to notify iBATIS that a new SQLMap exists. Open src/dao/**/ibatis/sql-map-config.xml and add the following:
Create a new DaoTest to run JUnit tests on your DAO [#3]NOTE: AppFuse versions 1.6.1+ contain include an AppGen tool that can be used to generate all the classes for the rest of these tutorials. However, it's best that you go through these tutorials before using this tool - then you'll know what code it's generating. ;-)
Now you'll create a DaoTest to test that your DAO works. "Wait a minute," you say, "I haven't created a DAO!" You are correct. However, I've found that Test-Driven Development breeds higher quality software. For years, I thought write your test before your class was hogwash. It just seemed stupid. Then I tried it and I found that it works great. The only reason I do all this test-driven stuff now is because I've found it rapidly speeds up the process of software development.
To start, create a PersonDaoTest.java class in the test/dao/**/dao directory. This class should extend BaseDaoTestCase, a subclass of Spring's AbstractTransactionalDataSourceSpringContextTests which already exists in this package. This parent class is used to load Spring's ApplicationContext (since Spring binds the layers together), and for (optionally) loading a .properties file (ResourceBundle) that has the same name as your *Test.class. In this example, if you put a PersonDaoTest.properties file in the same directory as PersonDaoTest.java, this file's properties will be available via an "rb" variable.
The code you see above is what you need for a basic Spring integration test that initializes and configures an implementation of PersonDao. Spring will use autowiring byType to call the setPersonDao() method and set the "personDao" bean as a dependency of this class.
Now you need test that the CRUD (create, retrieve, update, delete) methods work in your DAO. To do this, create methods that begin with "test" (all lower case). As long as these methods are public, have a void return type and take no arguments, they will be called by the <junit> task in build.xml. Below are some simple tests for testing CRUD. An important thing to remember is that each method (also known as a test), should be autonomous. Add the following methods to your PersonDaoTest.java file:
<table name='person'> <column>id</column> <column>first_name</column> <column>last_name</column> <row> <value>1</value> <value>Matt</value> <value>Raible</value> </row> </table>
In the above example, you can see that person.set*(value) is being called to populate the Person object before saving it. This is easy in this example, but it could get quite cumbersome if you're persisting an object with 10 required fields (not-null="true"). This is why I created the ResourceBundle in the BaseDaoTestCase. Simply create a PersonDaoTest.properties file in the same directory as PersonDaoTest.java and define your property values inside it:
firstName=Matt lastName=RaibleThen, rather than calling person.set* to populate your objects, you can use the BaseDaoTestCase.populate(java.lang.Object) method:
At this point, the PersonDaoTest class won't compile yet because there is no PersonDao.class in your classpath, you need to create it. PersonDao.java is an interface, and PersonDaoiBatis.java is the iBATIS implementation of that interface.
Create a new DAO to perform CRUD on the object [#4]First off, create a PersonDao.java interface in the src/dao/**/dao directory and specify the basic CRUD methods for any implementation classes.
Notice in the class above there are no exceptions on the method signatures. This is due to the power of Spring and how it wraps Exceptions with RuntimeExceptions. At this point, you should be able to compile all the source in src/dao and test/dao using ant compile-dao. However, if you try to run ant test-dao -Dtestcase=PersonDao, you will get an error: No bean named 'personDao' is defined. This is an error message from Spring - indicating that you need to specify a bean named personDao in applicationContext-ibatis.xml. Before you do that, you need to create the PersonDao implementation class.
Let's start by creating a PersonDaoiBatis class that implements the methods in PersonDao and uses iBATIS to get/save/delete the Person object. To do this, create a new class in src/dao/**/dao/ibatis and name it PersonDaoiBatis.java. It should extend BaseDaoiBatis and implement PersonDao.
Now, if you try to run ant test-dao -Dtestcase=PersonDao, you will get the same error. Yo need to configure Spring so it knows that PersonDaoiBatis is the implementation of PersonDao.
Create a bean definition for the PersonDao [#5]
In order to bind PersonDaoiBatis to the PersonDao interface, you need to register a "personDao" bean definition in src/dao/**/dao/ibatis/applicationContext-ibatis.xml. Open this file and add the following at the bottom:
Run the DaoTest [#6]Save all your edited files and try running ant test-dao -Dtestcase=PersonDao one more time.
Yeah Baby, Yeah:
Next Up: Part II: Creating new Managers - A HowTo for creating Business Facades, which are similar to Session Facades, but don't use EJBs. These facades are used to provide communication from the front-end to the DAO layer.