1. 程式人生 > >自動生成mapper和domain外掛

自動生成mapper和domain外掛

  1. 在maven專案中pom檔案中加入如下外掛

        <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.2</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.longzheng.crawler.Main</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
         </plugin>

  2.在maven中的mapper中的resource中加入如下配置(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="config.properties"/>

    <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <plugin type="${mapper.plugin}">
            <property name="mappers" value="${mapper.Mapper}"/>
        </plugin>

        <jdbcConnection driverClass="${mysql.driver}"
                        connectionURL="${mysql.dburl}"
                        userId="${mysql.username}"
                        password="${mysql.password}">
        </jdbcConnection>

        <javaModelGenerator targetPackage="${targetModelPackage}" targetProject="${targetJavaProject}"/>

        <sqlMapGenerator targetPackage="${targetXMLPackage}"  targetProject="${targetResourcesProject}"/>

        <javaClientGenerator targetPackage="${targetMapperPackage}" targetProject="${targetMapperProject}" type="XMLMAPPER" />

        <table tableName="${targetTableName}" >
            <generatedKey column="id" sqlStatement="Mysql" identity="true"/>
        </table>
    </context>
</generatorConfiguration>

   2.1  (  config.properties)複製即可裡面的資訊根據自己的實際情況修改

# 資料庫配置
mysql.driver=com.mysql.jdbc.Driver
mysql.dburl=jdbc:mysql://localhost:3306/dvd?useUnicode=true&characterEncoding=UTF-8
mysql.username=root
mysql.password=root
# 通用Mapper配置
mapper.plugin=tk.mybatis.mapper.generator.MapperPlugin
mapper.Mapper=tk.mybatis.mapper.common.Mapper
#  Java介面和實體類
targetJavaProject=../domain/src/main/java
targetModelPackage=com.czxy.domain
#  Mapper 生成路徑
targetMapperProject=src/main/java
targetMapperPackage=com.czxy.mapper
# Mapper XML 生成路徑
#targetResourcesProject=src/main/resources
#targetXMLPackage=com.czxy.mapper
# 指定要生成的表名 %為所有表 單表,請指定表名
targetTableName=%

3.執行外掛,生成mapper和domain