Mybatis maven插件自动生成数据库postgresql mapper代码

1.在dao层的pom.xml加入

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
                <version>3.3</version>
            </plugin>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <!-- 版本号自定义 -->
                        <version>xx</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

2.在dao层的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>
    <!-- 可不指定 -->
    <classPathEntry location="/xx/postgresql-xx.jar"/>
    <context id="sss">
        <jdbcConnection driverClass="org.postgresql.Driver"
                        connectionURL="jdbc:postgresql:/xx:xx/xx"
                        userId="xx"
                        password="xx">
        </jdbcConnection>

        <javaModelGenerator targetPackage="实体类包名" targetProject="/xx/src/main/java">
            <property name="enableSubPackages" value="false"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="xml文件包名" targetProject="/xx/src/main/resources/">
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER" targetPackage="mapper包名" targetProject="/xx/src/main/java">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>


        <!--    
        <table schema="xx" tableName="表名" domainObjectName="类名" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true" ></table>
        -->
    </context>
</generatorConfiguration>

3.mvn clean complie

over

猜你喜欢

转载自blog.csdn.net/fenglllle/article/details/81561193
今日推荐