Mybatis利用generator工具快速生成映射配置文件

本文介绍了一种利用MyBatis Generator快速生成Mapper文件的方法,包括配置generator.xml及执行生成命令的具体步骤。

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

了解Mybatis的人都知道Mybatis的一套反向映射机制,我们刚开始学习的时候,可能手动去书写,但是如果当在实际工作中时,数据库中会设计很多表,这样手动书写就不怎么现时了,这里介绍一种自动快速生成的方法;

第一步:创建一个用于测试额数据库springBoot-demo,里面建两张表用于测试使用,分别为user_info,account_info;

第二步:下载工具generator,https://download.youkuaiyun.com/download/qq_35308803/10448680;修改generator.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:\dataFile\workDocuments\Development\generator\mysql-connector-java-5.1.34.jar" /> 
	<!-- <classPathEntry location="C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" />-->
	<context id="DB2Tables" targetRuntime="MyBatis3">
		<commentGenerator>
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<!-- 数据库链接URL、用户名、密码 -->
		 <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/springBoot-demo" userId="root" password="password"> 
		<!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa">-->
		</jdbcConnection>
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>
		<!-- 生成模型的包名和位置 -->
		<javaModelGenerator targetPackage="andy.model" targetProject="D:\dataFile\workDocuments\Development\generator\src">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
		<!-- 生成的映射文件包名和位置 -->
		<sqlMapGenerator targetPackage="andy.mapping" targetProject="D:\dataFile\workDocuments\Development\generator\src">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>
		<!-- 生成DAO的包名和位置 -->
		<javaClientGenerator type="XMLMAPPER" targetPackage="andy.dao" targetProject="D:\dataFile\workDocuments\Development\generator\src">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		<!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
		<table tableName="user_info" domainObjectName="UserInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
		<table tableName="account_info" domainObjectName="AccountInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
    </context>

</generatorConfiguration>

第三步:在cmd命令窗口中将工作盘切换到generator目录下面,输入并执行java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite命令;看到下图信息表明映射成功

第四步:在generator目录中的src目录中查看映射成的文件;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值