Mybatis Generator 配置详解:https://www.imooc.com/article/21444
Mybatis Generator官网文档:http://mybatis.org/generator/running/runningWithMaven.html。
重点关注官网文档中的:Running Mybatis Generator-with Maven的配置,以及XML Configuration Reference的内容。
一、摘要
Mybatis-Generator可以帮助我们自动生成很多结构化的代码,比如每张表对应的Entity、Mapper接口和Xml文件,可以省去很多繁琐的工作。所以重点是帮助自动生成,这样我们可以少些很多代码,同时自动生成的代码能够满足我们的基本需要,当然,自己手写entity和dao以及mappers也是可以的,用在实现一些复杂的业务逻辑时。
二、使用方法
1.porm.xml中添加对应插件
需要在porm.xml的最下面的plugins里添加mybatis-generator对应的插件。
<project ...>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
</plugin>
...
</plugins>
...
</build>
...
</project>
版本号根据实际情况来选择。
2.配置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\DJ\Desktop\upload\mysql-connector-java-8.0.18.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- 不再文件中追加xml-->
<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"></plugin>
<commentGenerator>
<!-- 生成文件时不会添加注释-->
<property name="suppressAllComments" value="true"></property>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/mall?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=UTF-8"
userId="root"
password="123456dj">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="com.dj.mall.pojo" targetProject="src\main\java">
<property name="enableSubPackages" value="true" />
<!-- 去掉内容的空格-->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="mappers" targetProject="src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.dj.mall.dao" targetProject="src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="mall_order" domainObjectName="Order" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"></table>
<table tableName="mall_order_item" domainObjectName="OrderItem" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"></table>
</context>
</generatorConfiguration>
下面我们呢依次来解释下里面模块的内容:
<classPathEntry location="C:\Users\DJ\Desktop\upload\mysql-connector-java-8.0.18.jar" />
表示我们的数据库驱动的路径,这里我们用的时mysql,所以去下载mysql的jar文件,然后随便放置在一个位置,然后在location的属性里写出它的绝对地址即可。<context id="DB2Tables" targetRuntime="MyBatis3">
这是mybatis-generatore的一些配置的写入位置,里面写了相关的配置信息。
需要注意的是:在context里可以写入的模块有上面图片中的内容,但是请一定要按照上述的顺序来排列对应模块。
3. <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"></plugin>
这个配置表示当重新生成一边entiy,mapper等文件时覆盖当前文件里的内容,而不是追加在尾部,这样每一个文件的内容就只有一份,重新生成时不会重复内容。
4. 下面是在<commentGenerator>
里的内容
<commentGenerator>
<!-- 生成文件时不会添加注释-->
<property name="suppressAllComments" value="true"></property>
</commentGenerator>
用来让生成文件的注释去掉,这样看起来更加简洁些。
5. <jdbcConnection
这个是用来配置连接数据库的一些东西。包括驱动,数据库的url,用户,密码等。
6. <javaModelGenerator targetPackage="com.dj.mall.pojo" targetProject="src\main\java">
这个配置项是用来标识自动生成的entity(实体类,这里我起名叫pojo-president object Java object)的位置,前一个标识项目名,后一个标识绝对位置。
7. <sqlMapGenerator targetPackage="mappers" targetProject="src\main\resources">
同理,标识生成的mapper.xml的文件的位置。
8. <javaClientGenerator type="XMLMAPPER" targetPackage="com.dj.mall.dao" targetProject="src\main\java">
用来标识生成的dao文件的位置。
9. <table tableName="mall_order" domainObjectName="Order" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"></table>
表示自动生成哪个表的配置文件,这里对应mysql数据库中的mall_order表,其中domainObjectName=“Order” 表示我希望对应表生成的所有文件都以Order开头,省去了表名中的mall,enableCountByExample="false"等几个表示把dao中的几个无用的example方法在生成dao文件时不要添加进去。
10. 还有一点需要注意的是,需要在porm.xml中的mybatis-generator插件配置处添加
<configuration>
<overwrite>true</overwrite>
</configuration>
它的作用是在我们重新生成对应的dao,mapper等文件时,不会重新去新建一个新文件,而是向原有的文件里去添加,配合3一起食用更加。
3.生成entity,dao,mapper
我这里是idea,只需要在终端里执行命令,idea里的terminal就是终端,打开它就行,同时因为我是maven项目,所以用命令:mvn mybatis-generator:generate
即可。
最后会生成对应表的实体类,dao,以及mapper。
这里提一点:我们可以在idea里下载lombok和mybatis-plugin这两个插件,lombok是帮助我们省去enitty中的get和set方法,安装该插件后只需要在实体类上加上@data标记即可;而mybatis-plugin插件则是帮助我们快速管理dao和mapper文件的,安装后,在dao和mapper中的方法对应位置有小箭头,可以帮助我们快速查找其在mapper或者是dao中的位置。