一、在maven中經過properties節點指定MyBatis Generator配置文件的位置java
<properties> <spring.version>4.1.1.RELEASE</spring.version> <!--mybatis-generate配置文件位置--> <mybatis.generator.configurationFile> ${basedir}/src/main/resources/generatorConfig.xml </mybatis.generator.configurationFile> </properties>
二、添加Maven插件並設置插件屬性mysql
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.0</version> <!--configuration設置插件屬性--> <configuration> <!--configurationFile指定配置文件位置--> <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile> </configuration> <!--在maven編譯階段自動生產dao/mapper/bean--> <!--<executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </executions>--> <!--<configurationFile>${mybatis.generator.configurationFile}</configurationFile>--> </plugin>
三、對配置文件進行配置(一個實例)spring
<?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:\Program Files\repository\mysql\mysql-connector-java\5.1.25\mysql-connector-java-5.1.25.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- 數據庫鏈接地址賬號密碼 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/shenyidu" userId="root" password="123"/> <!-- 生成model類文件存放位置 --><!--targetPackage爲package名,targetProject爲工程目錄--> <javaModelGenerator targetPackage="shop.model" targetProject="src\main\java\"/> <!-- 生成映射文件存放位置 --> <sqlMapGenerator targetPackage="shop" targetProject="src\main\resources\mapper"/> <!-- 生成dao類存放位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="shop.dao" targetProject="src\main\java\"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 對應的數據表和實體類 --> <table tableName="t_account" domainObjectName="Account" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/> <table tableName="t_canteens" domainObjectName="Canteen" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/> <table tableName="t_comments" domainObjectName="Comment" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/> <table tableName="t_customers" domainObjectName="Customer" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/> </context> </generatorConfiguration>
點擊運行:sql