Documentation

What is BatGen?

BatGen is a simple yet powerful code generator that will generate the SQL, MyBatis mapper files, MyBatis interface files, supporting Data Access Object (DAO) and Business Object (BO) objects with the basic CRUD operations.

Features

  • The code generator generates clean, readable and maintainable code that you check in with the rest of the code. There is a reserved area in each generated file that you can write custom code into. This reserved area is preserved during code generation, so if you change something, you can still maintain the code you wrote in it.
  • Table definitions are stored in text files, and they describe the tables, columns, related classes, related class variables, and type information used to generate the code.

How does it work?

To keep it simple, we rely heavily on creating conventions for how projects are setup and how database column types are associated with corresponding class variable types.

For this reason, we recommend using this product on new projects, as existing projects will already have established standards.

This is a conscious choice we made and it dramatically simplifies the setup and configuration of new projects. It also causes all projects that use this product to have a standard directory structure for the project.

Formatted Tables

The tables used are formatted in an acceptable manner for the application to create the generated files. Your files should end in .txt , should match the below format, and should only contain one table. If your tables are not correctly formatted, an error will occur during the implementation of the code generation application. Each file is broken into three sections:
  1. The first section is Comments.
    1. Generally the comment section is used to define what the table name is called.
    2. Comments here can be defined by a multi-line comment (starting with /* and ending with */) or a single line comment (//).
  2. The second section is Settings.
    1. The keyword Settings must be surrounded by brackets ( [ ] ). Within the settings it contains the keyword class.
    2. CLASS specifies the name of the table (java) and the database name (sql) (The database name is not required).
  3. The third section is Fields.
    1. The keyword Fields must be surrounded by brackets ( [ ] ).
    2. The field section contains all the columns related to that table.
    3. The data type is on the left, followed by the name of the column (camelCase), the database alias name (not required), special character (not required), and a comment (not required)
      • The applicable data types are: long, integer, double, string, blob, clob, date, timestamp, boolean and vstring.
      • If the data type is an long, integer, or string, then you must also specify a magnitude (total number of digits).
        STRING(132)		lastName	LAST_NAME*
        
      • If the data type is an double, then you must also specify a interger part (number of digits to the left of the decimal point) and precision (number of digits to the right of the decimal point).
        DOUBLE(3,2)		hourWage	HOUR_WAGE*
        
      • If the data type is an timestamp then you must also specify a precision (precision of the seconds ).
        TIMESTAMP(0)		entryDat	ENTRY_DTTM*
        
      • If the data type is an vstring, then you must also specify a magnitude (total number of digits) and also a comment section with the sql-statement on how to retrieve it surrounded by `.
        VSTRING(4)	last4SSN	LAST_4_SSN*	// last four digits of SSN `cast(substr(SSN, 6, 4) as varchar2(4))`
        
    4. The special characters that are allowed are !, *, ? and -.
      • ! signifies that the column is a primary key to the table. There can only be one primary key and it can only be defined as a LONG(some value) or STRING(some value). If you do not create a key column, one will be automatically generated for you
      • The automatically generated key for creating the table.
        CREATE TABLE EMPLOYEE (
        KEY                NUMBER(10) NOT NULL,
        CONSTRAINT EMPLOYEE_PK PRIMARY KEY (KEY));
        
      • * signifies that the column is required, and cannot be null.
      • The required column for creating the table.
        LAST_NAME          VARCHAR2(132) NOT NULL,
        
      • - signifies that the column has it's sequence disabled.
      • If it's disabled, it would not produce the following code in the xml file.
        <selectKey resultType="_long" keyProperty="key" order="BEFORE">
        	select EMPLOYEE_SEQ.nextval from dual
        </selectKey>
        
      • ? signifies that the column is a searchable ID, used to get an list of all objects with same the ID.
      • If it's enabled, it would produce the following code in the xml file.
        <select id="getListBySupervisorKey" parameterType="long" resultMap="EmployeeMapper">
        	select * from EMPLOYEE
        	where SUPERVISOR_KEY = #{supervisorKey}
        </select>
        
  4. The fourth section is Compound Indexes (Optional).
    1. The keyword Indexes must be surrounded by brackets ( [ ] ).
    2. Use to create compound indexes within current table
    3. Requires at least two inputs: the index name, and all the columns names(second column in the txt file) separated by commas.
    Produces the following code for the sql file.
    	CREATE INDEX name ON EMPLOYEE( LAST_NAME, FIRST_NAME );
    
  5. The fifth section is Foreign Keys (Optional).
    1. The keyword ForeignKeys must be surrounded by brackets ( [ ] ).
    2. Use to create references to a column from another table
    3. Requires three inputs: the column name(second column in the txt file), the word 'constrainsTo', and the other table name.other column name
    Creates an AlterTable.sql file and addes the following code.
    	ALTER TABLE EMPLOYEE ADD CONSTRAINT FK_EMPLOYEE_1 FOREIGN KEY (SUPERVISOR_KEY) REFERENCES SUPVSR(SUPERVISOR_KEY);
    
The tutorial provides all the information you need to get started building applications using Eclipse and Maven.