逆向工程源码加详解

什么逆向工程:即根据数据库自动生成实体类以及mapper、mapper.xml文件

那么什么时候可以用到呢?

       对于以为一位初学者来说。你进入公司不可能一进入就是项目组长吧。肯定要一步一步的做起来,比如一个项目项目组长给你建好了数据库,字段已经建好,那我们总不能看着数据库去一个一个的去建实体类和数据库的操作。就出现了逆向工程,我们可以根据项目组长的数据库字段,建立相应的实体类 xml文件 和mapper接口。我们拿到这些东西就可以去书写我们的代码。是不是很方便。很快捷,省的我们再去根据数据库自己去建。大大的提高了效率。

 

 

pom.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.scm</groupId>
    <artifactId>generator</artifactId>
    <version>1.0-SNAPSHOT</version>


   <dependencies>
       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
           <version>5.1.34</version>
       </dependency>
   </dependencies>
<!--逆向工程的插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>


</project>

 

 

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>
    <!-- 配置mysql 驱动jar包路径.用了绝对路径 在你的maven工程里面找到你用数据库的版本 复制绝对路径-->
    <classPathEntry location="F:\maven\mavenMatter\mysql\mysql-connector-java\5.1.34/mysql-connector-java-5.1.34.jar" />

    <context id="scm_mysql_tables" targetRuntime="MyBatis3">
        <!-- 防止生成的代码中有很多注释,加入下面的配置控制 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
            <property name="suppressDate" value="true" />
        </commentGenerator>

        <!-- 数据库连接 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://localhost:3306/easybuy?characterEncoding=UTF-8"
                        userId="root"
                        password="123">
        </jdbcConnection>

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

        <!-- 数据表对应的model层  -->
        <javaModelGenerator targetPackage="cn.hd.entity"
                            targetProject="src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- sql mapper 映射配置文件 -->
        <sqlMapGenerator targetPackage="cn.hd.mapper"
                         targetProject="src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- mybatis3中的mapper接口 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="cn.hd.mapper"
                             targetProject="src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 数据表进行生成操作 schema:相当于库名; tableName:表名;
            domainObjectName:对应的实体类名
        -->
        <table schema="easybuy" tableName="easybuy_news" domainObjectName="News"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <table schema="easybuy" tableName="easybuy_order" domainObjectName="Order"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <table schema="easybuy" tableName="easybuy_order_detail" domainObjectName="Detail"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>


        <table schema="easybuy" tableName="easybuy_product" domainObjectName="Product"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>


        <table schema="easybuy" tableName="easybuy_product_category" domainObjectName="Category"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>


        <table schema="easybuy" tableName="easybuy_user" domainObjectName="User"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <table schema="easybuy" tableName="easybuy_user_address" domainObjectName="Address"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
    </context>
</generatorConfiguration>

3.点击

4.

 

 

他会自动帮你生产

 

希望对你们有所帮助,哪里不懂的话可以提问

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值