IDEA中使用maven建立mybatis的逆向工程

逆向工程(Mybatis Generator)

概念

mybatis Generator 中文官網html

mybatis須要程序員本身編寫sql語句,mybatis官方提供逆向工程,能夠針對單表 自動生成mybatis執行所須要的代碼.(mapper.java、mapper.xml、po類)java

常採用數據庫生成java代碼mysql

相關配置文件

這裏使用IDEA自帶的maven插件配置搭建運行環境,固然,官網提供了更多使用generator的方法,具體能夠參考 其餘方法.程序員

1、在使用maven以前,有必要貼一下maven插件所必須的setting.xml (默認是在C:\Users\${username}\.m2路徑下)

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
					http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <localRepository>E:\Java\Maven\repository</localRepository>
    <interactiveMode>true</interactiveMode>
    <usePluginRegistry>false</usePluginRegistry>
    <offline>false</offline>
    <pluginGroups/>
    <servers/>
    <mirrors>
        <mirror>  
            <id>nexus-aliyun</id>  
            <mirrorOf>central</mirrorOf>    
            <name>Nexus aliyun</name>  
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>  
        </mirror> 
    </mirrors>
    <proxies/>
    <profiles/>
    <activeProfiles/>
</settings>

2、接下來,重點是maven 使用的 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>club.lemos</groupId>
    <artifactId>mybatis-generator</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <spring.version>4.3.4.RELEASE</spring.version>
        <mybatis.version>3.4.1</mybatis.version>
        <slf4j.version>1.7.21</slf4j.version>
    </properties>

    <dependencies>
        <!-- mybatis 包-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <!-- mybatis與spring整合包-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>
        <!-- javaee jar包-->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
        </dependency>
        <!-- mysql 驅動包-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.40</version>
        </dependency>
        <!--dbcp2-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.1.1</version>
        </dependency>
        <!-- 日誌文件管理包-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <!-- 複製非java文件到class路徑下-->
            <resource>
                <directory>${basedir}/src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <!-- 配置generatorConfig.xml的路徑和名稱 -->
                <configuration>
                    <configurationFile>
                        ${basedir}\src\main\resources\generatorConfig.xml
                    </configurationFile>
                </configuration>
                <!-- 配置generator運行插件,使maven執行generator -->
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

 

3、下面是generator的重要配置文件

該文件在項目中所在的位置:
這裏寫圖片描述spring

文件的配置信息: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>
    <!-- !!!! Driver Class Path !!!! -->
    <classPathEntry location="E:\Java\Maven\repository-taotao\mysql\mysql-connector-java\5.1.40\mysql-connector-java-5.1.40.jar"/>
    <context id="context" targetRuntime="MyBatis3">
        <!-- 是否去除註釋-->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
            <property name="suppressDate" value="true"/>
        </commentGenerator>
        <!-- !!!! Database Configurations !!!! -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/taotao?characterEncoding=UTF8&amp;useSSL=false"
                        userId="root" password="lemo"/>

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

        <!-- !!!! Model Configurations !!!! -->
        <javaModelGenerator targetPackage="com.taotao.po" targetProject="src/main/java">
            <!-- enableSubPackages:是否讓schema做爲包的後綴 -->
            <property name="enableSubPackages" value="false"/>
            <!-- 從數據庫返回的值被清理先後的空格 -->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- !!!! Mapper XML Configurations !!!! -->
        <sqlMapGenerator targetPackage="com/taotao/mapping" targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!-- !!!! Mapper Interface Configurations !!!! -->
        <javaClientGenerator targetPackage="com.taotao.mapper" targetProject="src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>

        <!-- !!!! Table Configurations !!!! -->
        <!-- 指定數據庫表 -->
        <table schema="" tableName="tb_content"/>
        <table schema="" tableName="tb_content_category"/>
        <table schema="" tableName="tb_item"/>
        <table schema="" tableName="tb_item_cat"/>

    </context>
</generatorConfiguration>

4、配置運行maven

首先數據庫

這裏寫圖片描述

而後apache

這裏寫圖片描述

最後,嘗試運行api

這裏寫圖片描述

若是成功,輸出窗口是這樣的:mybatis

這裏寫圖片描述

同時,相應的文件夾會生成項目須要的文件.

END

相關文章
相關標籤/搜索