目录
用法介绍
使用mybatis plugin,配置如下:
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<phase>install</phase>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
</configuration>
</plugin>
查看官方对于生成主键的配置的解释
http://www.mybatis.org/generator/configreference/generatedKey.html
generatorConfig.xml 配置
<table schema="hotel_pda" tableName="vg_upload_file">
<property name="useActualColumnNames" value="false"/>
<generatedKey column="id" sqlStatement="MySql" identity="true" />
</table>
附:我的generatorConfig全部配置
<?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>
<classPathEntry location="/Users/lianghe/.m2/repository/mysql/mysql-connector-java/5.1.36/mysql-connector-java-5.1.36.jar"/>
<context id="mysql" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<plugin type="org.mybatis.generator.plugins.CaseInsensitiveLikePlugin" />
<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"/>
<plugin type="org.mybatis.generator.plugins.CaseInsensitiveLikePlugin"/>
<!-- 注释 -->
<commentGenerator >
<property name="suppressAllComments" value="true"/><!-- 是否取消注释 -->
<property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳-->
</commentGenerator>
<!-- jdbc -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://xx.xx.xx.xx/hotel_pda"
userId="xxx"
password="xxx" />
<!-- 生成model -->
<javaModelGenerator targetPackage="com.xxx.domain"
targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="false"/>
</javaModelGenerator>
<!-- 生成sql -->
<sqlMapGenerator targetPackage="mapper/auto"
targetProject="src/main/resources" >
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 生成sql client-->
<javaClientGenerator targetPackage="com.xxx.dao.auto"
targetProject="src/main/java"
type="XMLMAPPER" >
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- Tables -->
<table schema="hotel_pda" tableName="vg_upload_file">
<property name="useActualColumnNames" value="false"/>
<generatedKey column="id" sqlStatement="MySql" identity="true" />
</table>
</context>
</generatorConfiguration>
附:mybatis返回主键的用法
public int insert(Domain domain) {
//插入完成后 domain的主键id字段会被赋值,直接返回即可
domainMapper.insertSelective(domain);
return domain.getId();
}