mybatis-generator自动生成器的使用

本文详细介绍了如何在Maven项目中配置MyBatis Generator插件,包括添加依赖、配置mybatis-generator.xml文件,以及生成Model、DAO和映射文件的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.添加mybatis-generator jar

<plugin>
          <groupId>org.mybatis.generator</groupId>
          <artifactId>mybatis-generator-maven-plugin</artifactId>
          <version>1.3.5</version>
          <executions>
            <execution>
              <id>mybatis generator</id>
              <phase>package</phase>
              <goals>
                <goal>generate</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <!--允许移动生成的文件-->
            <verbose>true</verbose>
            <!--允许自动覆盖文件-->
            <overwrite>false</overwrite>
             <!--配置文件放置的位置-->
            <configurationFile>
              src/main/resources/mybatis-generator.xml
            </configurationFile>
          </configuration>
</plugin>

2.配置mybatis-generator.xml文件 ,对应的在项目中添加mapping 、dataobject ,dao 等文件夹

<generatorConfiguration>

    <!--<classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />-->


    <context id="MyTable" targetRuntime="MyBatis3">
        <!--数据库连接地址账号密码-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://xxxx/xxx"
                        userId="xxxx"
                        password="xxxx">
        </jdbcConnection>
  
        <!--生成Model/DataObject类存放的位置-->
        <javaModelGenerator targetPackage="com.miaoshaproject.dataobject" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!--生成映射文件存放的位置-->
        <sqlMapGenerator targetPackage="mapping"  targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!--生成Dao类存放的位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.miaoshaproject.dao"  targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
         <!--对应的数据库的实体 填写数据库名字 ,对应生成实体类的名字-->
         <table tableName="promo" domainObjectName="PromoDO" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
          </table>

    </context>
</generatorConfiguration>

3.选中项目 run- maven build
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值