现在的位置: 首页 > 综合 > 正文

mybatis-generator-meven-plugin

2019年04月13日 ⁄ 综合 ⁄ 共 2845字 ⁄ 字号 评论关闭

1、pom.xml下配置mybatis-generator-maven-plugin

<build>
    <plugins>
        <plugin>  
             <groupId>org.mybatis.generator</groupId>  
             <artifactId>mybatis-generator-maven-plugin</artifactId>  
             <version>1.3.1</version>  
             <configuration>  
                 <verbose>true</verbose>  
                 <overwrite>true</overwrite>  
             </configuration>  
        </plugin>
    </plugins>
</build> 

2、generatorConfig.xml,放在src/main/resource下,需要设置①数据库驱动路径,②jdbc连接信息,③实体、xml、Dao借口文件的包名和存放路径(如果设置成非项目路径,生成后拷到项目下相应路径即可),④要生成的表名(实体)等。

<?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:数据库的JDBC驱动 -->
    <classPathEntry
        location="/Users/nnxiaod/Downloads/mysql-connector-java-5.1.21.jar" />

    <context id="MysqlTables" targetRuntime="MyBatis3">

        <!-- 注意这里面的顺序确定的,不能随变更改 -->
        <!-- 自定义的分页插件 <plugin type="com.deppon.foss.module.helloworld.shared.PaginationPlugin"/> -->

        <!-- 可选的(0 or 1) -->
        <!-- 注释生成器 -->
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!-- 必须的(1 required) -->
        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/blog?generateSimpleParameterMetadata=true"
            userId="root" password="123456">
        </jdbcConnection>

        <!-- 可选的(0 or 1) -->
        <!-- 类型转换器或者加类型解析器 -->
        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和 
            NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>


        <!-- 必须的(1 required) -->
        <!-- java模型生成器 -->
        <!-- targetProject:自动生成代码的位置 -->
        <javaModelGenerator targetPackage="com.liufei.blog.model"
            targetProject="/Users/nnxiaod/Documents/workspace/blog/src/main/java">
            <!-- TODO enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="true" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- 必须的(1 required) -->
        <!-- map xml 生成器 -->
        <sqlMapGenerator targetPackage="com.liufei.blog.persistence"
            targetProject="/Users/nnxiaod/Documents/workspace/blog/src/main/java">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- 可选的(0 or 1) -->
        <!-- mapper 或者就是dao接口生成器 -->
        <javaClientGenerator targetPackage="com.liufei.blog.dao"
            targetProject="/Users/nnxiaod/Documents/workspace/blog/src/main/java"
            type="XMLMAPPER">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 必须的(1...N) -->
        <!-- pojo 实体生成器 -->
        <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->
        <!-- schema即为数据库名 可不写 -->
        <table schema="data" tableName="user" domainObjectName="User"
            enableInsert="true">
            <!-- 忽略字段 可选的(0 or 1) -->
            <!-- <ignoreColumn column="is_use" /> -->
            <!--//无论字段是什么类型,生成的类属性都是varchar。 可选的(0 or 1) 测试无效 -->
            <!-- <columnOverride column="city_code" jdbcType="VARCHAR" /> -->
        </table>
    </context>
</generatorConfiguration>

3、然后在pom.xml上右键Run As,Maven build...,Goals里填 mybatis-generator:generate,Run,OK!

抱歉!评论已关闭.