使用maven和mybatis自动生成代码配置

使用自动生成真的很方便,之前试过手写,但是无奈太慢而且容易出错,一急之下马上去 研究了下,没想到2个小时不到就研究出来了,废话不多说,我们正式开始,

如果需要生成数据库字段的注释,请下载:点我打开 链接内的文件,里面有详细教程

    准备工作:

          1、下载 mysql-connector-java-5.0.8-bin.jar 驱动包,点我下载,如果没有分数的话建议去官网或者百度找,很多的,因为我这里用的是mysql数据库,所以我下载的mysql 的驱动包, 其他数据库自行下载对于的驱动包

          2、maven环境的项目

 一、 一般情况下,我们的项目都是模块化编程的,我的mybatis的数据层就放在common层里面如下图,在 src/main/resources 目录源下创建一个 generatorConfig.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>
	<!--数据库驱动 ,这里你们需要自行修改自己的jar包路径-->
	<classPathEntry
		location="H:/repository/mysql-connector-java-5.0.8-bin.jar" />
	<context id="DB2Tables" targetRuntime="MyBatis3">
		<commentGenerator>
			<property name="suppressDate" value="true" />
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<!--数据库链接地址账号密码 ,这里你们需要自己修改-->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://xxx.xxx.xxx.xxx/xd_love" userId="root"
			password="123456">
		</jdbcConnection>
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>
		<!--生成Model类存放位置 -->
		<javaModelGenerator targetPackage="com.maven.common.model"
			targetProject="src/main/java">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
		<!--生成映射文件存放位置 -->
		<sqlMapGenerator targetPackage="sqlMapper"
			targetProject="src/main/resources">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>
		<!--生成Dao类存放位置 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.maven.common.mapper" targetProject="src/main/java">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		<!--生成对应表及类名 tableName对应你的表名,domainObjectName对应你生成的文件前缀名-->
		<table tableName="symphony_address" domainObjectName="symphonyAddress"
			enableCountByExample="false" enableUpdateByExample="false"
			enableDeleteByExample="false" enableSelectByExample="false"
			selectByExampleQueryId="false"></table>
	</context>
</generatorConfiguration>

三、在pom.xml文件中添加插件

<build>
	<finalName><!--你的项目名称--></finalName>
	<pluginManagement>
		<plugins>
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.2</version>
				<configuration>
					<configurationFile>src/main/resources/generatorConfig.xml</configurationFile><!--配置文件路径 -->
					<verbose>true</verbose>
					<overwrite>true</overwrite>
				</configuration>
				<executions>
					<execution>
						<id>Generate MyBatis Artifacts</id>
						<goals>
							<goal>generate</goal>
						</goals>
					</execution>
				</executions>
				<dependencies>
					<dependency>
						<groupId>org.mybatis.generator</groupId>
						<artifactId>mybatis-generator-core</artifactId>
						<version>1.3.2</version>
					</dependency>
				</dependencies>
			</plugin>
		</plugins>
	</pluginManagement>
</build>

四、右击项目-->Run As-->Maven Build

在Goals一栏输入:mybatis-generator:generate -e,然后点击Run之后就会自动生成代码了

五、生成代码后的目录

-------------------------------------我是分割线-------------------------------------------------------

后续:如果出现了错误Project build error: Non-resolvable parent POM for com.maven:xxx-common:0.0.1-SNAPSHOT: Could not find artifact com.maven:xxx-parent:pom:0.0.1-  SNAPSHOT and 'parent.relativePath' points at wrong local POM

出现这个错误是因为父工程没有注册,右键parent 项目 -run as - maven install  即可解决

猜你喜欢

转载自blog.csdn.net/qq_27184497/article/details/81139981