首页 > 技术文章 > Mybatis自动生成实体类、dao接口和mapping映射文件

shawWey 2017-03-23 11:36 原文

由于Mybatis是一种半自动的ORM框架,它的工作主要是配置mapping映射文件,为了减少手动书写映射文件,可以利用mybatis生成器,自动生成实体类、dao接口以及它的映射文件,然后直接拷贝到工程中稍微修改就可以直接使用了。

生成器目录如下:

首先进入lib文件夹中,该目录如下:

(图上文件下载地址:http://download.csdn.net/detail/qiwei31229/9790909

主要修改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>  
 <!-- 数据库驱动--> 
    <classPathEntry  location="mysql-connector-java-5.1.25-bin.jar"/>  
    <context id="DB2Tables"  targetRuntime="MyBatis3">  
        <commentGenerator>  
            <property name="suppressDate" value="true"/>  
              <!-- 是否去除自动生成的注释 true:是 : false:否 --> 
            <property name="suppressAllComments" value="true"/>  
        </commentGenerator>  
        <!--数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/test" userId="root" password="123456">  
        </jdbcConnection>  
        <javaTypeResolver>  
            <property name="forceBigDecimals" value="false"/>  
        </javaTypeResolver>  
        <!-- 生成模型的包名和位置-->    
        <javaModelGenerator targetPackage="com.shaw.cn.domain" targetProject="src">  
            <property name="enableSubPackages" value="true"/>  
            <property name="trimStrings" value="true"/>  
        </javaModelGenerator>  
        <!-- 生成映射文件的包名和位置-->    
        <sqlMapGenerator targetPackage="com.shaw.cn.mapping" targetProject="src">  
            <property name="enableSubPackages" value="true"/>  
        </sqlMapGenerator>  
     <!-- 生成DAO的包名和位置-->    
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.shaw.cn.IDao" targetProject="src">  
            <property name="enableSubPackages" value="true"/>  
        </javaClientGenerator>  
     <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->   
        <table tableName="user_t" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>  
</generatorConfiguration>  

注意表要现在具体的数据库里头创建好,然后在lib文件夹中shift+鼠标右键打开控制台命令输入:

Java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

这样就可以在src看到制定的目录的代码了。

 

推荐阅读