eclipse中根据数据库表逆向生成实体类,mapper接口,mapper.xml文件

逆向工程的核心是: mybatis-generator-core-1.3.2.jar这个jar包

一.安装eclipse插件:mybatis-generator

安装方法有很多,本人使用的是:点help,然后marketplace,搜索mybatis,安装,重启.

二.新建generatorConfig.xml文件

点自己的项目,new,other,找到mybatis文件夹,里面有所需文件

三.配置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>   
    <!-- 数据库连接的jar包路径(oracle同理) --> 
    <classPathEntry location="D:\maven\repository\mysql\mysql-connector-java\5.1.28\mysql-connector-java-5.1.28.jar" /> 
    <context id="mysqlTables" targetRuntime="MyBatis3">    
        <!-- 生成的pojo,将implements Serializable-->    
        <!--  <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>  -->
        <commentGenerator>    
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->    
            <property name="suppressAllComments" value="true" />    
        </commentGenerator>    

        <!-- 数据库链接URL、用户名、密码 (这里写死了mysql,其实Oracle也可以)-->    
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"    
                        connectionURL="jdbc:mysql://localhost:3306/dbName"    
                        userId="root"    
                        password="123456">    
        </jdbcConnection>    

        <!--      
           默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer    
            true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal      
        -->    
        <javaTypeResolver>    
            <property name="forceBigDecimals" value="true" />    
        </javaTypeResolver>    

        <!--     
        生成model模型,对应的包路径,以及文件存放路径(targetProject),targetProject可以指定具体的路径   
        -->
        <javaModelGenerator targetPackage="com.project.entity" targetProject="yourProject">   
            <!-- enableSubPackages:是否让schema(数据库名)作为包的后缀 -->  
            <property name="enableSubPackages" value="true"/>    
            <!-- 从数据库返回的值被清理前后的空格  -->    
            <property name="trimStrings" value="true" />    
        </javaModelGenerator>    

        <!--对应的mapper.xml文件  -->    
        <sqlMapGenerator targetPackage="mapper" targetProject="yourProject/src/main/resources">    
            <property name="enableSubPackages" value="true"/>    
        </sqlMapGenerator>    

        <!-- 对应的Mapper接口类文件 -->    
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.project.mapper" targetProject="yourProject">    
            <property name="enableSubPackages" value="true"/>    
        </javaClientGenerator>    


        <!-- 列出要生成代码的所有表,这里配置的是不生成Example文件 -->   
        <table schema="dbName" tableName="tableName" domainObjectName="EntityName"    
               enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"    
               enableSelectByExample="false" selectByExampleQueryId="false" >    
            <property name="useActualColumnNames" value="false"/>    
        </table>    
    </context>    
</generatorConfiguration>

四.运行generatorConfig.xml文件

右键此文件,run as , run mybatis-generator,就是那个小鸟图标的…

注: 除了使用eclipse插件,还可以使用命令行,maven,java代码等实现逆向工程,这里只介绍这一种

猜你喜欢

转载自blog.csdn.net/hero_hope/article/details/81534170