您的位置:首页 > 编程语言 > Java开发

eclipse 使用maven generator 插件自动生成mybatis所需代码

2017-06-06 14:31 681 查看
一、 maven pom.xml文件中引入plugin插件



<plugins>

        <plugin>

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

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

                <version>1.3.5</version>

                <configuration>

                   <!--  mybatis-generator 配置文件存放地址 -->

                    <configurationFile>src/test/resources/generator/mybatis-generator.xml</configurationFile>

                    <overwrite>true</overwrite>

                </configuration>

                <dependencies>

                    <dependency>

                        <groupId>mysql</groupId>

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

                        <version>5.1.26</version>

                    </dependency>

                </dependencies>

            </plugin>

<plugins>

二、在创建mybatis-generator.xml配置文件

注意:mybatis-generator.xml文件创建位置与pom.xml文件中plugin插件中指定的配置文件相匹配


创建 mybatis-generator.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>

    <!--数据库驱动 -->

    <context id="generatorTables" 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" />

        <plugin type="org.mybatis.generator.plugins.MapperConfigPlugin">

            <property name="fileName" value="mybatis-config.xml" />

            <property name="targetPackage" value="/" />

            <property name="targetProject" value="src/main/resources" />

        </plugin>

        <!-- <plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin">

            <property name="searchString" value="Example" /> <property name="replaceString"

            value="Criteria" /> </plugin> -->

        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />

        <commentGenerator>

            <property name="suppressAllComments" value="true" />

            <property name="suppressDate" value="true" />

        </commentGenerator>


      <!--  数据库连接地址 -->


        <jdbcConnection driverClass="com.mysql.jdbc.Driver"

            connectionURL="jdbc:mysql://192.168.0.1:3306/test" userId="root"

            password="root">

        </jdbcConnection>

        <javaTypeResolver>

            <property name="forceBigDecimals" value="false" />

        </javaTypeResolver>

        <!-- 指定自动生成model的位置 -->

        <javaModelGenerator targetPackage="com.test.dal.dao.mybatis.model"

            targetProject="src/main/java">

            <property name="enableSubPackages" value="true" />

            <property name="trimStrings" value="true" />

        </javaModelGenerator>

        <!-- 指定自动生成mapper的位置 -->

        <sqlMapGenerator targetPackage="com.test.dal.dao.mybatis.mapper"

            targetProject="src/main/java">

            <property name="enableSubPackages" value="true" />

        </sqlMapGenerator>

        <!-- 指定自动生成mapper接口的位置 -->

        <javaClientGenerator type="XMLMAPPER"

            targetPackage="com.test.dal.dao.mybatis" targetProject="src/main/java">

            <property name="enableSubPackages" value="true" />

        </javaClientGenerator>

        <!--

        <table tableName="student" domainObjectName="Student"

             enableCountByExample="false" enableUpdateByExample="false"

            enableDeleteByExample="false" enableSelectByExample="false"

            selectByExampleQueryId="false">         

         </table>

          -->

         <!--

        <table tableName="teacher" domainObjectName="Teacher"

             enableCountByExample="false" enableUpdateByExample="false"

            enableDeleteByExample="false" enableSelectByExample="false"

            selectByExampleQueryId="false">

            <property name="useActualColumnNames" value="true" />

            <property name="mergeable" value="true" />             

         </table>

           -->

  

    </context>

</generatorConfiguration>


注意:具体配置标签内容请查看mybatis-generator开源文档或google搜索



三、生成代码

(1)如果是在eclipse 中,选择项目或者pom.xml文件,

鼠标单击右键先择Run AS——>Maven Build… ——>

在Goals框中输入:mybatis-generator:generate 

(2)如果在命令行输入Maven命令即可

注意:一定是当前项目目录下运行该命令:

mvn mybatis-generator:generate

注意:如果使用generator已经生成过一次代码,那么在下次修改数据库表结构后,再次生成对应表时,一定要把原来的***Mapper.xml文件和其他对应的文件进行删除,否则在***Mapper.xml文件中会生成多个代码,在运行过程中会报错。



代码生成完,大功告别成。





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  eclipse maven mybatis