Mybartis逆向工程

Mybartis逆向工程 java

0.建立工程項目,切記莫用中文,親測在運行時報錯 mysql

 

1.Pom文件,使用mybatis-generator插件sql

<?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.nf</groupId>
    <artifactId>mybatis-generator</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <!-- 使用mybatis-generator插件(簡稱MBG) -->
            <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/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>-->
    </build>
</project>
pom

 

2.建立一個generatorConfig.xml文件,頭部可能報錯,已解決數據庫

<?xml version="1.0" encoding="UTF-8"?>
<!-- 若是下面的頭部報紅,請按照下面的步驟找到相應的地方將報紅的這句加進去 -->
<!-- file  settings  languages  DTDS -->
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <!--數據庫驅動包的絕對路徑 -->
    <classPathEntry location="G:\\Maven\\repository\\mysql\\mysql-connector-java\\5.1.6\\mysql-connector-java-5.1.6.jar" />

    <context id="DB2Tables" targetRuntime="Ibatis2Java5">
        <!-- 去除註釋,防止生成的代碼中有不少註釋  -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!--數據庫鏈接 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/e3mall"
                        userId="root"
                        password="admins">
        </jdbcConnection>

        <!--默認false
           Java type resolver will always use java.math.BigDecimal if the database column is of type DECIMAL or NUMERIC.
         -->
        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!--生成實體類 指定包名 以及生成的地址 (能夠自定義地址,可是路徑不存在不會自動建立  使用Maven生成在target目錄下,會自動建立) -->
        <javaModelGenerator targetPackage="com.e3mall.pojo" targetProject="MAVEN">
            <property name="enableSubPackages" value="false" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!--生成sql mapper文件 -->
        <sqlMapGenerator targetPackage="com.e3mall.mapper"  targetProject="MAVEN">
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

        <!--生成Dao文件 能夠配置 type="XMLMAPPER"生成xml的dao實現  context id="DB2Tables" 修改targetRuntime="MyBatis3"  -->
        <javaClientGenerator type="SPRING" targetPackage="com.e3mall.dao"  targetProject="MAVEN">
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <!--對應數據庫表 mysql能夠加入主鍵自增 字段命名 忽略某字段等-->
        <!-- domainObjectName:生成的pojo類名,可不寫默認 -->
        <table tableName="tb_content" domainObjectName="" />
        <table tableName="tb_content_category" domainObjectName="" />
        <table tableName="tb_item" domainObjectName="" />
        <table tableName="tb_item_cat" domainObjectName="" />
        <table tableName="tb_item_desc" domainObjectName="" />
        <table tableName="tb_item_param" domainObjectName="" />
        <table tableName="tb_item_param_item" domainObjectName="" />
        <table tableName="tb_order" domainObjectName="" />
        <table tableName="tb_order_item" domainObjectName="" />
        <table tableName="tb_order_shipping" domainObjectName="" />
        <table tableName="tb_user" domainObjectName="" />
    </context>
</generatorConfiguration>
generatorConfig.xml

 

3.下面雙擊運行以後生成的文件目錄如上圖 apache

 

大功告成!將生成文件拷貝到項目相應文件夾下便可!mybatis

相關文章
相關標籤/搜索