如何使用Mybatis-Generator自动生成代码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/redsoft_mymuch/article/details/78204730

如何使用Mybatis-Generator自动生成代码

目前很流行SSM框架的使用,在用到mybatis时,dao、mapping、model代码可以用mybatis的自动生成工具生成。下面就介绍2种生成的方式。


方法1 使用windows CMD命令执行

  • 访问 mybatis-generator的下载地址 mybatis
  • 选择一个版本【mybatis-generator-1.3.5.zip】进行下载。
  • 下载后解压,得到一个【mybatis-generator-core-1.3.5.jar】文件
  • 把这个文件和 【mysql-connector-java-5.1.40.jar】放在同一文件夹下

注意:文件夹请不要使用中文,否则会乱码导致生成失败。

<?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="C:\mybatis-generator\mysql-connector-java-5.1.40.jar" />
	<context id="DB2Tables" targetRuntime="MyBatis3">
		<commentGenerator>
			<property name="suppressDate" value="true" />
			<!-- 是否去除自动生成的注释 true:是 : false:否 -->
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<!--数据库链接URL,用户名、密码 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://ws-test-97/mybatis" userId="test"
			password="Password">
		</jdbcConnection>
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>
		<!-- 生成模型的包名和位置 -->
		<javaModelGenerator targetPackage="cn.redsoft.com.vo"
			targetProject="MyBatisGenerator" >
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
		<!-- 生成映射文件的包名和位置 -->
		<sqlMapGenerator targetPackage="cn.redsoft.com.mapping"
			targetProject="MyBatisGenerator" >
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>
		<!-- 生成DAO的包名和位置 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="cn.redsoft.com.dao" targetProject="MyBatisGenerator" >
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名 -->
		<table tableName="t_userlistdaliy" domainObjectName="UserListDaliy"
			enableCountByExample="false" enableUpdateByExample="false"
			enableDeleteByExample="false" enableSelectByExample="false"
			selectByExampleQueryId="false"></table>
		<table tableName="t_userlistdiff" domainObjectName="UserListDiff"
			enableCountByExample="false" enableUpdateByExample="false"
			enableDeleteByExample="false" enableSelectByExample="false"
			selectByExampleQueryId="false"></table>
	</context>
</generatorConfiguration>    
  • 在jar的所在目录下运行CMD执行以下的命令
java -jar mybatis-generator-core-1.3.5.jar -configfile generatorConfig.xml -overwrite
  • 就可以在指定的目录下找到生成的代码了,把这些生成的内容复制到你的工程中即可。

注意:如果提示targetproject:MyBatisGenerator不存在,需要手动创建一个这样的目录在C:\mybatis-generator\下。


方法2 使用eclipse的mybatis-generator插件执行

  • 下载eclipse的插件,比如org.mybatis.generator.eclipse.site-1.3.7.201807042148.zip,解压后重启eclipse。
  • 打开eclipse,New→Other→MyBatis Generator Configuration File创建上文中的XML配置文件。
  • 然后在generatorConfig.xml上右键→Run As→Run Mybatis Generator,就可以在当前project下生成对应的package和文件。

猜你喜欢

转载自blog.csdn.net/redsoft_mymuch/article/details/78204730
今日推荐