要使用generator插件自动生成相关文件,需要引入mybatis-generator-core这个包,在 <dependencys>
中加入
1. 在pom.xml中做两处配置
1.1. 配置dependency
要使用generator插件自动生成相关文件,需要引入mybatis-generator-core这个包,在 <dependencys>
中加入:
1 2 3 4 5 6 7 8
| <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
|
1.2. 配置plugin
在 <build>
这个节点的 <plugins>
节点内部加入一个 <plugin>
,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <configuration> <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> </dependencies> </plugin>
|
2. 创建generatorConfig.xml
2.1. 配置文件路径名称以及内容
在resource目录下创建generatorConfig.xml配置文件,当然了该文件起这个名字,并且放到resource根目录下是根据genereator的默认方案来的,如果要用别的名,放到别的目录也可以,只是要做其它配置,这里就按默认算了,该文件的配置内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| <?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="E:\develop\mavenRepository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar"/> <context id="mysqlTables" targetRuntime="MyBatis3">
<property name="autoDelimitKeywords" value="false"/> <property name="javaFileEncoding" value="UTF-8"/> <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/> <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/personal?characterEncoding=UTF-8" userId="root" password="123456"></jdbcConnection>
<javaTypeResolver type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl">
<property name="forceBigDecimals" value="false"/></javaTypeResolver> <javaModelGenerator targetPackage="com.littlefxc.personal.model" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <sqlMapGenerator targetPackage="mybatis" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.littlefxc.personal.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator>
<table tableName="article" domainObjectName="ArticlePo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <generatedKey column="id" sqlStatement="MySql" identity="true"/> </table>
<table tableName="article_tag" domainObjectName="ArticleTagPo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <generatedKey column="id" sqlStatement="MySql" identity="true"/> </table>
<table tableName="user" domainObjectName="UserPo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <generatedKey column="id" sqlStatement="MySql" identity="true"/> </table> </context> </generatorConfiguration>
|
3. 执行
3.1. 利用idea 执行

3.2. 也可以执行mvn mybatis-generator:generate命令
生成最终效果图结构
