Mybatis自动生成实体类,映射文件,以及dao层接口。

      Mybatis不像Hibernate那样可以直接通过Myeclipse直接生成相应的映射文件,它是一个半自动化的ORM框架,所以主要的工作就是配置Mapping映射文件,但是由于手写映射文件很容易出错,所以可利用MyBatis生成器自动生成实体类、DAO接口和Mapping映射文件。这样可以省去很多的功夫,将生成的代码copy到项目工程中即可。

所需要的文件如下:


主要是配置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>  
	<!-- mysql-connector文件路径 -->
    <classPathEntry  location="mysql-connector-java-5.1.25-bin.jar"/>  
    <context id="DB2Tables"  targetRuntime="MyBatis3">  
        <commentGenerator>  
            <property name="suppressDate" value="true"/>  
            
            <property name="suppressAllComments" value="true"/>  
        </commentGenerator>  
        <!-- 链接配置 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" 
		connectionURL="jdbc:mysql://127.0.0.1:3306/ssm" 
		userId="root" password="root">  
        </jdbcConnection>  
        <javaTypeResolver>  
            <property name="forceBigDecimals" value="false"/>  
        </javaTypeResolver>  
        <!-- 生成实体类的路径,com.project.ssm.entity 这个路径可以自动生成,但是必须有src这个路径-->
        <javaModelGenerator targetPackage="com.project.ssm.entity" 
		targetProject="src">  
            <property name="enableSubPackages" value="true"/>  
            <property name="trimStrings" value="true"/>  
        </javaModelGenerator>  
       <!-- 生成实体类的路径,这个路径可以自动生成,但是必须有src这个路径-->
       <sqlMapGenerator targetPackage="com.project.ssm.mapping" targetProject="src">  
            <property name="enableSubPackages" value="true"/>  
        </sqlMapGenerator>  
        
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.project.ssm.dao" 
		targetProject="src">  
            <property name="enableSubPackages" value="true"/>  
        </javaClientGenerator>  
       
        <table tableName="user" domainObjectName="User" enableCountByExample="false" 
		      enableUpdateByExample="false" enableDeleteByExample="false" 
			  enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>  
</generatorConfiguration>




使用dos命令窗口进入lib文件夹下面运行:java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite


文件已经成功生成:


工具下载地址:http://download.csdn.net/detail/gaoxuaiguoyi/9431576

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值