I’m playing with technologies and framework for a new project: because I’m learning things probably useful for others, I’m gonna write here few notes about what I’ve learned. This is the first article of a series.

Part 1 – Building the model library

In this article, I will set up the library for the MyBatis entities,
that will be used to access the database.

Technologies used:

  • PostgreSQL 9.1
  • Java 8
  • MyBatis 3.3
  • MyBatis Generator (MBG) 1.2
  • Maven 3

For the moment, Spring, Mybatis-Spring and Infinispan are not present: my project doesn’t need them in this library, they will be introduced at a later moment.

Step 1 – Setting up the project

I’ve creted a simple Maven project with the following structure:

2015-10-08-115927 Initially, src/main/java and src/main/resources have no subdirectories: the will be generated by MBG.

The important parts are: pom.xml, generatiorConfig.xml. Let’s have a look at them.

This is the important part of pom.xml:

 <properties>
 <mybatis.version>3.3.0</mybatis.version>
 <postgresql.group>org.postgresql</postgresql.group>
 <postgresql.artifact>postgresql</postgresql.artifact>
 <postgresql.version>9.4-1201-jdbc41</postgresql.version>
 <mybatis.version>3.3.0</mybatis.version>
 <mybatisGenerator.version>1.3.2</mybatisGenerator.version>
 <commons.logging.version>1.1.1</commons.logging.version>
 
 <!-- MBG: mybatis generator -->

 <mybatis.generator.overwrite>true</mybatis.generator.overwrite>
 <project.source.dir>src/main/java</project.source.dir>
 <project.resources.dir>src/main/resources</project.resources.dir>

 <generated.model.package>it.caladyon.db.model.generated</generated.model.package>
 <generated.mapper.package>it.caladyon.db.mapper.generated</generated.mapper.package>
 <generated.mapper.dir>it/caladyon/db/mapper/generated</generated.mapper.dir>

 <dev.db.driver>org.postgresql.Driver</dev.db.driver>
 <dev.db.url>jdbc:postgresql://***</dev.db.url>
 <dev.db.username>***</dev.db.username>
 <dev.db.password>***</dev.db.password>
 </properties>

 <build>

 <plugins>
 <plugin>
 <groupId>org.mybatis.generator</groupId>
 <artifactId>mybatis-generator-maven-plugin</artifactId>
 <version>1.3.0</version>
 <dependencies>
 <dependency>
 <groupId>org.mybatis.generator</groupId>
 <artifactId>mybatis-generator-core</artifactId>
 <version>${mybatisGenerator.version}</version>
 </dependency>
 <dependency>
 <groupId>${postgresql.group}</groupId>
 <artifactId>${postgresql.artifact}</artifactId>
 <version>${postgresql.version}</version>
 </dependency>
 </dependencies>
 </plugin>
 </plugins>

 </build>

 <dependencies>

 <dependency>
 <groupId>org.mybatis</groupId>
 <artifactId>mybatis</artifactId>
 <version>${mybatis.version}</version>
 </dependency>

 <dependency>
 <groupId>${postgresql.group}</groupId>
 <artifactId>${postgresql.artifact}</artifactId>
 <version>${postgresql.version}</version>
 <scope>provided</scope>
 </dependency>

 <dependency>
 <groupId>commons-logging</groupId>
 <artifactId>commons-logging</artifactId>
 <version>${commons.logging.version}</version>
 <scope>test</scope>
 </dependency>
 
 </dependencies>

The configuration for launching MBG lies in the properties. You may have a look at the MBG documentation for the complete list of properties.

It’s worth noticing that the database entities (either generated by MBG or made by hand) have a separated package from the source files I will have to write, and that model and mapper entities are separated too: it will be useful for the introduction of mybatis-spring.

The parent pom defines the Java version to be used:

 <plugin>
 <artifactId>maven-compiler-plugin</artifactId>
 <version>2.3.2</version>
 <configuration>
 <source>1.8</source>
 <target>1.8</target>
 </configuration>
 </plugin>

This is the generatorConfig.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>

 <context id="development">
 <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
 <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>
 <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>
 <plugin type="org.mybatis.generator.plugins.VirtualPrimaryKeyPlugin"></plugin>

 <jdbcConnection driverClass="${dev.db.driver}" connectionURL="${dev.db.url}" userId="${dev.db.username}" password="${dev.db.password}" />

 <javaModelGenerator targetPackage="${generated.model.package}" targetProject="${project.source.dir}">
 <property name="enableSubPackages" value="true" />
 <property name="trimStrings" value="true" />
 </javaModelGenerator>

 <sqlMapGenerator targetPackage="${generated.mapper.package}" targetProject="${project.resources.dir}">
 <property name="enableSubPackages" value="true" />
 </sqlMapGenerator>

 <javaClientGenerator targetPackage="${generated.mapper.package}" targetProject="${project.source.dir}" type="XMLMAPPER">
 <property name="enableSubPackages" value="true" />
 </javaClientGenerator>



<table schema="bita" tableName="vbitac_seg" enableSelectByPrimaryKey="true" enableSelectByExample="true" enableCountByExample="true" enableInsert="false" enableUpdateByPrimaryKey="false" enableUpdateByExample="false" enableDeleteByPrimaryKey="false" enableDeleteByExample="false" >
 <property name="immutable" value="true" />
 <property name="virtualKeyColumns" value="c_seg" />
 </table>



 </context>
</generatorConfiguration>

As you can see, I’m modeling a view, that is immutable and with no modification methods. The VirtualPrimaryKeyPlugin is used to specify the actual primary key (the view is a 1:1 map of a real table of another schema); the documentation says that the column names for the virtualKeyColumns property are all upper case, but this is not the case with Postgres.

The actual database structure is not important for the current article; it is enough to say that the “c_seg” column is of type bigint (so MBG will generate no “key class” for this table).

About the MBG plugins: I’ve used the ToStringPlugin (with prints the object’s hashcode) but not the EqualsHashCodePlugin: this way the toString() method will use the hash generate by the Object.hashCode() implementation, highiighting if two object are different instances.

Step 2 – Building the MyBatis entities

As stated in the MBG documentation, the generation of model and mappers can be activated manually by issuing the following command to the command line:

mvn mybatis-generator:generate

After a refresh (F5) of the project, there will be the following files:

  • src/main/java/it/caladyon/db/model/generated/bita/VbitacSeg.java: the model class of the table (no key class generated, as described before), just a simple DTO bean without setter methods, because it describes an immutable entity
  • src/main/java/it/caladyon/db/model/generated/bita/VbitacSegExample.java: a facility to express the where clause of queries
  • src/main/java/it/caladyon/db/mapper/generated/bita/VbitacSegMapper.java: an interface with the allowed operations
  • src/main/recources/it/caladyon/db/mapper/generated/bita/VbitacSegMapper.xml: this file contains the SQL enhanced by the MyBatis XML.

That’s all for now, See you soon

Annunci