新建 IDEA Maven 项目
打开IDEA,点击Create New Project,在弹出的界面中选择Maven(左侧菜单列表),右侧选择好Project SDK版本,这里的版本是1.8,如果你没有设置那么可以点击New按钮重新设置JAVA JDK目录。
选中Create from archetype,
在下面的列表中选中org.apache.maven.archetypes:maven-archetype-webapp,点击Next按钮 (如图),
此处为您本地安装maven的路径
如果没有生成java 目录的话,手动在main目录下添加 java,resources 两个目录
设置java目录为source,resources为资源包 File --》ProjectStructure...
然后在 resources目录下新建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>
<!-- 配置mysql 驱动jar包路径.用了绝对路径,就是maven仓库下的mysql的jar -->
<classPathEntry location="/Users/huangfeixiang/.m2/repository/mysql/mysql-connector-java/5.1.34/mysql-connector-java-5.1.34.jar" />
<context id="mysql_tables" targetRuntime="MyBatis3">
<!-- 防止生成的代码中有很多注释,加入下面的配置控制 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
<property name="suppressDate" value="true" />
</commentGenerator>
<!-- 数据库连接 请使用您自己的数据库配置-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true"
userId="test"
password="test">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 数据表对应的model层 -->
<javaModelGenerator targetPackage="com.mybatis.generator.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- sql mapper 映射配置文件 -->
<sqlMapGenerator targetPackage="com.mybatis.generator.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- mybatis3中的mapper接口 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.mybatis.generator.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 数据表进行生成操作 schema:相当于库名; tableName:表名; domainObjectName:对应的DO -->
<table schema="test" tableName="test_table" domainObjectName="TestTable"
enableCountByExample="true" enableUpdateByExample="true"
enableDeleteByExample="true" enableSelectByExample="true"
selectByExampleQueryId="true">
</table>
</context>
</generatorConfiguration>
项目的pom 文件,参考如下配置
<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mybatis.generator</groupId>
<artifactId>mybatisgenerator</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite></configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.1</version>
</dependency>
<!-- mybatis-generator-core 反向生成java代码-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
</project>
到此配置结束,点击 IDEA右侧的 Maven标签 如下图
右键运行 Run Maven Build