https://blog.csdn.net/j18423532754/article/details/86488448
先说什么是mybatis-generator?
mybatis-geneator是一款mybatis自动代码生成工具,可以通过配置,快速生成mapper和xml文件。
步骤一:在pom文件中添加插件配置
-
<plugin>
-
<groupId>org.mybatis.generator
</groupId>
-
<artifactId>mybatis-generator-maven-plugin
</artifactId>
-
<version>1.3.2
</version>
-
<configuration>
-
<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>
在resource目录下创建generatorConfig.xml文件
如果你先为了整洁美观,想把generatorConfig.xml放在generator文件下,在插件申明一下configurationFile
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
步骤二:编写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>
-
<!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
-
<classPathEntry location="C:\Users\Administrator\Downloads\mysql-connector-java-8.0.13.jar"/>
-
<context id="DB2Tables" targetRuntime="MyBatis3">
-
<commentGenerator>
-
<property name="suppressDate" value="true"/>
-
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
-
<property name="suppressAllComments" value="true"/>
-
</commentGenerator>
-
<!--数据库链接URL,用户名、密码
-
1.一般jdbc数据库的版本6.x以上,都是com.mysql.cj.jdbc.Driver 其他的低版本就是com.mysql.cj.jdbc.Driver
-
-->
-
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/cigit2019?serverTimezone=GMT" userId="root" password="1234">
-
</jdbcConnection>
-
<javaTypeResolver>
-
<property name="forceBigDecimals" value="false"/>
-
</javaTypeResolver>
-
<!-- 生成模型的包名和位置-->
-
<javaModelGenerator targetPackage="com.example.cigit.dao" targetProject="src/main/java">
-
<property name="enableSubPackages" value="true"/>
-
<property name="trimStrings" value="true"/>
-
</javaModelGenerator>
-
<!-- 生成映射文件的包名和位置-->
-
<sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
-
<property name="enableSubPackages" value="true"/>
-
</sqlMapGenerator>
-
<!-- 生成DAO的包名和位置-->
-
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.cigit.mapping" targetProject="src/main/java">
-
<property name="enableSubPackages" value="true"/>
-
</javaClientGenerator>
-
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
-
<table tableName="t_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table>
-
</context>
-
</generatorConfiguration>
这是模板,你要修改的地方
-
classPathEntry---数据库驱动:选择你的本地硬盘上面的数据库驱动包,具体可以在https://mvnrepository.com/artifact/mysql/mysql-connector-java下载
-
jdbcConnection---数据库连接的信息,用户名密码等
-
javaModelGenerator---生成模型的包名和位置
-
sqlMapGenerator---生成生成映射文件的包名和位置
-
javaClientGenerator---生成DAO的包名和位置
-
table--- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名
运行:IDEA的运行方法
点击右侧的Maven projects即可
网上关于mybatis的generator自动生成代码教程很多,现在我来说说我写mybatis的generator时候遇到最坑的
1.电脑上的mysql版本太高,你用的数据库连接jar包太低,会出现authentication plugin 'caching_sha2_password'
这是由于数据库connector jar包与你的数据库版本不一致导致的,这是只要修改你的数据库jar包即可。换个高点的jar包,比如mysql-connector-java-8.0.13.jar---8.0.13的版本或者6.x的版本
2.出现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
3.运行不起,一定要检查你要generator为你生成的文件的路径是否正确和存在。
4.电脑上的数据库版本太高就用高版本的jar包,低了就用低版本的jar包
低版本的jar包链接的driverClass为:com.mysql.jdbc.Driver
高版本的jar包链接的driverClass为:com.mysql.cj.jdbc.Driver
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true"> <use xlink:href="#csdnc-thumbsup"></use> </svg><span class="name">点赞</span> <span class="count">2</span> </a></li> <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true"> <use xlink:href="#icon-csdnc-Collection-G"></use> </svg><span class="name">收藏</span></a></li> <li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{"mod":"1582594662_002"}"><svg class="icon" aria-hidden="true"> <use xlink:href="#icon-csdnc-fenxiang"></use> </svg>分享</a></li> <!--打赏开始--> <!--打赏结束--> <li class="tool-item tool-more"> <a> <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg> </a> <ul class="more-box"> <li class="item"><a class="article-report">文章举报</a></li> </ul> </li> </ul> </div> </div> <div class="person-messagebox"> <div class="left-message"><a href="https://blog.csdn.net/j18423532754"> <img src="https://profile.csdnimg.cn/A/3/6/3_j18423532754" class="avatar_pic" username="j18423532754"> <img src="https://g.csdnimg.cn/static/user-reg-year/1x/3.png" class="user-years"> </a></div> <div class="middle-message"> <div class="title"><span class="tit"><a href="https://blog.csdn.net/j18423532754" data-report-click="{"mod":"popu_379"}" target="_blank">超厂长</a></span> </div> <div class="text"><span>发布了14 篇原创文章</span> · <span>获赞 22</span> · <span>访问量 5万+</span></div> </div> <div class="right-message"> <a href="https://im.csdn.net/im/main.html?userName=j18423532754" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信 </a> <a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379"}">关注</a> </div> </div> </div> </article>