1.要有一个maven工程,推荐自己新搭建一个,不要放在主项目下,因为一般最终都只是要获取pojo、mapper接口及映射文件,把这些复制粘贴出去就完事了.,这个工程与主项目关联不大,可以用完即删或者留着以后用,到时修改一下配置就好.
结构如下:
2.在pom.xml中添加mybatis-generator-maven-plugin插件,其中configurationFile根据配置文件generationConfig.xml路径自行修改
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
3.添加generationConfig.xml文件到src/main/resources目录下,这里需要个人配置的有
1.数据库连接的信息
2.指定数据库表,以及pojo类,映射文件,mapper接口的生成位置(一般pojo类单独放,mapper映射文件和接口放一起)
3.指定特定数据库的jbdc驱动jar包的位置(没有指定会报错Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate (default-cli) on project mybatis-generator: Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate failed: Exception getting JDBC Driver: com.mysql.jdbc.Driver -> [Help 1])
<?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="C:mysql-connector-java-5.1.8-bin.jar" />
<context id="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/testbase" userId="root"
password="root">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="com.test.pojo"
targetProject=".\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.test.mapper"
targetProject=".\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.test.mapper"
targetProject=".\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<table schema="" tableName="table1"></table>
<table schema="" tableName="table2"></table>
<table schema="" tableName="table3"></table>
</context>
</generatorConfiguration>
4.执行,run
5,大功告成!其实没有,还要拷贝到其他项目
pojo的拷贝通常不会有什么问题,但是要注意mapper是依赖pojo的.工具会帮你管理依赖,但是还是会出现Cannot resolve symbol 'Param,这个时候我们发现
import org.apache.ibatis.annotations.Param;是灰色的,提示Unused import statement,这个时候就需要我们手动到pom.xml添加以下依赖了
<dependency>
<groupId>org.apache.ibatis</groupId>
<artifactId>ibatis-core</artifactId>
<version>3.0</version>
</dependency>