1.0          Other languages
 
 

4.2 Generate database model and base DAO class using Middlegen

 
 

There is a sample Ant build file in VPetStore\middlegen, named as build_dao.xml. You should modify some configuration parameters before you use it for your own application.

db.model.package=org.vpetstore.dbmodel
db.dao.package=org.vpetstore.dao.generated
build.gen-src.dir=../src
build.gen-src.db.model.dir=../src/org/vpetstore/dbmodel
build.gen-src.db.dao.dir=../src/org/vpetstore/dao/generated

The template file is tbl_dao.vm and tbl_model.vm, they are plain text files writen following Apache Velocity syntax. Of cause, you can modify these template file for your own appliction.

For each table, it can generate one Java DAO class and one Java model class. In the generated DAO class, there are functions named as insert(), getAll(), deleteByKey(), getByKey(), update(). The functions deleteByKey(), getByKey() can only be used in table with single primary key, they will not appear in DAO class for combined primary key table. Others can be used in both single primary key and combined primary key.

Generated code files should be put in folders different to manual code files. It's a better idea to make sure that only one programmer can modify files been generated. That only him can run the Ant script to generate database relative DAO and model code files. It's also a good idea to make it a part of DBA. And Make sure each time DBA change the database during developing progress, he should also update the generated files.