maven项目使用mybatis-generator自动生成代码

1.添加mybatis-generator插件,打开pom.xml文件 在project节点下添加:

<build>
         <plugins>
         <!-- MyBatis代码生成 -->
         <plugin>  
             <groupId>org.mybatis.generator</groupId>  
             <artifactId>mybatis-generator-maven-plugin</artifactId>  
             <version>1.3.2</version>  
             <configuration>  
                 <verbose>true</verbose>  
                 <overwrite>true</overwrite>  
                 <configurationFile>src_config/generatorConfig.xml</configurationFile>  
             </configuration>  
         </plugin> 
         </plugins>
         <resources>  
           <!-- 配置多个资源文件夹 -->
           <resource>  
              <directory>src_config</directory>  
           </resource>  
            </resources>  
     </build>

2.maven默认的资源文件是放在:src/main/resources下的,笔者习惯放在src_config下,所以上面使用了resources节点配置多个资源文件夹

3.添加generatorConfig.properties文件,内容如下:

#数据库驱动jar  
drive.class.path=C:/Users/think/.m2/repository/mysql/mysql-connector-java/5.1.30/mysql-connector-java-5.1.30.jar
   
#数据库连接参数  
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/trip?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
   
#包路径配置  
model.package=com.trip.model
dao.package=com.trip.dao
xml.mapper.package=resources\mybatis\mappers
target.project=src/main/java

注意:mysql-connector-java-5.1.30.jar路径 使用/ ,key-value后面不要有多余的空格,否则会报找不到jdbc驱动。

4.添加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>  
<!-- 配置文件路径 -->  
<properties resource="generatorConfig.properties"/>  
<!--数据库驱动包路径 -->  
<classPathEntry location="${drive.class.path}"/>  
   
<context id="MySQLTables" targetRuntime="MyBatis3">  
<!--关闭注释 -->  
 <commentGenerator>  
<property name="suppressDate" value="true"/>  
</commentGenerator>  
   
<!--数据库连接信息 -->  
<jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}" userId="${jdbc.username}"  
password="${jdbc.password}">  
</jdbcConnection>  
   
<!--生成的model 包路径 -->  
<javaModelGenerator targetPackage="${model.package}" targetProject="${target.project}">  
<property name="enableSubPackages" value="ture"/>  
<property name="trimStrings" value="true"/>  
</javaModelGenerator>  
   
<!--生成xml mapper文件 路径 -->  
<sqlMapGenerator targetPackage="mybatis/mappers" targetProject="src_auth">  
<property name="enableSubPackages" value="ture"/>  
</sqlMapGenerator>  
   
<!-- 生成的Dao接口 的包路径 -->  
<javaClientGenerator type="XMLMAPPER" targetPackage="${dao.package}" targetProject="${target.project}">  
<property name="enableSubPackages" value="ture"/>  
</javaClientGenerator>  
   
<!--对应数据库表名 -->  
<table tableName="auth_user">  
   
</table>  
</context>  
</generatorConfiguration>  

5.运行mybatis-generator插件

2.输入命令:mybatis-generator:generate

3.生成成功

 ==========================================================================================

笔者框架是基于SpringMVC+Mybatis,贴出pom.xml代码

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.trip</groupId>
  <artifactId>trip</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>trip</name>
  <url>http://maven.apache.org</url>

  <properties>
       <!-- 文件拷贝时的编码 -->  
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  
     <!-- 编译时的编码 -->  
     <maven.compiler.encoding>UTF-8</maven.compiler.encoding>  
     <!-- spring版本号 -->  
     <spring.version>4.0.2.RELEASE</spring.version>  
     <!-- mybatis版本号 -->  
     <mybatis.version>3.2.6</mybatis.version>  
     <!-- log4j日志文件管理包版本 -->  
     <slf4j.version>1.7.7</slf4j.version>  
     <log4j.version>1.2.17</log4j.version>  
  </properties>

  <dependencies>  
          <!-- google gson -->
          <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.6.2</version>
        </dependency>
          <!-- junit -->
        <dependency>  
            <groupId>junit</groupId>  
            <artifactId>junit</artifactId>  
            <version>4.11</version>  
            <!-- 表示开发的时候引入,发布的时候不会加载此包 -->  
            <scope>test</scope>  
        </dependency>  
        <!-- spring核心包 -->  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-core</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-web</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-oxm</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-tx</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-jdbc</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-webmvc</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-aop</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context-support</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-test</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <!-- mybatis核心包 -->  
        <dependency>  
            <groupId>org.mybatis</groupId>  
            <artifactId>mybatis</artifactId>  
            <version>${mybatis.version}</version>  
        </dependency>  
        <!-- mybatis/spring包 -->  
        <dependency>  
            <groupId>org.mybatis</groupId>  
            <artifactId>mybatis-spring</artifactId>  
            <version>1.2.2</version>  
        </dependency>  
        <!-- 导入java ee jar 包 -->  
        <dependency>  
            <groupId>javax</groupId>  
            <artifactId>javaee-api</artifactId>  
            <version>7.0</version>  
        </dependency>  
        <!-- 导入Mysql数据库链接jar包 -->  
        <dependency>  
            <groupId>mysql</groupId>  
            <artifactId>mysql-connector-java</artifactId>  
            <version>5.1.30</version>  
        </dependency>  
        <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->  
        <dependency>  
            <groupId>commons-dbcp</groupId>  
            <artifactId>commons-dbcp</artifactId>  
            <version>1.2.2</version>  
        </dependency>  
        <!-- JSTL标签类 -->  
        <dependency>  
            <groupId>jstl</groupId>  
            <artifactId>jstl</artifactId>  
            <version>1.2</version>  
        </dependency>  
        <!-- 日志文件管理包 -->  
        <!-- log start -->  
        <dependency>  
            <groupId>log4j</groupId>  
            <artifactId>log4j</artifactId>  
            <version>${log4j.version}</version>  
        </dependency>  
          
          
        <!-- 格式化对象,方便输出日志 -->  
        <dependency>  
            <groupId>com.alibaba</groupId>  
            <artifactId>fastjson</artifactId>  
            <version>1.1.41</version>  
        </dependency>  
  
  
        <dependency>  
            <groupId>org.slf4j</groupId>  
            <artifactId>slf4j-api</artifactId>  
            <version>${slf4j.version}</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.slf4j</groupId>  
            <artifactId>slf4j-log4j12</artifactId>  
            <version>${slf4j.version}</version>  
        </dependency>  
        <!-- log end -->  
        <!-- 映入JSON -->  
        <dependency>  
            <groupId>org.codehaus.jackson</groupId>  
            <artifactId>jackson-mapper-asl</artifactId>  
            <version>1.9.13</version>  
        </dependency>  
        <!-- 上传组件包 -->  
        <dependency>  
            <groupId>commons-fileupload</groupId>  
            <artifactId>commons-fileupload</artifactId>  
            <version>1.3.1</version>  
        </dependency>  
        <dependency>  
            <groupId>commons-io</groupId>  
            <artifactId>commons-io</artifactId>  
            <version>2.4</version>  
        </dependency>  
        <dependency>  
            <groupId>commons-codec</groupId>  
            <artifactId>commons-codec</artifactId>  
            <version>1.9</version>  
        </dependency>  
    </dependencies> 
     
     <build>
         <plugins>
         <!-- MyBatis代码生成 -->
         <plugin>  
             <groupId>org.mybatis.generator</groupId>  
             <artifactId>mybatis-generator-maven-plugin</artifactId>  
             <version>1.3.2</version>  
             <configuration>  
                 <verbose>true</verbose>  
                 <overwrite>true</overwrite>  
                 <configurationFile>src_config/generatorConfig.xml</configurationFile>  
             </configuration>  
         </plugin> 
         </plugins>
         <resources>  
           <!-- 配置多个资源文件夹 -->
           <resource>  
              <directory>src_config</directory>  
           </resource>  
            </resources>  
     </build>
</project>

 

posted @ 2016-05-23 09:20  思如雨  阅读(13761)  评论(1编辑  收藏  举报