利用MyBatis Generator自动创建代码

本文详细介绍如何在MyEclipse中安装MyBatisGenerator插件并利用它自动生成基于数据库表的XML映射文件、接口及实体类。通过具体步骤指导用户完成配置文件generatorConfig.xml的设置,并解释了部分配置项的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

功能:创建数据库表后,利用MyBatis Generator自动创建代码(xml,interface,model)。
一、安装myeclipse插件
1 下载http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/下features和plugins文件夹内容。
2 在myeclipse10安装目录的dropins文件夹下新建mybaits-gen(名字随意),将features和plugins拷贝其下,插件即安装成功。如果是myeclipse以前版本,用link方式链接。

二、自动创建代码
1 利用插件创建generatorConfig.xml.
new--other--mybatis--mybatis generator configuration file
[img]http://dl.iteye.com/upload/picture/pic/126395/6696b171-ff92-3f3b-a62c-477f5037508c.jpg[/img]
2 打开generatorConfig.xml

<generatorConfiguration >

<classPathEntry location="D:\mysql-connector-java-5.1.15-bin.jar" />

<context id="context1" >
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.1.118:3306/qhxdb" userId="root" password="root" />
<javaModelGenerator targetPackage="需要生成的包名" targetProject="项目名" />
<sqlMapGenerator targetPackage="需要生成的包名" targetProject="项目名" />
<javaClientGenerator targetPackage=需要生成的包名" targetProject="项目名" type="XMLMAPPER" />

<table schema="数据库名" tableName="表名" ></table>
</context>
</generatorConfiguration>

3 对generatorConfig.xml右键--generator mybatis,即生成。
[img]http://dl.iteye.com/upload/picture/pic/126397/6324ce70-ce49-3be1-aec9-a9b2f2b9bfba.jpg[/img]


其他属性说明:
以下配置可以不生成example这个类
[xml]
<table schema="数据库名" tableName="表名" domainObjectName="给生成的类重命名,不要既跟表名一样" enableSelectByPrimaryKey="true" enableSelectByExample="false" enableDeleteByExample="false"
enableCountByExample="false" enableUpdateByExample="false">
<property name="useActualColumnNames" value="false" />
</table>
[/xml]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值