欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mybatis-generator使用Maven Plugin管理和生成代码

程序员文章站 2022-05-29 16:51:01
...
之前用批处理文件.bat来执行命令生成mybatis的相关文件,但是假如用Mac或者Linux,还得改配置文件,那么灵活一点,通过Maven安装插件,来管理jar和执行命令

pom.xml
<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.2</version>
				<executions>
					<execution>
						<id>Generate MyBatis Artifacts</id>
						<goals>
							<goal>generate</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<verbose>true</verbose>
					<overwrite>true</overwrite>
					<!-- <jdbcDriver>com.mysql.jdbc.Driver</jdbcDriver> -->
					<!-- <jdbcURL>jdbc:mysql://127.0.0.1:3306/test</jdbcURL> -->
					<!-- <jdbcUserId>test</jdbcUserId> -->
					<!-- <jdbcPassword>test</jdbcPassword> -->
				</configuration>
				<dependencies>
					<dependency>
						<groupId>mysql</groupId>
						<artifactId>mysql-connector-java</artifactId>
						<version>5.1.6</version>
					</dependency>
					<dependency>
						<groupId>org.mybatis.generator</groupId>
						<artifactId>mybatis-generator-core</artifactId>
						<version>1.3.2</version>
					</dependency>
					<dependency>
						<groupId>org.mybatis</groupId>
						<artifactId>mybatis</artifactId>
						<version>3.2.2</version>
					</dependency>
				</dependencies>
			</plugin>


然后generatorConfig.xml网上有的是,就不贴了

重点注意以下几点
如果不再plugin里面添加依赖包得引用的话,会找不到相关得jar包,在plugin外部得jar包,他不会去找到并执行,所以要把plugin运行依赖得jar配置都放在里面
另外generatorConfig.xml里面得targetProject,可以写成src/main/java,也就是Maven管理得编译代码路径,我之前用generator插件右键生成可以写项目名,但是在这里就不好用了。
generatorConfig.xml如果不写路径的话,默认是读取src/main/resource下面得

具体更多配置可参看官方文档
http://mybatis.org/generator/running/runningWithMaven.html
http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/core/mybatis-generator-systests-mybatis3/pom.xml

关于M2E 的Eclipse插件,目前还不支持execution
报如下错误
Plugin execution not covered by lifecycle configuration
官方文档给出的如下
http://wiki.eclipse.org/M2E_plugin_execution_not_covered
这里注意添加如下代码,要与plugins级别并列添加
<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.eclipse.m2e</groupId>
					<artifactId>lifecycle-mapping</artifactId>
					<version>1.0.0</version>
					<configuration>
						<lifecycleMappingMetadata>
							<pluginExecutions>
								<!-- copy-dependency plugin -->
								<pluginExecution>
									<pluginExecutionFilter>
										<groupId>org.apache.maven.plugins</groupId>
										<artifactId>maven-dependency-plugin</artifactId>
										<versionRange>[1.0.0,)</versionRange>
										<goals>
											<goal>copy-dependencies</goal>
										</goals>
									</pluginExecutionFilter>
									<action>
										<ignore />
									</action>
								</pluginExecution>

								<!-- mybatis-generator-plugin -->
								<pluginExecution>
									<pluginExecutionFilter>
										<groupId>org.mybatis.generator</groupId>
										<artifactId>mybatis-generator-maven-plugin</artifactId>
										<versionRange>[1.3.2,)</versionRange>
										<goals>
											<goal>generate</goal>
										</goals>
									</pluginExecutionFilter>
									<action>
										<ignore />
									</action>
								</pluginExecution>
							</pluginExecutions>
						</lifecycleMappingMetadata>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>