美文网首页
mybatis-generator-maven-plugin

mybatis-generator-maven-plugin

作者: 浮x尘 | 来源:发表于2017-06-20 15:45 被阅读177次

由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,mybatis-gennerator插件帮我们自动生成mybatis所需要的dao、bean、mapper xml文件。

       <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <dependencies>
                    <dependency>
                        <groupId>com.vua</groupId>
                        <artifactId>vua-common</artifactId>
                        <version>${parent.version}</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <!--运行时显示详细的信息-->
                    <verbose>true</verbose>
                    <!--覆盖文件-->
                    <overwrite>true</overwrite>
                    <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile> 
                </configuration>
            </plugin>

配置文件:

<?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="generator.properties"></properties>

    <context id="MysqlContext" targetRuntime="MyBatis3" defaultModelType="flat">

        <property name="javaFileEncoding" value="UTF-8"/>
        <!-- 由于beginningDelimiter和endingDelimiter的默认值为双引号("),在Mysql中不能这么写,所以还要将这两个默认值改为`  -->
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <!-- 为生成的Java模型创建一个toString方法 -->
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>

        <!-- 为生成的Java模型类添加序列化接口,并生成serialVersionUID字段 -->
        <plugin type="com.zheng.common.plugin.SerializablePlugin">
            <property name="suppressJavaInterface" value="false"/>
        </plugin>

        <!-- 生成一个新的selectByExample方法,这个方法可以接收offset和limit参数,主要用来实现分页,只支持mysql(已使用pagehelper代替) -->
        <!--<plugin type="com.zheng.common.plugin.PaginationPlugin"></plugin>-->

        <!-- 生成在XML中的<cache>元素 -->
        <plugin type="org.mybatis.generator.plugins.CachePlugin">
            <!-- 使用ehcache -->
            <property name="cache_type" value="org.mybatis.caches.ehcache.LoggingEhcache" />
            <!-- 内置cache配置 -->
            <!--
            <property name="cache_eviction" value="LRU" />
            <property name="cache_flushInterval" value="60000" />
            <property name="cache_readOnly" value="true" />
            <property name="cache_size" value="1024" />
            -->
        </plugin>

        <!-- Java模型生成equals和hashcode方法 -->
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>

        <!-- 生成的代码去掉注释 -->
        <commentGenerator type="com.zheng.common.plugin.CommentGenerator">
            <property name="suppressAllComments" value="true" />
            <property name="suppressDate" value="true"/>
        </commentGenerator>

        <!-- 数据库连接 -->
        <jdbcConnection driverClass="${generator.jdbc.driver}"
                        connectionURL="${generator.jdbc.url}"
                        userId="${generator.jdbc.username}"
                        password="123456" />

        <!-- model生成 -->
        <javaModelGenerator targetPackage="com.vua.upms.dao.model" targetProject="vua-upms/vua-upms-dao/src/main/java" />

        <!-- MapperXML生成 -->
        <sqlMapGenerator targetPackage="com.vua.upms.dao.mapper" targetProject="vua-upms/vua-upms-rpc-service/src/main/java" />

        <!-- Mapper接口生成 -->
        <javaClientGenerator targetPackage="com.vua.upms.dao.mapper" targetProject="vua-upms/vua-upms-dao/src/main/java" type="XMLMAPPER" />

        <!-- 需要映射的表 -->
        <!-- 需要映射的表 -->
                                    <table tableName="upms_log" domainObjectName="UpmsLog"></table>
                                                <table tableName="upms_organization" domainObjectName="UpmsOrganization"></table>
                                                <table tableName="upms_permission" domainObjectName="UpmsPermission"></table>
                                                <table tableName="upms_role" domainObjectName="UpmsRole"></table>
                                                <table tableName="upms_role_permission" domainObjectName="UpmsRolePermission"></table>
                                                <table tableName="upms_system" domainObjectName="UpmsSystem"></table>
                                                <table tableName="upms_user" domainObjectName="UpmsUser">
                    <generatedKey column="user_id" sqlStatement="MySql" identity="true"/>
                </table>
                                                <table tableName="upms_user_organization" domainObjectName="UpmsUserOrganization"></table>
                                                <table tableName="upms_user_permission" domainObjectName="UpmsUserPermission"></table>
                                                <table tableName="upms_user_role" domainObjectName="UpmsUserRole"></table>
                        </context>
</generatorConfiguration>

执行mybatis-generator:generate命令,生成文件

相关文章

网友评论

      本文标题:mybatis-generator-maven-plugin

      本文链接:https://www.haomeiwen.com/subject/xupoqxtx.html