在eclipse中使用MyBatisGenerator方法

方法一:

    1、直接在pom.xml文件中引入mybatis-generator-maven-plugin:

			<!--
			MyBatis Generator插件:根据数据库表结构,自动生成MyBatis所需的实体类、Dao层接口和映射XML文件
			配置官网:http://www.mybatis.org/generator/running/runningWithMaven.html
			-->
			<plugin>
			    <groupId>org.mybatis.generator</groupId>
			    <artifactId>mybatis-generator-maven-plugin</artifactId>
			    <version>${mybatis-generator.version}</version>
			  <!-- 
			    最初,插件类路径非常有限 - 它只包含MyBatis生成器本身。 
			    如果你需要在插件的类路径中添加一些东西(例如,JDBC驱动程序),
			    你可以通过向插件配置中添加依赖项来实现,若配置了以下内容,则
			    在generatorConfig.xml文件中不用配置<classPathEntry/>属性了如下所示:
			   -->
			   <dependencies>
			        <!--添加MySql-connector依赖-->
			        <dependency>
			            <groupId>mysql</groupId>
			            <artifactId>mysql-connector-java</artifactId>
			            <version>${mysql-connector-java.version}</version>
			        </dependency>
			    </dependencies>
			    <configuration>
			        <!--MyBaits-generator的配置文件generatorConfig.xml的位置-->
			        <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
			        <!--是否覆盖同名文件(只是针对XML文件,java文件生成类似*.java.1、*.java.2形式的文件)-->
			        <overwrite>true</overwrite>
			        <!--如果为true,则MBG将进度消息写入构建日志。-->
			        <verbose>true</verbose>
			    </configuration>
			    <!-- 
			    	execution:MBG插件绑定到Maven构建的generate-sources阶段,因此它将在编译步骤之前执行。 
			    	另请注意,MBG生成Java源文件和XML资源。 MBG目标将生成的Java文件和XML资源绑定到构建,
			    	它们都将包含在构建生成的任何JAR中。 
			    -->
				<!-- 			
				<executions>
			        <execution>
			            <id>mybatis-generator</id>
			            <goals>
			                <goal>generate</goal>
			            </goals>
			        </execution>
			    </executions> 
			    -->
			</plugin>

2、右键选中项目Run As ->Maven bulid,然后再在goals中输入mybatis-generator:generate->最后点击run就可以了,如下图:

eclipse中执行Maven命令

goal

eclipse中maven运行mybatis-generator结果

 

 

 

 

 

 

 

 

 

 

 

 

第二种方法去github上下载插件安装,

或者直接用java命令运行:

java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite

 

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值添加到类加载路径中,一般为JDBC driver数据库连接驱动路径。
  <classPathEntry>元素用于将类路径位置添加到MyBatis Generator(MBG)运行的类路径中。 <classPathEntry>元素是<generatorConfiguration>元素的选项子元素。 MBG在这些实例中从这些位置加载类:
	加载JDBC驱动程序以进行数据库自省时
	在JavaModelGenerator中加载根类以检查重写方法时
	此元素是可选的,如果在MBG外部设置类路径(例如,使用java命令的-cp参数),则不需要此元素
  -->
  <!-- <classPathEntry location="mysql-connector-java-8.0.11.jar" /> -->
  <!-- <classPathEntry location="src\main\java\mybatis\generator" /> -->

  <!-- The <context> element is used to specify the environment for generating a set of objects. Child elements are used to specify the database to connect to, the type of objects to		     generate, and the tables to introspect. Multiple <context> elements can be listed inside an <generatorConfiguration> element to allow generating objects from different databases, or		  with different generation parameters, in the same run of MyBatis Generator (MBG). 
	targetRuntime:
	This is the default value 
	With the value, MBG will generate objects that are compatible with MyBatis versions 3.0 and higher, and JSE 5.0 and higher (e.g. the Java model and mapper interfaces will use generic types). The "by example" methods in these generated objects support virtually unlimited dynamic where clauses. Additionally, the Java objects generated with these generators support many JSE 5.0 features including parameterized types and annotations.
  -->
  <!-- 
	<context>元素用于指定生成一组对象的环境。 子元素用于指定要连接的数据库,要生成的对象类型以及要内省的表。 可以在<generatorConfiguration>元素内列出多个<context>元素,以允许在MyBatis	Generator(MBG)的同一运行中生成来自不同数据库或具有不同生成参数的对象。
	id : A unique identifier for this context. This value will be used in some error messages. 
	targetRuntime:
	这是默认值
	使用该值,MBG将生成与MyBatis 3.0及更高版本以及JSE 5.0及更高版本兼容的对象(例如,Java模型和映射器接口将使用泛型类型)。 这些生成的对象中的“by example”方法支持几乎无限的动态where子句。 此外,使用这些生成器生成的Java对象支持许多JSE 5.0功能,包括参数化类型和注释。
  -->
  
  <context id="DB2Tables" targetRuntime="MyBatis3" defaultModelType="flat">
    <property name="beginningDelimiter" value="`"/>
    <property name="endingDelimiter" value="`"/>
    <property name="javaFileEncoding" value="GBK"/>
    <!-- 这里的type里写的是你的实现类的类全路径 -->
    <!-- 使用maven插件来运行mybatis-generator,需要把这些你自定义的这些类放到把成jar包,并且在mybatis-generator-maven-plugin插件中添加相关依赖 -->
    <commentGenerator type="mybatis.generator.Comments">
      <property name="suppressDate" value="false" /> 
    </commentGenerator>
  	
	<!--
		The <jdbcConnection> element is used to specify the properties of the database connection required to introspect tables. MyBatis Generator uses JDBC's DatabaseMetaData class to discover the properties of the tables you specify in the configuration. One <connectionFactory> or <jdbcConnection> 
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值