-
Notifications
You must be signed in to change notification settings - Fork 16
SQLMOP DAO generation
The next control directives have been changed: pojo
to is-pojo
, table
to is-table
, procedure
to is-procedure
, function
to is-function
.
The advanced feature of the SQLMOP is the ability to generate the DAO API based on DB layout. It can significantly help to improve coding efficiency. The generated code can be taken as the first step in a new project, mainly for CRUD statements. Later the generated classes can be manually overwritten and improved.
The usage is rather simple. Let's have the following control directives in the definitions.model
to establish connection to the target database (this tutorial is based on the sample project https://github.com/hudec/sql-processor/blob/master/sql-samples/simple-jdbc-crud/src/main/resources/definitions.model):
database-is-online;
database-jdbc-driver org.hsqldb.jdbcDriver;
database-has-url jdbc:hsqldb:mem:sqlproc;
database-login-username sa;
database-login-password "";
database-ddl-create hsqldb.ddl; // should be located in the same directory as definitions.meta
pojogen-generate-wrappers; // DB types are not converted to the Java native types
compress-meta-directives;
pojogen-package org.sqlproc.sample.simple.model;
daogen-package org.sqlproc.sample.simple.dao;
The next steps are described in SQLMOP POJO generation.
As a final step the DAO layer can be generated. There are several control directives daogen-...
, which are devoted to this process. The initial content of the dao.model
can be for example
package org.sqlproc.sample.simple.dao {
}
Put the cursor inside the curly brackets and press a Ctrl-Space. A content assist is activated and in the popup menu a couple of templates is offered. Select the new advanced template daogen - DAO generator. A block of model code is generated based on the DB layout. For every database table (and so for every POJO) one DAO class is created. For every basic META SQL statement (INSERT, GET, UPDATE, DELETE, SELECT) a couple of methods are created.
The sample of the generated DAO API can be seen at the https://github.com/hudec/sql-processor/blob/master/sql-samples/simple-jdbc-crud/src/main/resources/dao.model.
The process of the DAO layer generation is controlled by the next control directives
- pojogen-*
- daogen-*
We can create DAO only for selected tables in the target database. The selection can be a positive one - only the tables BOOK
and PERSON
are processed
daogen-only-tables BOOK PERSON;
The selection can be a negative one - the tables LIBRARY
and MOVIE
are not processed
daogen-ignore-tables LIBRARY MOVIE;
All the generated DAO classes can be forced to implement required interfaces as is described in SQLMOP DAO modelling. For example to force the SQLMOP to generate proper DAO utilizing interfaces org.sqlproc.sample.simple.dao.BaseDao
and java.io.Serializable
, use in definitions.model
daogen-implements-interfaces org.sqlproc.sample.simple.dao.BaseDao java.io.Serializable;
All the generated Java classes can be forced to extend one Java class as is described in SQLMOP DAO modelling. For example to force the SQLMOP to generate proper DAO utilizing Java class org.sqlproc.sample.simple.dao.impl.BaseDaoImpl
, use in definitions.model
daogen-extends-class org.sqlproc.sample.simple.dao.impl.BaseDaoImpl;
The correct approach to DAO layer is to have the implementation separated from the interface. For example for the following snippet
#Implementation(impl)
package org.sqlproc.sample.simple.dao {
...
final dao LibraryDao :: Library {
scaffold
}
...
}
the DAO interface LibraryDao.java
is automatically generated in the directory src-gen/org/sqlproc/sample/simple/dao
and the DAO implementation LibraryDaoImpl.java
is automatically generated in the directory src-gen/org/sqlproc/sample/simple/dao/impl
. The implementation subdirectory is controlled by the control directive implementation-package impl
. To force the generator to create this control directive, use in definitions.model
daogen-implementation-package impl;
The DAO can be assigned using the final
in the DAO declaration (described in SQLMOP DAO modelling). This is an indicator for the DAO generator to not overwrite this class in the next daogen
template usage. To force SQLMOP to mark all generated DAOs as final, we can use
daogen-make-it-final;
- SQL Processor Home
- SQL Processor News
- SQL Processor Eclipse Plugin Home
- SQL Processor Eclipse Plugin News
- SQL Processor Architecture
- SQLP and SQLM*P Tutorials
- SQLM*P Control directives
- Basic Tutorials
- 10 minutes Tutorial
- Simple Tutorial
- CRUD Tutorial
- Associations Tutorial
- Inheritance Tutorial
- More inheritance Tutorial
- Custom Types Tutorial
- Stored procedures Tutorial
- IDE Setup and Coding Standards
- Catalog (JPA) Sample
- Catalog (Hibernate) Sample
- Catalog (Spring) Sample
- Advanced catalog (Spring) Sample
- The Reference Guide
- The Eclipse Plugin Tutorials
- The Improvements
- Tutorials archive