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

相关文章
相关标签/搜索