关于使用mybatis时逆向工程()

首先需要在pom.xml中添加build插件与dependency依赖

dependency依赖如下:

  <!-- mybatis 下generator生成工具依赖包-->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>

            <version>1.3.5</version>

        </dependency>
plugins插件
 <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>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.6</version>

                </dependency>

            </dependencies>

        </plugin>

然后创建generatorConfig.xml配置文件,配置要生成数据的信息(在网上看到有的同学喜欢吧数据库的连接信息放在db.properties文件中,凭个人喜好,当然我感觉这个放在db.properties中取出value更好一下)

配置文件信息为:

<?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>
    <!-- 指定连接数据库的JDBC驱动包所在位置,指定到你本机的完整路径(我是用的Oracle的数据库,第一次用这个做开发!!,mysql和他原理一样,找到驱动包即可) -->
    <classPathEntry location="D:\Users\Administrator\.m2\repository\com\oracle\ojdbc6\11.2.0.3\ojdbc6-11.2.0.3.jar"/>

    <!-- 配置table表信息内容体,targetRuntime指定采用MyBatis3的版本 -->
    <context id="tables" targetRuntime="MyBatis3">

        <!-- 抑制生成注释,由于生成的注释都是英文的,可以不让它生成 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--配置信息 驱动类,数据库连接信息,用户名,密码 -->
        <!-- 配置数据库连接信息 -->
        <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
                        connectionURL="jdbc:oracle:thin:@192.168.103.88:1521:clamc"
                        userId="dev_team_3_test"
                        password="test">
        </jdbcConnection>

        <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>


        <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
            targetPackage     指定生成的model生成所在的包名
            targetProject     指定在该项目下所在的路径
        -->
        <javaModelGenerator targetPackage="com.yss.flow.entity"
                            targetProject="src/main/java">

            <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
            <property name="enableSubPackages" value="false"/>
            <!-- 是否对model添加 构造函数 -->
            <property name="constructorBased" value="false"/>
            <!-- 是否对类CHAR类型的列的数据进行trim操作 -->
            <property name="trimStrings" value="true"/>
            <!-- 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 -->
            <property name="immutable" value="false"/>

        </javaModelGenerator>

        <!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="src/main/resources">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
                type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
                type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
                type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
        -->
        <javaClientGenerator targetPackage="com.yss.flow.mapper"
                             targetProject="src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- 对应的表与实体BO
        这里table是可以多实例生成的 
        -->
        <table tableName="T_XXXXXXXXXXXX" domainObjectName="T_XXXXXXXXXXXX"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>

        <table tableName="T_XXXXXXXXXXXX" domainObjectName="XXXXXXXXXXXX"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <table tableName="T_XXXXXXXXXXXX" domainObjectName="T_XXXXXXXXXXXX"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <table tableName="T_XXXXXXXXXXXX" domainObjectName="T_XXXXXXXXXXXX"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
      
    </context>
</generatorConfiguration>

有的表字段和属性名不一致的时候,这个我也想知道怎么吧数据库中表字段的某些参数名字符抑制生成,还有有时候我们使用到lombok不需要生成get,get与构造器。这时候也可以在上面的配置中配置。

配置完信息后就可以在Maven Project下执行了,如下:

双击就可以完成代码生成,(ps:我们在公司有时候是我们的实体类继承了公司的一套代码。这时候我们的代码中的生成的sql语句还有mapper下的接口可能会因为我们继承类中已经存在了该方法或者xml中存在sql语句报错,删除掉我们自己通过插件生成的sql与接口方法就好)

猜你喜欢

转载自blog.csdn.net/weixin_41847607/article/details/81208173