MyBatis Generator 使用

一、简介

1.1 MyBatis Generator介绍

MyBatis Generator 是MyBatis 官方出品的一款,用来自动生成MyBatis的 mapper、dao、entity 的框架,让我们省去规律性最强的一部分最基础的代码编写。

1.2 MyBatis Generator使用

MyBatis Generator的使用方式有4种:

  • 命令行生成
  • Maven方式生成
  • 使用Ant任务生成
  • 使用Java代码生成

其中推荐使用Maven方式进行代码生成,因为集成和使用比较简单。

<!--more-->

1.3 开发环境

MySQL:8.0.12

MyBatis Generator:1.3.7

Maven:4.0

IDEA:2018.2

二、代码自动生成配置

上面介绍了使用MyBatis Generator的几种方式,其中最推荐使用的是Maven方式,所以下面我们来看Maven方式的MyBatis代码生成,分为四步:

Step1:添加依赖

配置pom.xml文件,增加依赖和配置生成文件(“generatorConfig.xml”)路径:


 
 
  1. <plugin>
  2. <groupId>org.mybatis.generator </groupId>
  3. <artifactId>mybatis-generator-maven-plugin </artifactId>
  4. <version>1.3.7 </version>
  5. <dependencies>
  6. <dependency>
  7. <groupId>mysql </groupId>
  8. <artifactId>mysql-connector-java </artifactId>
  9. <version>8.0.12 </version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.mybatis.generator </groupId>
  13. <artifactId>mybatis-generator-core </artifactId>
  14. <version>1.3.7 </version>
  15. </dependency>
  16. </dependencies>
  17. <executions>
  18. <execution>
  19. <id>Generate MyBatis Artifacts </id>
  20. <phase>package </phase>
  21. <goals>
  22. <goal>generate </goal>
  23. </goals>
  24. </execution>
  25. </executions>
  26. <configuration>
  27. <!--允许移动生成的文件 -->
  28. <verbose>true </verbose>
  29. <!-- 是否覆盖 -->
  30. <overwrite>true </overwrite>
  31. <!-- 自动生成的配置 -->
  32. <configurationFile>generatorConfig.xml </configurationFile>
  33. </configuration>
  34. </plugin>

Step2:添加配置文件

根据上面在pom里的配置,我们需要添加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. <properties resource="application.properties"/>
  8. <!--defaultModelType="flat" 大数据字段,不分表 -->
  9. <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
  10. <property name="autoDelimitKeywords" value="true" />
  11. <property name="beginningDelimiter" value="`" />
  12. <property name="endingDelimiter" value="`" />
  13. <property name="javaFileEncoding" value="utf-8" />
  14. <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
  15. <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
  16. <!-- 注释 -->
  17. <commentGenerator >
  18. <property name="suppressAllComments" value="true"/> <!-- 是否取消注释 -->
  19. <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳-->
  20. </commentGenerator>
  21. <!--数据库链接地址账号密码-->
  22. <jdbcConnection driverClass="${spring.datasource.driver-class-name}"
  23. connectionURL= "${spring.datasource.url}"
  24. userId= "${spring.datasource.username}"
  25. password= "${spring.datasource.password}">
  26. </jdbcConnection>
  27. <!-- 类型转换 -->
  28. <javaTypeResolver>
  29. <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
  30. <property name="forceBigDecimals" value="false"/>
  31. </javaTypeResolver>
  32. <!--生成Model类存放位置-->
  33. <javaModelGenerator targetPackage="com.hello.springboot.entity" targetProject="src/main/java">
  34. <property name="enableSubPackages" value="true"/>
  35. <property name="trimStrings" value="true"/>
  36. </javaModelGenerator>
  37. <!-- 生成mapxml文件 -->
  38. <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources/mybatis" >
  39. <property name="enableSubPackages" value="false" />
  40. </sqlMapGenerator>
  41. <!-- 生成mapxml对应client,也就是接口dao -->
  42. <javaClientGenerator targetPackage="com.hello.springboot.dao" targetProject="src/main/java" type="XMLMAPPER" >
  43. <property name="enableSubPackages" value="false" />
  44. </javaClientGenerator>
  45. <table tableName="article" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">
  46. <generatedKey column="id" sqlStatement="Mysql" identity="true" />
  47. </table>
  48. <table tableName="user_log" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true">
  49. <generatedKey column="id" sqlStatement="Mysql" identity="true" />
  50. </table>
  51. </context>
  52. </generatorConfiguration>

其中数据库连接的配置,是从application.properties直接读取的。

Step3:配置全局属性文件

全局属性文件application.properties的配置,和Spring Boot增加MyBatis的配置是一样的,如果你的Spring Boot项目里面已经配置了MyBatis支持,请忽略此步骤。


 
 
  1. # MyBatis 配置
  2. spring.datasource.url=jdbc:mysql://172.16.10.79:3306/mytestdb?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true
  3. spring.datasource.username=root
  4. spring.datasource.password=123456
  5. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  6. mybatis.type-aliases-package=com.hello.springboot.mapper
  7. mybatis.config-locations=classpath:mybatis/mybatis-config.xml
  8. mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

注意: MySQL 6以后JDBC的配置就不一样了,参照如上MySQL 8的配置。

Step4:点击Maven生成代码

如果你使用的是IDEA,点击最右侧的Maven Projects => 点击mybatis-generator => 右键mybatis-generator:generate => Run Maven Build,如下图所示:

正常控制台输出“BUILD SUCCESS”说明生成已经成功了,如果出现错误,根据错误提示信息排除处理错误即可。

MyBatis Generator 示例源码:https://github.com/vipstone/s...

三、安装IDEA插件

如果你使用的是 IDEA,那么强烈建议你安装一款免费的IDEA插件“Free MyBatis plugin”,可以实现dao到mapper xml对应方法的快速映射,点击任意一个快速调整到相应的方法,提高工作效率,效果如下图所示:

点击绿色的箭头直接跳转到了mapper xml对应的方法了,如下图所示:

可以相互点击,进行对应的跳转。

安装步骤

  • 点击菜单栏Flie => Settings
  • 点击Browse repostitories..
  • 输入“Free MyBatis plugin”查找插件
  • 点击安装,重启IDEA

关键步骤的截图如下:

四、总结

使用了MyBatis Generator可以帮我们自动生成实体类,和5个最基础的方法,大大的提高我们的工作效率,用户只需要按需写自己独有的一些业务即可。同时增加“Free MyBatis plugin”插件,可以很方便的帮我们开发和调试代码,真是实实在在的福利。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值