目前大多插件總是被和諧,為了生成entity、dao、和映射xml方便,我們在項目中自己添加一個文件,按照實際需求生成需要的文件
1.首先在項目中創建generatorConfiguration.xml
我這是在已有項目的基礎上,也可以做一個空的框架,用來專門生成文件,我放在了resource下。
xml代碼如下,由於mysql版本是8,所以驅動包要下載對應的,這個可以pom.xml文件中配置,然后從maven取,也可以在網上找資源。
<?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="D:\Work\Tools\invoke\mysql-connector-java-8.0.11.jar"/> <context id="mysql" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <!-- 是否去除自動生成的注釋 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--數據庫鏈接URL,用戶名、密碼 --> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/sm?&useSSL=false&useUnicode=true&characterEncoding=utf-8&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai" userId="root" password="123456"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成模型的包名和位置--> <javaModelGenerator targetPackage="com.manage.entity" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成映射文件的包名和位置--> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成DAO的包名和位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.manage.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 要生成的表 tableName是數據庫中的表名或視圖名 domainObjectName是實體類名--> <table schema="" tableName="SysUser" domainObjectName="SysUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration>
2.在pom.xml添加依賴,加上如下配置
注意:要和pluginManagement同級,要不然maven窗口找不到這個插件,為方便參考,代碼如下:
<plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.1</version> <configuration> <configurationFile>${basedir}/src/main/resources/generator/generatorConfiguration.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> </plugin> </plugins>
創建好了之后我們右鍵項目,選擇【Maven】-->【Reload Project】,這樣在右面的Maven窗口就有了這個插件
需要用的包為:mybatis-generator-maven-plugin-1.3.1.jar,如果沒有可以點擊此鏈接下載
mybatis-generator-maven-plugin-1.3.1.jar
3.運行插件
選中插件右鍵選擇第二個運行,控制台輸出成功信息,然后我們的項目中就有生成好的文件了
至此,插件創建完成,快去試試手吧