查閱文檔得知這主要由targetRuntime屬性控制。
targetRuntime屬性用於指定生成代碼的運行時環境。該屬性支持以下值:
- MyBatis3 :會生成example類,支持幾乎所有的動態子句。此外,生成的java對象還支持 JSE 5.0特性,包括泛型和註解。
- MyBatis3Simple:使用這個值,不會生成example類,僅支持很少的動態SQL。生成的java對象支持 JSE 5.0特性,包括泛型和註解。
- MyBatis3DynamicSql:使用這個值,MBG將生成與 “Mybatis v3.4.2 及以上版本 和 Java 8 及以上版本”相兼容的對象(例如,Java模型和映射器接口將使用泛型類型)。targetRuntime使用此值時,還有以下內容需要注意:
- 無論爲“defaultModelType”指定什麼,model對象都以“flat”模式生成。這也意味着沒有“with BLOBs”和“without BLOBs”方法。
- 不管爲<javaClientGenerator>的“type”指定爲什麼,映射器都會生成爲帶註解的映射器。
- 不會生成XML。<sqlMapGenerator>不是必需的,如果指定將被忽略。
- MyBatis Dynamic SQL以“per query”方式支持表別名,而不是“all or nothing”方式。出於這個原因,配置的表別名被忽略。
- 使用MyBatis3DynamicSql 生成的Java代碼依賴於“MyBatis Dynamic SQL”支持庫
使用 默認<context id="context1">的寫法不會生成example和xml文件,因此需要改爲
<context id="context1" targetRuntime="MyBatis3" defaultModelType="flat">
配置文件 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="context1" targetRuntime="MyBatis3" defaultModelType="flat">
<!--mybatis generator 根據數據庫自動生成類 -->
<commentGenerator>
<!-- 關閉自動生成的註釋 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection connectionURL="jdbc:mysql://localhost:3306/test1" driverClass="com.mysql.jdbc.Driver" password="1" userId="root" />
<javaModelGenerator targetPackage="com.ppp.ssm.model" targetProject="ssm" />
<sqlMapGenerator targetPackage="com.ppp.ssm.mapper" targetProject="ssm" />
<javaClientGenerator targetPackage="com.ppp.ssm.mapper" targetProject="ssm" type="XMLMAPPER" />
<table tableName="user" domainObjectName="User"
enableCountByExample="true" enableUpdateByExample="true"
enableDeleteByExample="true" enableSelectByExample="true"
selectByExampleQueryId="true">
<columnOverride column="id" property="id" />
</table>
</context>
</generatorConfiguration>