逆向工程的简单创建

逆向工程工具使用

1、简介

  • 根据数据表,自动生成代码

    • 实体类

    • mapper文件

    • dao接口

2、如何配置逆向工具

  • 新建maven模块,配置pom文件

    <?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.gec</groupId>
       <artifactId>gen_mybatis</artifactId>
       <version>1.0-SNAPSHOT</version>

       <dependencies>
           <dependency>
               <groupId>org.mybatis.generator</groupId>
               <artifactId>mybatis-generator-core</artifactId>
               <version>1.3.7</version>
           </dependency>

           <dependency>
               <groupId>mysql</groupId>
               <artifactId>mysql-connector-java</artifactId>
               <version>5.1.38</version>
           </dependency>

       </dependencies>

       <build>
           <plugins>
               <plugin>
                   <groupId>org.apache.maven.plugins</groupId>
                   <artifactId>maven-compiler-plugin</artifactId>
                   <version>3.6.0</version>
                   <configuration>
                       <source>1.8</source>
                       <target>1.8</target>
                   </configuration>
               </plugin>
               <plugin>
                   <groupId>org.mybatis.generator</groupId>
                   <artifactId>mybatis-generator-maven-plugin</artifactId>
                   <version>1.3.7</version>
                   <configuration>
                       <!--配置文件的路径 -->
                       <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                       <overwrite>true</overwrite>
                   </configuration>
               </plugin>
           </plugins>
       </build>


    </project>
  • 在resources目录配置

    • datasource.properties

      jdbc.driverClass=com.mysql.jdbc.Driver
      jdbc.url=jdbc:mysql://localhost:3306/oa_db?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
      jdbc.username=root
      jdbc.password=1111

       

    • 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>
      <!--导入属性配置-->
      <properties resource="datasource.properties"/>
      <classPathEntry location="D:\repository\mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar"/>
      <context id="test" targetRuntime="MyBatis3">
      <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>
      <commentGenerator>
      <!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 false:表示保护 -->
      <!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,不利于版本控制,所以设置为true -->
      <property name="suppressDate" value="true" />
      <!-- 是否去除自动生成的注释 true:是 : false:否 -->
      <property name="suppressAllComments" value="true" />
      <property name="javaFileEncoding" value="UTF-8"/>
      </commentGenerator>
      <!--数据库链接URL,用户名、密码 -->
      <!--jdbc的数据库连接,直接写死也可以 -->
      <jdbcConnection
      driverClass="${jdbc.driverClass}"
      connectionURL="${jdbc.url}"
      userId="${jdbc.username}"
      password="${jdbc.password}">
      </jdbcConnection>
      <javaTypeResolver>
      <!-- This property is used to specify whether MyBatis Generator should
      force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, -->
      <property name="forceBigDecimals" value="false" />
      </javaTypeResolver>
      <!-- 生成模型的包名和位置 -->
      <javaModelGenerator targetPackage="com.gec.oasys.pojo"
      targetProject="target">
      <property name="constructorBased" value="true"/>
      <property name="enableSubPackages" value="false" />
      <!-- String getXxxx return xxx.trim() -->
      <property name="trimStrings" value="true" />
      </javaModelGenerator>
      <!-- 生成映射文件的包名和位置 -->
      <sqlMapGenerator targetPackage="com.gec.oasys.mapper"
      targetProject="target">
      <property name="enableSubPackages" value="true" />
      </sqlMapGenerator>
      <!-- 生成DAO的包名和位置 -->
      <javaClientGenerator type="XMLMAPPER"
      targetPackage="com.gec.oasys.dao" implementationPackage="com.gec.oasys.dao.impl"
      targetProject="target">
      <property name="enableSubPackages" value="true" />
      </javaClientGenerator>

      <!-- 要生成哪些表 -->
      <table tableName="sys_department" domainObjectName="SysDepartMent"
      enableCountByExample="false" enableUpdateByExample="false"
      enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false"></table>

      </context>
      </generatorConfiguration>

3、如何运行插件生成代码

 

 

 

 

 

 

 

 

 

posted @ 2020-11-09 13:56  hya是p10  阅读(148)  评论(0)    收藏  举报