你的位置:首页 > Java教程

[Java教程]Maven多模块项目使用MyBatis Generator


开发环境

  JDK:8u102

  Maven:3.3.9

  MySQL:5.7.10

  MySQL Connector:5.1.40

  IDE:IntelliJ IDEA 2016

  MyBatis:3.4.1

  MyBatis Generator:1.3.5

 

项目案例:

  个人博客

 

项目结构:

  

 

   blog-dao-impl模块:只是考虑到多个实现层而已。象征性的创建在这里。

 

项目依赖关系:

  

 

数据库详细:

  

 

blog-dao-api pom.

 1 <??> 2 <project ="http://maven.apache.org/POM/4.0.0" 3      ="http://www.w3.org/2001/ 4      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5   <parent> 6     <artifactId>blog</artifactId> 7     <groupId>pers.kaloquan</groupId> 8     <version>1.0-SNAPSHOT</version> 9   </parent>10   <modelVersion>4.0.0</modelVersion>11 12   <artifactId>blog-dao-api</artifactId>13 14   <properties>15     <!-- MySQL Connector 版本 -->16     <mysql-connector.version>5.1.40</mysql-connector.version>17     <!-- MyBatis Generator 版本 -->18     <mybatis-generator.version>1.3.5</mybatis-generator.version>19 20     <!-- 指定MyBatis Generator插件的配置文件 -->21     <plugin.generator.configurationFile>${project.basedir}\src\main\resources\mybatis-generator.</plugin.generator.configurationFile>22     <!-- 是否重写已存在的文件 -->23     <plugin.generator.overwrite>true</plugin.generator.overwrite>24     <!-- 生成器targetProject的根目录 -->25     <generator.project.root>D:\IDEAProjects\blog</generator.project.root>26     <!-- 生成器所需驱动包的路径 -->27     <generator.classpath>D:\Maven repository\mysql\mysql-connector-java\5.1.40\mysql-connector-java-5.1.40.jar</generator.classpath>28 29     <!-- 驱动类 -->30     <jdbc.driverClass>com.mysql.jdbc.Driver</jdbc.driverClass>31     <!-- 链接地址 -->32     <jdbc.url>jdbc:mysql://localhost:3306/blog?createDatabaseIfNotExist=true&amp;useSSL=true&amp;serverTimezone=UTC&amp;passwordCharacterEncoding=UTF-8&amp;characterEncoding=UTF-8</jdbc.url>33     <!-- 用户名 -->34     <jdbc.userId>root</jdbc.userId>35     <!-- 密码 -->36     <jdbc.password>root</jdbc.password>37   </properties>38 39   <dependencies>40     <dependency>41       <groupId>org.mybatis</groupId>42       <artifactId>mybatis</artifactId>43       <version>3.4.1</version>44     </dependency>45 46     <dependency>47       <groupId>pers.kaloquan</groupId>48       <artifactId>blog-model</artifactId>49     </dependency>50   </dependencies>51 52   <build>53     <plugins>54       <plugin>55         <groupId>org.mybatis.generator</groupId>56         <artifactId>mybatis-generator-maven-plugin</artifactId>57         <version>${mybatis-generator.version}</version>58         <!-- 插件配置 -->59         <configuration>60           <configurationFile>${plugin.generator.configurationFile}</configurationFile>61           <overwrite>${plugin.generator.overwrite}</overwrite>62         </configuration>63         <!-- 编译之前需要执行的命令 -->64         <executions>65           <execution>66             <id>Generate MyBatis Artifacts</id>67             <goals>68               <goal>generate</goal>69             </goals>70           </execution>71         </executions>72       </plugin>73     </plugins>74   </build>75 </project>

查看配置

  

  *注意:在properties标签中声明的属性值可以在mybatis-generator.

 

mybatis-generator.

  位置:(根据自己需要放在某个地方,改变后在对应项目的pom.

    

 1 <??> 2 <!DOCTYPE generatorConfiguration PUBLIC 3     "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4     "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" > 5 <generatorConfiguration> 6  7   <!-- !!!! Driver Class Path !!!! --> 8   <classPathEntry location="${generator.classpath}"/> 9 10   <!-- 使用MyBatis3Simple,避免生成多余的代码 -->11   <context id="context" targetRuntime="MyBatis3Simple">12     <commentGenerator>13       <property name="suppressAllComments" value="false"/>14       <property name="suppressDate" value="true"/>15     </commentGenerator>16 17     <!-- !!!! Database Configurations !!!! -->18     <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.url}" userId="${jdbc.userId}" password="${jdbc.password}"/>19 20     <javaTypeResolver>21       <property name="forceBigDecimals" value="false"/>22     </javaTypeResolver>23 24     <!-- !!!! Model Configurations !!!! -->25     <javaModelGenerator targetPackage="blog.model" targetProject="${generator.project.root}\blog-model\src\main\java">26       <property name="enableSubPackages" value="false"/>27       <property name="trimStrings" value="true"/>28     </javaModelGenerator>29 30     <!-- !!!! Mapper -->31     <sqlMapGenerator targetPackage="mapper" targetProject="${generator.project.root}\blog-dao-impl\src\main\resources" >32       <property name="enableSubPackages" value="false"/>33     </sqlMapGenerator>34 35     <!-- !!!! Mapper Interface Configurations !!!! -->36     <!-- 使用MyBatis3Simple运行时,type只能为“ANNOTATEDMAPPER”或者“-->37     <!-- 这里采用注解配置,而非-->38     <javaClientGenerator targetPackage="blog.dao" targetProject="${generator.project.root}\blog-dao-api\src\main\java" type="ANNOTATEDMAPPER">39       <property name="enableSubPackages" value="false"/>40     </javaClientGenerator>41 42     <!-- !!!! Table Configurations !!!! -->43     <table tableName="user" schema="blog">44       <columnOverride column="id" property="id"/>45       <columnOverride column="name" property="name"/>46     </table>47   </context>48 </generatorConfiguration>

查看配置

 

  不明确的地方或者有错误的地方,请指出!

 

参考资料:

  Driver/Datasource Class Names, URL Syntax and Configuration Properties for Connector/J

  MyBatis Generator

  Running MyBatis Generator With Maven