mybatis-generator(mybatis根据已创建表反向生成对应的实体类、mapper文件和mapper.xml文件)

1在pom.xml中做两处配置

配置dependency,在<dependencys>中加入:

 <dependency>

        <groupId>org.mybatis.generator</groupId>

         <artifactId>mybatis-generator-core</artifactId>

        <version>1.3.5</version>

 </dependency>

配置plugin

在 <build> 这个节点的<plugins>节点内部加入一个<plugin>,如下:

<plugin>
           <groupId>org.mybatis.generator</groupId>

           <artifactId>mybatis-generator-maven-plugin</artifactId>

           <version>1.3.2</version>

           <executions>

              <execution>

                 <id>Generate MyBatis Files</id>

                 <goals>

                    <goal>generate</goal>

                 </goals>

                 <phase>generate</phase>

                 <configuration>

                    <verbose>true</verbose>

                    <overwrite>true</overwrite>

                 </configuration>

              </execution>

           </executions>
           <dependencies>

              <dependency>

                 <groupId>mysql</groupId>

                 <artifactId>mysql-connector-java</artifactId>

                 <version>5.1.38</version>

              </dependency>

              <dependency>

                 <groupId>org.mybatis.generator</groupId>

                   <artifactId>mybatis-generator-core</artifactId>

                 <version>1.3.5</version>

              </dependency>

              <dependency>

                 <groupId>org.mybatis</groupId>

                 <artifactId>mybatis</artifactId>

                 <version>3.4.2</version>

              </dependency>

           </dependencies>
 </plugin>

    注意:这个plugin里面又使用dependencies引入了mysql 的驱动和mybatis的相关jar包,这个不能省略。

2创建generatorConfig.xml

 在resource目录下创建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>
    <classPathEntry
            location="E:\maven\house\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar"/>
    <context id="mybatis" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="false"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/target?characterEncoding=UTF-8" 
                        userId="root"
                        password="ds8297901"/>

        <javaModelGenerator targetPackage="com.geovis.domain"
                            targetProject="E:\eclipse\GV\target\target-service\src\main\java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="com.geovis.mapper.mapperxml"
                         targetProject="E:\eclipse\GV\target\target-service\src\main\java">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <javaClientGenerator targetPackage="com.geovis.mapper"
                             targetProject="E:\eclipse\GV\target\target-service\src\main\java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

       <table tableName="target" domainObjectName="Target" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
		<table tableName="target_file" domainObjectName="TargetFile" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
		<table tableName="target_classify" domainObjectName="TargetClassify" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
		<table tableName="target_relation" domainObjectName="TargetRelation" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
		<table tableName="target_coordinate" domainObjectName="TargetCoordinate" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>

    </context>
</generatorConfiguration>

3打开项目存在.project文件的这个路径,在此处打开命令窗口

执行mvn mybatis-generator:generate命令,成功之后,刷新项目,就看到自动生成了实体,mapper和mapper.xml文件



猜你喜欢

转载自blog.csdn.net/qq_18298439/article/details/80499988