美文网首页
mybatis maven 生成器插件使用

mybatis maven 生成器插件使用

作者: 天堂宝宝_V | 来源:发表于2017-05-24 09:23 被阅读323次

    mybatis maven 生成器插件使用
    mybatis,Generator,maven,插件

    配置pom.xml插件

    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <!-- mybatis 配置文件路径 -->
                    <configurationFile>src/main/resources/mybatisgen.xml</configurationFile>
                    <!-- 警告:覆盖式文件写入 -->
                    <overwrite>true</overwrite>
                    <!-- 打印日志 -->
                    <verose>true</verose>
                </configuration>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <!-- 为插件单独配置mysql驱动 -->
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.39</version>
                    </dependency>
                </dependencies>
    
            </plugin>
        </plugins>
    </pluginManagement>
    

    资源路径下配置mybatisgen.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>
        <context id="cloudDiskTables" targetRuntime="MyBatis3"
            defaultModelType="flat">
            <property name="beginningDelimiter" value="`" />
            <property name="endingDelimiter" value="`" />
    
            <commentGenerator>
                <property name="suppressDate" value="true" />
            </commentGenerator>
            
            
            <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/clouddisk" userId="root"
                password="123">
            </jdbcConnection>
            
            <!-- 配置表、Java属性的对应关系 -->
            <javaTypeResolver >
                <property name="forceBigDecimals" value="false" />
            </javaTypeResolver>
    
            <!-- 配置生成文件的位置 -->
            <javaModelGenerator targetPackage="com.clouddisk.model" targetProject="src/main/java">
                <property name="enableSubPackages" value="true" />
                <property name="trimStrings" value="true" />
            </javaModelGenerator>
            
            <!-- xml生成器 -->
            <sqlMapGenerator targetPackage="com.clouddisk.dao.mapper" targetProject="src/main/java">
                <property name="enableSubPackages" value="true" />
            </sqlMapGenerator>
            
            <!-- 生成dao -->
            <javaClientGenerator type="XMLMAPPER"
                targetPackage="com.clouddisk.dao" targetProject="src/main/java">
                <property name="enableSubPackages" value="true" />
            </javaClientGenerator>
    
            <!-- 配置需要哪些表需要生成 全部生成-->
            <!-- 
            <table tableName="%">
                <generatedKey column="id" sqlStatement="mysql" />
            </table>
             -->
            
            <!-- 单独设置一张表的自动代码生成 -->
            <table tableName="p_file_index" domainObjectName="PFileIndex">
                <!-- 配置插入一条记录后 获取到插入后的主键ID-->
                <generatedKey column="p_index_id" sqlStatement="Mysql" type="post" identity="true"/>
            </table>
            
    
        </context>
    </generatorConfiguration>
    

    配置run configuration

    enter description hereenter description here enter description hereenter description here enter description hereenter description here

    其他

    生成后的插入sql写法是这样的

    <insert id="insert" parameterType="com.clouddisk.model.PFileIndex">
        <!--
          WARNING - @mbg.generated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        <selectKey keyProperty="pIndexId" order="AFTER" resultType="java.lang.Long">
          SELECT LAST_INSERT_ID()
        </selectKey>
        insert into p_file_index (file_size, create_time, p_name, 
          p_path, file_md5)
        values (#{fileSize,jdbcType=BIGINT}, #{createTime,jdbcType=TIMESTAMP}, #{pName,jdbcType=VARBINARY}, 
          #{pPath,jdbcType=VARBINARY}, #{fileMd5,jdbcType=VARBINARY})
      </insert>
    

    相关资料

    MyBatis Generator 官方maven使用配置文档

    MyBatis Generator中文文档
    MyBatis Generator 详解

    相关文章

      网友评论

          本文标题:mybatis maven 生成器插件使用

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