pom.xml文件
<!--mybatis逆向生成工具的核心jar-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
</plugin>
generatorConfig.xml
注意只能用这个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="D:/Maven/repo/m2/mysql/mysql-connector-java/5.1.27/mysql-connector-java-5.1.27.jar"/>
<!--生成对象环境 targetRuntime= MyBatis3:默认的值,还有一种是MyBatis3Simple-->
<context id="mybatis" targetRuntime="MyBatis3Simple">
<commentGenerator>
<property name="suppressDate" value="false"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库相关设置-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/spring"
userId="root"
password="123"/>
<!--实体放置目录-->
<javaModelGenerator targetPackage="cn.yanxi.model"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--mybatis映射xml文件放置位置-->
<sqlMapGenerator targetPackage="config.mapping"
targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--mapper接口-->
<javaClientGenerator targetPackage="cn.yanxi.dao"
targetProject="src/main/java"
type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--可以配置多个表-->
<table tableName="user" domainObjectName="User"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
javaClientGenerator的type值
- type:选择怎么生成mapper接口(在MyBatis3/MyBatis3Simple下):
- ANNOTATEDMAPPER:会生成使用Mapper接口+Annotation的方式创建(SQL生成在annotation中),不会生成对应的XML;
- MIXEDMAPPER:使用混合配置,会生成Mapper接口,并适当添加合适的Annotation,但是XML会生成在XML中;
- XMLMAPPER:会生成Mapper接口,接口完全依赖XML;
注意,如果context是MyBatis3Simple:只支持ANNOTATEDMAPPER和XMLMAPPER
table参数说明
- domainObjectName:如果不设置,使用表名作为domain类的名字;
- enableInsert(默认true):指定是否生成insert语句;
- enableSelectByPrimaryKey(默认true):指定是否生成按照主键查询对象的语句(就是getById或get);
- enableSelectByExample(默认true):MyBatis3Simple为false,指定是否生成动态查询语句;
- enableUpdateByPrimaryKey(默认true):指定是否生成按照主键修改对象的语句(即update);
- enableDeleteByPrimaryKey(默认true):指定是否生成按照主键删除对象的语句(即delete);
- enableDeleteByExample(默认true):MyBatis3Simple为false,指定是否生成动态删除语句;
- enableCountByExample(默认true):MyBatis3Simple为false,指定是否生成动态查询总条数语句(用于分页的总条数查询);
- enableUpdateByExample(默认true):MyBatis3Simple为false,指定是否生成动态修改语句(只修改对象中不为空的属性);
- modelType:参考context元素的defaultModelType,相当于覆盖;
参照于网上的一些博文
###运行 测试
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building ssmTest Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- mybatis-generator-maven-plugin:1.3.5:generate (default-cli) @ ssmTest ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.741 s
[INFO] Finished at: 2016-11-19T13:45:23+08:00
[INFO] Final Memory: 10M/155M
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0