|
|
Using MDAOGThis is a guide to help you get started with MDAOG.PrerequisitesEnvironmentYou'll need to have a PostgreSQL database to access, a Java Development Kit and a servlet engine. Apache Tomcat is a good open source choice.Designing your Database SchemaDesigning your data model for use with MDAOG shouldn't be much different than normal. If you are going to be using automatically generated integer keys from sequences the sequence name should be in the form tablename_columnname_seq or you can use the Serial data type which creates a sequence of that form behind the scenes. Also it is a good idea to keep the keys on the top of your table definition.Let's assume we're building a very simple application that manages books and their authors. We have an author table and a book table with a relationship between the two. The SQL to create those tables would look something like this: ![]() It is a good idea to include comments on your tables and attributes. MDAOG will use those comments in the JavaDoc comments of the generated files. They have been omitted here for brevity. Joining TablesThe quickest and easiest way to join 2 or more tables is to create a view in the database that represents the columns you would like returned and the criteria for joining those tables. WHen you run MDAOG you can select the views and MDAOG will create the DAO's and DTOs for accessing the view.Generating Code with MDAOGWith our environment and database tables created we can focus on generating the DAO code. You can also view a Flash based demo on running MDAOG.Obtaining MDAOGYou can download the MDAOG jar file or sources from here.Save the jar files somewhere on your system. Running MDAOGMDAOG is packaged as an executable jar file. To start MDAOG use the following command from the directory where you saved mdaog.jar:
MDAOG should then start. MDAOG is a Java Swing based application but it is set up to user your computer's System Look and Feel so it should closely resemble other applications on your system. Setting up MDAOG to GenerateBefore MDAOG can generate the source files it needs to have the following information entered into the following fields:
Saving Project SettingsIf you plan to make changes to your data model after you have generated the DAO files you should save your project settings. Go to the File menu and select Save and name the file. The file will be saved with a .properties extension and saved in the directory you choose.Generating SourcesNow that we've set up the info MDAOG needs and saved the settings, we can have MDAOG generate the source code.To do this select Generate DAO Sources from the Action menu. MDAOG will now attempt to connect to the database via the JDBC settings you supplied. If all the settings were correct, it will pop up a window with two list boxes. The top list displays all the tables found in the database, the bottom all the views. Select the tables and views you'd like to generate files for and click on the Generate button. MDAOG will now generate all the necessary Java source files for connecting to the database. Now is a good time to go through the generated code and get a feel for what it does. Using the Generated CodeNow that MDAOG has generated the DAO code to access the database from your web application we'll go through the steps of using that code in a web application.Web configurationThe code generated by MDAOG requires some settings in web.xml to function properly.
Using DAOs from Web ApplicationBelow is an example of a simple Bean that can be used by a JSP page to display all the records in the authors table sorted by pen_name.
Using the generated DAOs to insert an object can be performed as described in the following example:
import com.mycompany.mywebapp.data.dao.*; import com.mycompany.mywebapp.data.factory.*; import com.mycompany.mywebapp.data.dto.*; import com.mycompany.mywebapp.data.exception.*; /** * Inserts a new Author into the database */ public class AddAuthorPage { public void addAuthor(String firstName, String lastName, String middleInitial, String penName) { //get an instance of the DaoFactory DaoFactory daof = DaoFactory.getDaoFactory(); //get an instance of AuthorDao AuthorDao adao = daof.getAuthorDao(); //create a new Author DTO and PK Author author = new Author(); AuthorPK pk = null; //set author attributes author.setFirstName(firstName); author.setLastName(lastName); author.setMiddleInitial(middleInitial); author.setPenName(penName); try { //insert the new DTO and retrieve the PK object pk = adao.insert(author); } catch (AuthorDaoException e) { //handle exception } } } |