查阅文档得知这主要由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>