使用MyBatis Generator生成DAO

本文介绍了如何使用MyBatis Generator进行DAO生成。首先,在Eclipse中通过指定URL安装Generator插件,然后在src/main/resources目录下创建generatorConfig.xml配置文件。当遇到'path for project must have only one segment'错误时,解决方案是将targetProject设置为工程名称,而非具体路径。
摘要由CSDN通过智能技术生成
Help--Install new software--add

在弹出框中填入generator,地址是http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/

然后选中搜索出的结果,下一步直到安装结束。

在src/main/resources下添加 名为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="D:/mysql-connector-java-5.1.22-bin.jar" />
	<context id="DB2Tables" targetRuntime="MyBatis3">
		<!-- 用来除去时间信息的,这在配合类似subversion的代码管理工具时使用很有效 -->
		<commentGenerator>
			<property name="suppressDate" value="true" />
			<!--是否去除自动生成的注释true:是:false:否 -->
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<!--数据库链接URL,用户名、密码 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://10.15.172.108:3306/userdata" userId="userdata"
			password="43f59a7e5d">
		</jdbcConnection>
		<!-- 类型转换的信息 -->
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>
		<!-- 生成模型的包名和位置 -->
		<javaModelGenerator targetPackage="test.model"
			targetProject="DBToJavaBean">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
		<!-- 生成映射文件的包名和位置 -->
		<sqlMapGenerator targetPackage="test.mapping"
			targetProject="DBToJavaBean">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</sqlMapGenerator>
		<!-- 生成DAO的包名和位置 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="test.dao" targetProject="DBToJavaBean">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		<!-- 要生成哪些表 -->
		<table tableName="position_data" domainObjectName="PositionData"
			enableCountByExample="false" enableUpdateByExample="false"
			enableDeleteByExample="false" enableSelectByExample="false"
			selectByExampleQueryId="false"></table>
		<!-- <table tableName="user" domainObjectName="UserDto" enableCountByExample="false" 
			enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" 
			selectByExampleQueryId="false"></table> <table tableName="syslogs" domainObjectName="SyslogsDto" 
			enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" 
			enableSelectByExample="false" selectByExampleQueryId="false"></table> -->
	</context>
</generatorConfiguration>  

然后再该配置文件上右键,Generator Mybatis/IBatis Aftifacts,刷新目录,即可看到生成的文件。

有时候会出现如下错误path for project must have only one segment

修改方法是,将targetProject改成项目的工程名,而不是src/main/java或../src/main/java




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值