利用MyBatis-Generator自动生成代码

一、MyBatis-Generator介绍

MyBatis-Generator是一个MyBatis相关代码的自动生成工具,使用MyBatis-Generator可以自动生成Dao层代码、Model层代码、Mapping SQL映射文件。

 

二、安装MyBatis-Generator插件

网上下载mybatis generator eclipse插件,安装方式跟其它Eclipse插件安装方式一样,这里我就不多说了,详情请见:http://xieke90.iteye.com/blog/2151568

 

三、配置自动生成代码所需的xml文件

   示例: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>

	<!-- 配置数据库驱动 location:数据库驱动路径 -->
	<classPathEntry
		location="G:\workspace\javaworkspace\MyBatis_Generator_Test\lib\mysql-connector-java-5.1.18.jar" />

	<context id="mbgtest">
	
		<!-- 是否去除自动生成的注释 true:是 : false:否 -->
		<commentGenerator>
			<property name="suppressAllComments" value="true" />
			<property name="suppressDate" value="true" />
		</commentGenerator>

		<!-- 配置数据库链接URL、用户名、密码 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/test" userId="root"
			password="999999" />
		<!-- 配置生成模型的包名和位置 -->
		<javaModelGenerator targetPackage="com.xieke.test.model"
			targetProject="MyBatis_Generator_Test/src" />
		<!-- 配置生成映射文件的包名和位置 -->
		<sqlMapGenerator targetPackage="com.xieke.test.mapper"
			targetProject="MyBatis_Generator_Test/src" />
		<!-- 配置生成DAO的包名和位置 -->
		<javaClientGenerator targetPackage="com.xieke.test.mapper"
			targetProject="MyBatis_Generator_Test/src" type="XMLMAPPER" />

		<!-- 配置需要生成的表 -->
		<table schema="test" tableName="orders" domainObjectName="Orders">
			<!-- 使用从数据库元数据获取的列名作为生成的实体对象的属性 -->
			<property name="useActualColumnNames" value="true" />
			<!-- 指定自动生成主键 -->
			<generatedKey column="id" sqlStatement="MySql" identity="true" />
		</table>
		<table schema="test" tableName="orderItem" domainObjectName="OrderItem">
			<property name="useActualColumnNames" value="true" />
			<generatedKey column="id" sqlStatement="MySql" identity="true" />
		</table>
	</context>

</generatorConfiguration>

   选中generatorConfig.xml文件【右键】,选择【Generate MyBatis/iBATIS Artifacts】就可以生成相应代码了。

 

   

   转载请注明出处:http://xieke90.iteye.com/blog/2240577

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值