用mybatis的generator自动生成代码--坑我都走了一遍,后面的同学别踩了

https://blog.csdn.net/j18423532754/article/details/86488448

先说什么是mybatis-generator?

mybatis-geneator是一款mybatis自动代码生成工具,可以通过配置,快速生成mapper和xml文件。

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


 
 
  1. <plugin>
  2. <groupId>org.mybatis.generator </groupId>
  3. <artifactId>mybatis-generator-maven-plugin </artifactId>
  4. <version>1.3.2 </version>
  5. <configuration>
  6. <verbose>true </verbose>
  7. <overwrite>true </overwrite>
  8. </configuration>
  9. <executions>
  10. <execution>
  11. <id>Generate MyBatis Artifacts </id>
  12. <goals>
  13. <goal>generate </goal>
  14. </goals>
  15. </execution>
  16. </executions>
  17. <dependencies>
  18. <dependency>
  19. <groupId>org.mybatis.generator </groupId>
  20. <artifactId>mybatis-generator-core </artifactId>
  21. <version>1.3.2 </version>
  22. </dependency>
  23. </dependencies>
  24. </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文件


 
 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration
  3. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  5. <generatorConfiguration>
  6. <!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
  7. <classPathEntry location="C:\Users\Administrator\Downloads\mysql-connector-java-8.0.13.jar"/>
  8. <context id="DB2Tables" targetRuntime="MyBatis3">
  9. <commentGenerator>
  10. <property name="suppressDate" value="true"/>
  11. <!-- 是否去除自动生成的注释 true:是 : false:否 -->
  12. <property name="suppressAllComments" value="true"/>
  13. </commentGenerator>
  14. <!--数据库链接URL,用户名、密码
  15. 1.一般jdbc数据库的版本6.x以上,都是com.mysql.cj.jdbc.Driver 其他的低版本就是com.mysql.cj.jdbc.Driver
  16. -->
  17. <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/cigit2019?serverTimezone=GMT" userId="root" password="1234">
  18. </jdbcConnection>
  19. <javaTypeResolver>
  20. <property name="forceBigDecimals" value="false"/>
  21. </javaTypeResolver>
  22. <!-- 生成模型的包名和位置-->
  23. <javaModelGenerator targetPackage="com.example.cigit.dao" targetProject="src/main/java">
  24. <property name="enableSubPackages" value="true"/>
  25. <property name="trimStrings" value="true"/>
  26. </javaModelGenerator>
  27. <!-- 生成映射文件的包名和位置-->
  28. <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
  29. <property name="enableSubPackages" value="true"/>
  30. </sqlMapGenerator>
  31. <!-- 生成DAO的包名和位置-->
  32. <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.cigit.mapping" targetProject="src/main/java">
  33. <property name="enableSubPackages" value="true"/>
  34. </javaClientGenerator>
  35. <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
  36. <table tableName="t_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table>
  37. </context>
  38. </generatorConfiguration>

这是模板,你要修改的地方

  1. classPathEntry---数据库驱动:选择你的本地硬盘上面的数据库驱动包,具体可以在https://mvnrepository.com/artifact/mysql/mysql-connector-java下载
  2. jdbcConnection---数据库连接的信息,用户名密码等
  3. javaModelGenerator---生成模型的包名和位置
  4. sqlMapGenerator---生成生成映射文件的包名和位置
  5. javaClientGenerator---生成DAO的包名和位置
  6. 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="{&quot;mod&quot;:&quot;popu_824&quot;}"><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="{&quot;mod&quot;:&quot;1582594662_002&quot;}"><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="{&quot;mod&quot;:&quot;popu_379&quot;}" 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="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                    </div>
                            </div>
                    </div>
    </article>
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hello_world!

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值