一:在Pom中添加插件
<plugins>
<plugin>
<!--Mybatis-generator插件,用于自动生成Mapper和POJO-->
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>
${basedir}/src/main/resources/mybatis-generator-config.xml
</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
二:在resource下创建mybatis-generator-config.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>
<!-- 本地数据库驱动程序jar包的全路径,修改为你自己的驱动位置 -->
<classPathEntry location="C:\xxxx\.m2\repository\mysql\mysql-connector-java\mysql-connector-java-5.1.6.jar"/>
<context id="context" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="false"/>
<property name="suppressDate" value="true"/>
</commentGenerator>
<!-- 数据库的相关配置 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/classroom?serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true"
userId="root"
password="123456"/>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 实体类生成的位置 -->
<javaModelGenerator targetPackage="cn.zz.pojo" targetProject="D:\xxx\src\main\java">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- *Mapper.xml 文件的位置 -->
<sqlMapGenerator targetPackage="mapper" targetProject="D:\xxx\src\main\resources\">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- Mapper 接口文件的位置 -->
<javaClientGenerator targetPackage="cn.zz.mapper" targetProject="D:\xxx\src\main\java\" type="XMLMAPPER">-->
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 相关表的配置 -->
<table tableName="user" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
</context>
</generatorConfiguration>
有几个注意的地方:
1:以上生成的位置都根据自己的情况修改
2:mysql驱动的版本使用5.1.6
3:数据库connectionURL这里要加一个参数
nullCatalogMeansCurrent=true
connectionURL中不允许使用‘&’,使用&代替
connectionURL="jdbc:mysql://localhost:3306/classroom?serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true"
三:运行插件
在IDEA中右侧打开Maven Projects,双击mybatis-generator
运行成功如下:
然后就可以在自己指定的位置看到pojo、mapper接口和xml文件了
________________________________________________________________________________________________
以上方法在普通的Maven项目中也可以使用,但是springboot“习惯优于配置”的理念可以大大减少我们的配置文件,可以省略xml的配置,实际中也不会自己写UserMapper接口中的方法,通常使用通用的mapper或者Spring Data JPA继承Repository接口提供更丰富友好的方法