Database does not have primary keys enabled - generating insertByPK statements

37 views
Skip to first unread message

Steve Hill

unread,
May 25, 2012, 11:23:25 AM5/25/12
to mybatis-user
Hi!

We are new to myBatis (migrating from Ibatis + heavily customized
MiddleGen)

We are attempting to build objects/xml using myBatis Generator which
is working except for tables which do not have primary key columns
configured in the database. We are wanting to use the
insertByPrimaryKey, selectByPrimaryKey methods etc that are built for
the other tables.

Is there something that can be done in the configuration file for
myBatisGenerator to allow us to tell the generator which columns to
treat as Primary Key columns for a given table?

Here is what we tried but it did not work.

<table tableName="ContractPackage" modelType="hierarchical" >
<property name="useActualColumnNames" value="true"/>
<generatedKey column="ClientID,ContractID,PackageID"
sqlStatement="SqlServer" identity="true" />
</table>

Thanks for your help!
Steve.

Jeff Butler

unread,
May 25, 2012, 2:03:36 PM5/25/12
to mybati...@googlegroups.com
This is a fairly common request that is easily resolved with a plugin.
I just added one to SVN here:

http://code.google.com/p/mybatis/source/detail?r=5288

This will be in the next release of the generator, but you can compile
this and use it now.

Jeff Butler

Steve Hill

unread,
May 25, 2012, 4:08:48 PM5/25/12
to mybati...@googlegroups.com
Thank you Jeff. Your help was very much appreciated. It took a little
while to getting working correctly with the Spring configuration, but I
have it now.

In case others are attempting this, we determined for some unknown
reason it did not work when added to our core library module and
referenced that in the plug in. We needed to create a new module with
only the plugin in it.

In the plugin for the generator it now looks as follows.

<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.1</version>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.tmghealth.app</groupId>
<artifactId>atlas-mybatis-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>

In the pom for altas-mybatis-plugin we added the following dependency.

<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.1</version>
</dependency>

Thanks again and enjoy the long holiday weekend.

Steve.
Reply all
Reply to author
Forward
0 new messages