SpringBoot 之 Mybatis 逆向工程

  1. 今天給你們介紹在 spring- boot 項目中如何使用 maven 插件逆向工程生成 Mybatis 代碼。java

    pom.xml 添加依賴和插件mysql

    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.0</version>
    </dependency>
    
    <build>
     <plugins>
         <plugin>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
               <plugin>
                   <groupId>org.mybatis.generator</groupId>
                   <artifactId>mybatis-generator-maven-plugin</artifactId>
                   <version>1.3.2</version>
                   <configuration>
          <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
                       <overwrite>true</overwrite>
                       <verbose>true</verbose>
                   </configuration>
                   <dependencies>
                       <dependency>
                           <groupId>mysql</groupId>
                           <artifactId>mysql-connector-java</artifactId>
                           <version>5.1.29</version>
                           <scope>runtime</scope>
                       </dependency>
                   </dependencies>
               </plugin>
               <plugin>
                   <groupId>org.jacoco</groupId>
                   <artifactId>jacoco-maven-plugin</artifactId>
                   <version>0.8.2</version>
                   <executions>
                       <execution>
                           <id>pre-test</id>
                           <goals>
                               <goal>prepare-agent</goal>
                           </goals>
                       </execution>
                       <execution>
                           <id>pre-test</id>
                           <phase>test</phase>
                           <goals>
                               <goal>report</goal>
                           </goals>
                       </execution>
                       <execution>
                           <id>post-test-aggregate</id>
                           <phase>test</phase>
                           <goals>
                               <goal>report-aggregate</goal>
                           </goals>
                       </execution>
                   </executions>
               </plugin>
     </plugins>
    </build>

    上圖還配置了 jacoco 插件,爲了使用看代碼單元測試的覆蓋率,不須要的同窗能夠去掉此插件。spring

    在 resources 根目錄下建立 generator 文件夾,再在此文件夾下建立 generatorConfig.xml。sql

    <?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="DB2Tables"  targetRuntime="MyBatis3">
            <commentGenerator>
                <property name="suppressDate" value="true"/>
                <!-- 是否去除自動生成的註釋 -->
                <property name="suppressAllComments" value="true"/>
            </commentGenerator>
            <!--數據庫連接信息 -->
            <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                            connectionURL="jdbc:mysql://localhost:3306/parse_json"
                            userId="root"
                            password="root">
            </jdbcConnection>
            <javaTypeResolver>
                <!-- 是否使用bigDecimal, false可自動轉化如下類型(Long, Integer, Short, etc.) -->
                <property name="forceBigDecimals" value="false"/>
            </javaTypeResolver>
            <!-- 生成實體類的包名和位置 ,targetPackage指的是包名,targetProject值得是路徑位置-->
            <javaModelGenerator targetPackage="com.rookie.model" targetProject="src/main/java">
                <property name="enableSubPackages" value="true"/>
                <property name="trimStrings" value="true"/>
            </javaModelGenerator>
            <!-- 生成映射文件的包名和位置-->
            <sqlMapGenerator targetPackage="main.resources.com.rookie.mapper" targetProject="src">
                <property name="enableSubPackages" value="true"/>
            </sqlMapGenerator>
            <!-- 生成DAO的包名和位置-->
            <javaClientGenerator type="XMLMAPPER" targetPackage="com.rookie.mapper" targetProject="src/main/java">
                <property name="enableSubPackages" value="true"/>
            </javaClientGenerator>
            <!-- 要生成的表 tableName是數據庫中的表名或視圖名 domainObjectName是實體類名-->
            <table tableName="input_file_path" domainObjectName="FilePathDO"
                   enableCountByExample="false" enableUpdateByExample="false"
                   enableDeleteByExample="false" enableSelectByExample="false"
                   selectByExampleQueryId="false">
            </table>
        </context>
    </generatorConfiguration>
  2. 項目結構如圖。按照如圖所示,點擊插件生成便可。
    數據庫

相關文章
相關標籤/搜索