Eclipse中关于mybatis插件geneartor的使用和遇到的一些问题

安装

步骤一:点击上方help

在这里插入图片描述

步骤二:搜索框搜索插件名字,然后按回车或者点击放大镜

搜索框在这里插入图片描述

步骤三:点击installeo安装

在这里插入图片描述

使用

步骤一:在pom文件中添加插件配置

<plugin>
	<groupId>org.mybatis.generator</groupId>
	<artifactId>mybatis-generator-maven-plugin</artifactId>
	<version>1.4.0</version>
	<configuration>
		<verbose>true</verbose>
		<overwrite>true</overwrite>
	</configuration>
</plugin>

步骤二:创建或者生成generatorConfig.xml文件

对应目录下鼠标右键选择new other输入关键字搜索
在这里插入图片描述

步骤三:修改模板

// 
<generatorConfiguration>

	<!--导入属性配置 -->
	<properties resource="datasource.properties"></properties>

	<!--指定特定数据库的jdbc驱动jar包的位置 -->
	<classPathEntry location="${db.driverLocation}" />

	<context id="default" targetRuntime="MyBatis3">

		<!-- optional,旨在创建class时,对注释进行控制 -->
		<commentGenerator>
			<property name="suppressDate" value="true" />
			<property name="suppressAllComments" value="true" />
		</commentGenerator>

		<!--jdbc的数据库连接 -->
		<jdbcConnection driverClass="${db.driverClassName}"
			connectionURL="${db.url}" userId="${db.username}"
			password="${db.password}">
		</jdbcConnection>


		<!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制 -->
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类 targetPackage 指定生成的model生成所在的包名 
			targetProject 指定在该项目下所在的路径 -->
		<javaModelGenerator targetPackage="com.xxx.pojo"
			targetProject="xxx/src/main/java">
			<!-- 是否允许子包,即targetPackage.schemaName.tableName -->
			<property name="enableSubPackages" value="false" />
			<!-- 是否对model添加 构造函数 -->
			<property name="constructorBased" value="true" />
			<!-- 是否对类CHAR类型的列的数据进行trim操作 -->
			<property name="trimStrings" value="true" />
			<!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 -->
			<property name="immutable" value="false" />
		</javaModelGenerator>

		<!--mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
		<!--<sqlMapGenerator targetPackage="mappers" targetProject="xxx\src\main\resources"> -->
		<sqlMapGenerator targetPackage="mappers"
			targetProject="xxx\src\main\java">
			<property name="enableSubPackages" value="false" />
		</sqlMapGenerator>


		<!-- targetPackage:mapper接口dao生成的位置 -->
		<!--<javaClientGenerator type="XMLMAPPER" targetPackage="com.xxx.dao" 
			targetProject="xxx\src\main\java"> -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.xxx.dao" targetProject="xxx/src/main/java">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
		</javaClientGenerator>



		<table tableName="表名" domainObjectName="类名"
			enableCountByExample="false" enableUpdateByExample="false"
			enableDeleteByExample="false" enableSelectByExample="false"
			selectByExampleQueryId="false"></table>
		<table tableName="表名" domainObjectName="类名"
			enableCountByExample="false" enableUpdateByExample="false"
			enableDeleteByExample="false" enableSelectByExample="false"
			selectByExampleQueryId="false"></table>
		...
		...

	</context>
</generatorConfiguration>

步骤四:运行generatorConfig.xml文件

右键点击generatorConfig.xml文件
在这里插入图片描述

可能出现的问题

一:Project 没找到

当出现Project 没找到问题时,需要将targetProject="./src/main/java"由相对路径改为targetProject="项目名/src/main/java"绝对路径。

二:路径不正确

请注意mysql jar包路径是否正确,例如:本地路径,
driverLocation=I:/Develop/xxx/src/main/webapp/WEB-INF/lib/mysql-connector-java-5.1.6-bin.jar
特别注意斜杠不要搞反了,否则可能会路径不正确生成失败

三:mysql jar包版本与本机安装的mysql版本差别太大导致的问题

低版本的jar包链接的driverClass为:com.mysql.jdbc.Driver

高版本的jar包链接的driverClass为:com.mysql.cj.jdbc.Driver

电脑上的mysql版本太高,你用的数据库连接jar包太低,会出现authentication plugin ‘caching_sha2_password’

四:数据库时间与系统时间不符

出现The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

解决方法:
在jdbcConnection下的connectionURL这样写:
jdbc:mysql://127.0.0.1/cigit2019?serverTimezone=GMT
这样加个serverTimezone=GMT

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值