Mybatis使用Maven插件,根据表逆向生成实体类、mapper接口和mapper的xml文件

resorues所需xml文件(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>

    <!--    连接数据库jar包的路径-->
    <classPathEntry
            location="D:\IDEA2021Demo0\apache-maven-3.6.3\mvn_resp\mysql\mysql-connector-java\5.1.47\mysql-connector-java-5.1.47.jar"/>
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--数据库连接参数 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" userId="root" password="2770085055"
                        connectionURL="jdbc:mysql://localhost:3306/ssm?useSSL=false&amp;nullNamePatternMatchesAll=true">
        </jdbcConnection>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 实体类的包名和存放路径 -->
        <javaModelGenerator targetPackage="com.santi.pojo" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 生成映射文件*.xml的位置-->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- 生成DAO的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.santi.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- tableName:数据库中的表名或视图名;domainObjectName:生成的实体类的类名-->
        <table tableName="emp" domainObjectName="Emp"
               enableCountByExample="true"
               enableUpdateByExample="true"
               enableDeleteByExample="true"
               enableSelectByExample="true"
               selectByExampleQueryId="true"/>

    </context>


    <!--    <classPathEntry location="E:\repository\mysql\mysql-connector-java\8.0.26\mysql-connector-java-8.0.26.jar" />-->
    <!--    <context id="DB2Tables" targetRuntime="MyBatis3">-->


    <!--        &lt;!&ndash;数据库链接URL,用户名、密码 &ndash;&gt;-->
    <!--        <jdbcConnection driverClass="com.mysql.jdbc.Driver"-->
    <!--                        connectionURL="jdbc:mysql://localhost:3306/book?useSSL=false&amp;nullNamePatternMatchesAll=true"-->
    <!--                        userId="root"-->
    <!--                        password="xxx">-->
    <!--            &lt;!&ndash;设置可以获取tables remarks信息&ndash;&gt;-->
    <!--            <property name="useInformationSchema" value="true"/>-->
    <!--            &lt;!&ndash;设置可以获取remarks信息&ndash;&gt;-->
    <!--            <property name="remarks" value="true"/>-->
    <!--        </jdbcConnection>-->


    <!--        &lt;!&ndash; 生成模型的包名和位置 &ndash;&gt;-->
    <!--        <javaModelGenerator targetPackage="com.santi.entity"-->
    <!--                            targetProject="src/main/java">-->
    <!--            <property name="enableSubPackages" value="true" />-->
    <!--            <property name="trimStrings" value="true" />-->
    <!--        </javaModelGenerator>-->
    <!--        &lt;!&ndash; 生成映射文件的包名和位置 &ndash;&gt;-->
    <!--        <sqlMapGenerator targetPackage="com.santi.mapper"-->
    <!--                         targetProject="src/main/java"  >-->
    <!--            <property name="enableSubPackages" value="true" />-->
    <!--        </sqlMapGenerator>-->

    <!--        &lt;!&ndash; 生成DAO的包名和位置 &ndash;&gt;-->
    <!--        <javaClientGenerator type="XMLMAPPER"-->
    <!--                             targetPackage="com.santi.mapper" targetProject="src/main/java">-->
    <!--            <property name="enableSubPackages" value="true" />-->
    <!--        </javaClientGenerator>-->


    <!--        <table catalog="book" tableName="t_book" domainObjectName="Book">-->
    <!--            <property name="ignoreQualifiersAtRuntime" value="true" />-->
    <!--            <generatedKey column="id" sqlStatement="mysql" identity="true" />-->
    <!--        </table>-->


    <!--    </context>-->
</generatorConfiguration>

pom.xml文件的jar:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.santi</groupId>
    <artifactId>Spring-Mybatis</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <!--mybatis-generator的jar-->
        <mybatis-generator-maven-plugin.version>1.3.2</mybatis-generator-maven-plugin.version>
        <mybatis-generator-core.version>1.3.2</mybatis-generator-core.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>${mybatis-generator-core.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.ibatis/ibatis-core -->
        <dependency>
            <groupId>org.apache.ibatis</groupId>
            <artifactId>ibatis-core</artifactId>
            <version>3.0</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>Spring-Mybatis</finalName>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>${mybatis-generator-maven-plugin.version}</version>
                <configuration>
                    <!-- mybatis-generator配置文件路径 -->
                    <configurationFile>src/main/resources/generatorConfig.xml
                    </configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>

    </build>

</project>

最后添加配置:

在Editor Configurations里添加Maven ,第一个选项中 第一个为你的模块,第二个填:

mybatis-generator:generate 然后打开Maven 

 双击spring-mybatis,出现

就成功了 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程小朱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值