做逆向工程需要安装mybatis generator插件。

在Eclipse安装插件:

1.先下载插件,插件文件夹是两个文件夹,一个是features,一个是plugins。将这两个文件夹放到Eclipse文件夹的dropins目录下

2.重启Eclipse,new -> other 就可以看见Generator插件了:

 

在config下建一个mybatis-generator.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 >   
    <!-- 数据库mysql驱动包jar路径,路径不能有中文 -->  
    <classPathEntry location="D:\mysql-connector-java-5.0.8-bin.jar"/>  
<!-- 配置数据源和生成代码所存放的位置 -->  
  <context id="context1" >  
    <!-- 注释 -->  
    <commentGenerator>  
        <property name="suppressAllComments" value="false"/><!-- 是否取消注释,true就是屏蔽注释,false就是要注释 -->  
    </commentGenerator>  
    <!-- jdbc连接 -->  
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"  
     connectionURL="jdbc:mysql://127.0.0.1:3306/mybatise"  
     userId="${jdbc.username}"  
     password="${jdbc.password}" />  
    
    <!-- 所生成的实体类的默认资源包src
        targetProject:项目名
        targetPackage:包名
    --> 
    <javaModelGenerator targetPackage="xxx.x.model" targetProject="MyFirstMybatisProject">
    </javaModelGenerator>  

    <!-- 所生成的SQLMap的映射文件的位置,默认资源包src -->  
    <sqlMapGenerator targetPackage="xxx.x.mapper" targetProject="MyFirstMybatisProject">
    </sqlMapGenerator>
    
    <!-- 指定对哪张表做逆向,表名schema:不用填写,其余属性是急用例子查询的生成 -->
    <table schema="" tableName="person"   
        domainObjectName="Items" enableCountByExample="false"  
        enableDeleteByExample="false" enableSelectByExample="false"  
        enableUpdateByExample="false">  
        <!-- 忽略列,不生成bean字段 -->  
<!--         <ignoreColumn column="FRED"/> -->  
        <!-- 指定列的java数据类型 -->  
<!--       <columnOverride column="PRICE" javaType="double" /> -->  
    </table>  
  </context>  
</generatorConfiguration> 

 

保存,然后右键mybatis-generator.xml 文件,点Generate Mybatis/IBatis Artifacts

然后就生成了xxx.x.model和xxx.x.mapper

xxx.x.mapper下自动生成了PersonMapper.xml,代码都生成好了

xxx.x.model下自动生成的Person.java实体类

 逆向工程generator的xml里的table可以写多个:

    <table schema="" tableName="order"   
        domainObjectName="Items" enableCountByExample="false"  
        enableDeleteByExample="false" enableSelectByExample="false"  
        enableUpdateByExample="false">  
    </table>  
    <table schema="" tableName="order_detail"   
        domainObjectName="Items" enableCountByExample="false"  
        enableDeleteByExample="false" enableSelectByExample="false"  
        enableUpdateByExample="false">  
    </table> 

 

posted on 2018-05-08 22:30  lonske  阅读(129)  评论(0编辑  收藏  举报