How do I use batGen?
It is highly recommended that you start out with an
understanding of how to use MyBatis. This tool will not
substitute the need for this knowledge.
For this tutorial, we will be using
Eclipse Luna, which already have Maven
installed.
- Download
and extract files. Then import batGen into Eclipse as existing Maven
Projects.
- Create a new Maven project: select simple project,
fill in required informations:
- Group Id: batGen.Tutorial
- Artifact Id: batGenTutorial
Click finish.
- Insert following dependencies into the pom.xml of
the new project:
<dependencies>
<dependency>
<groupId>batgen.lib</groupId>
<artifactId>batgen</artifactId>
<version>1.0.0-RC1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.171</version>
</dependency>
</dependencies>
- Create the sample Employee.txt under /src/test/resources:
/**
* The Employee Table.
*/
[Settings]
CLASS Employee
[Fields]
DOUBLE(10,2) salary // Salary of employee
STRING(132) lastName* // Last Name
STRING(132) firstName // First Name
LONG(10) supervisorKey SUPERVISOR_KEY?
[Indexes]
EMPLOYEE_NAME lastName, firstName
[ForeignKeys]
supervisorKey constrainsTo Supervisor.supervisorKey
Note the use of special characters in the file:
- /** documentation comment
- * the field is required
- - SequenceDisabled
- ! the field is a primary key
- ? the field is a search Id
- // allow comments to be inserted after
- Create the sample Supervisor.txt under /src/test/resources:
/**
* Activate supervisors.
*/
[Settings]
CLASS Supervisor SUPVSR
[Fields]
LONG(10) supervisorKey SUPERVISOR_KEY!-
INTEGER(4) count CNT
DOUBLE(10,2) salary SLRY
STRING(132) lastName LST_NME*
STRING(132) firstName FST_NME*
DATE hireDate HIRE_DATE
BOOLEAN active ACTIVE
BLOB profile PROFILE
[Indexes]
SUPERVISOR_NAME lastName, firstName
- Within src/test/java, create a new CodeGen class called start with the following code and
import the required 'package org.batgen.CodeGen' and 'org.batgen.DatabaseType'.
- Takes 3 parameters: resource location, package name, and
default database environment ( H2 or ORACLE )
package codegen;
import org.batgen.BatGen;
import org.batgen.DatabaseType;
public class CodeGen {
public static void main( String[]args ) {
BatGen batGen = new BatGen( "src/test/resources", "test.app.sample", DatabaseType.H2 );
batGen.run();
}
}
- Select Run As Java Application, and press Enter
when prompted.
- Refresh project and new files should be created.
- Database must be setup before these files can be used.
Use either H2 or
Oracle XE