关于GeneratorMapper.xml的使用说明

步骤:

1. 创建Springboot项目

2. 在pom文件中加入generator插件

      <!-- mybatis 代码自动生成插件 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <configurationFile>GeneratorMapper.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>

3. 加入generatorMapper.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>

    <!-- 指定连接数据库的JDBC驱动包所在位置,指定到你本机的完整路径 -->
    <classPathEntry location="D:/mysql-connector-java-5.1.48.jar"/>

    <!-- 配置table表信息内容体,targetRuntime指定采用MyBatis3的版本 -->
    <context id="tables" targetRuntime="MyBatis3">
        <!--序列化-->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>

        <!--以下需要插件  -->


<!--            插入成功后返回ID-->
<!--           <plugin type="cn.doity.common.generator.plugin.InsertAndReturnKeyPlugin"/>-->

<!--           分页查询功能-->
<!--           <plugin type="cn.doity.common.generator.plugin.SelectByPagePlugin"/>-->

<!--           生成带有for update后缀的select语句插件-->
<!--           <plugin type="cn.doity.common.generator.plugin.SelectForUpdatePlugin"/>-->


        <!-- 抑制生成注释,由于生成的注释都是英文的,可以不让它生成 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>


        <!-- 配置数据库连接信息 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/springdao"
                        userId="root"
                        password="root">
        </jdbcConnection>

        <!-- 生成model类,targetPackage指定model类的包名, targetProject指定生成的model放在eclipse的哪个工程下面-->
        <javaModelGenerator targetPackage="com.example.pojo" targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />
            <property name="trimStrings" value="false" />
        </javaModelGenerator>

        <!-- 生成MyBatis的Mapper.xml文件,targetPackage指定mapper.xml文件的包名, targetProject指定生成的mapper.xml放在eclipse的哪个工程下面 -->
        <sqlMapGenerator targetPackage="com.example.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>

        <!-- 生成MyBatis的Mapper接口类文件,targetPackage指定Mapper接口类的包名, targetProject指定生成的Mapper接口放在eclipse的哪个工程下面 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <!-- 数据库表名及对应的Java模型类名 -->
        <table tableName="student"
               domainObjectName="Student"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"/>
        <table tableName="goods"
               domainObjectName="Goods"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"/>
        <table tableName="sale"
               domainObjectName="Sale"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"/>

    </context>
</generatorConfiguration>

在指定位置加入GeneratorMapper.xml文件
在这里插入图片描述
4.右侧双击maven的mybatis-generator插件生成即可。
在这里插入图片描述

<think>嗯,用户想找关于MyBatis自动生成mapper.xml文件的方法或工具,还有配置教程。我之前整理过一些相关的引用资料,现在需要结合这些来回答。首先,引用[1]提到了使用tk.mybatis.mapper的通用Mapper自动生成代码,但用户可能更关心具体的工具和步骤。引用[2]和[5]提到了mapper.xml的配置和注册,但可能不够详细。引用[3]和[4]提到了MybatisX-Generator插件和MyBatis Generator使用,这两个应该是重点。 用户需要的是工具和配置教程,所以应该分点介绍不同的方法。首先,MyBatis Generator是最常用的,需要详细说明步骤,比如添加Maven插件,配置generatorConfig.xml文件,然后运行命令。这部分在引用[4]里有提到,可以引用一下。然后,MybatisX插件对于IntelliJ用户来说更方便,引用[3]有相关的内容,需要说明安装步骤和生成方式。通用Mapper可能也是一个选项,引用[1]和[2]中的配置可以作为补充。 还要注意用户可能遇到的问题,比如路径配置不对,或者生成后的文件如何引入项目。引用[2]提到在Spring Boot中添加扫描注解和mapper-locations配置,这部分需要包含在配置注意事项里。同时,确保生成的xml文件被正确放置在resources目录下,避免路径错误。 另外,用户可能需要对比不同工具的优缺点,比如MyBatis Generator需要配置文件,而MybatisX更图形化操作。通用Mapper可能适合喜欢注解方式的开发者。最后,常见问题解答可以帮助用户解决生成过程中可能遇到的典型问题,比如文件位置、插件安装等。</think>### MyBatis自动生成mapper.xml文件的工具及配置教程 MyBatis的自动生成工具能够根据数据库表结构生成对应的实体类、Mapper接口和XML映射文件,大幅提升开发效率。以下是主流工具的配置方法和教程: --- #### 一、使用 **MyBatis Generator**(官方推荐) **适用场景**:基于Maven的Spring Boot项目或其他Java项目。 **步骤**: 1. **添加Maven插件依赖** 在`pom.xml`中添加以下配置: ```xml <build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.2</version> <configuration> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency> </dependencies> </plugin> </plugins> </build> ``` 2. **配置`generatorConfig.xml`文件** 在`src/main/resources`目录下创建`generatorConfig.xml`,内容示例: ```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="default" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/your_database" userId="root" password="root" /> <javaModelGenerator targetPackage="com.example.entity" targetProject="src/main/java"/> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mapper" targetProject="src/main/java"/> <table tableName="your_table"/> </context> </generatorConfiguration> ``` 3. **运行生成命令** 在终端执行: ```bash mvn mybatis-generator:generate ``` 生成的文件包括: - 实体类:`com.example.entity.YourTable` - Mapper接口:`com.example.mapper.YourTableMapper` - XML映射文件:`src/main/resources/mapper/YourTableMapper.xml` **注意**:需确保数据库连接配置正确[^4]。 --- #### 二、使用 **MybatisX插件**(IDEA插件) **适用场景**:IntelliJ IDEA用户,需图形化界面操作。 **步骤**: 1. **安装插件** 在IDEA中搜索并安装`MybatisX`插件。 2. **配置数据源** - 右键项目 → `Database` → 添加数据库连接。 - 输入数据库URL、用户名和密码。 3. **生成代码** - 在数据库视图中右键表 → `MybatisX-Generator`。 - 选择生成路径(实体类、Mapper接口、XML文件的位置)。 - 点击生成,自动创建代码和XML文件[^3]。 --- #### 三、使用 **通用Mapper**(tk.mybatis) **适用场景**:希望减少XML编写,通过注解实现CRUD。 **步骤**: 1. **添加依赖** 在`pom.xml`中引入: ```xml <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>4.2.1</version> </dependency> ``` 2. **配置Spring Boot** 在`application.yml`中添加: ```yaml mybatis: mapper-locations: classpath*:mapper/**/*.xml type-aliases-package: com.example.entity ``` 3. **生成代码** 使用MyBatis Generator或逆向工程工具生成基础代码[^1][^2]。 --- ### 配置注意事项 1. **XML文件路径** 确保生成的XML文件位于`src/main/resources/mapper`目录下,并在配置中指定扫描路径: ```yaml mybatis: mapper-locations: classpath:mapper/*.xml ``` 2. **实体类与Mapper接口包** 需在启动类添加扫描注解: ```java @MapperScan("com.example.mapper") @SpringBootApplication public class Application { /* ... */ } ``` --- ### 常见问题解答 1. **生成文件后无法找到XML** - 检查`mapper-locations`路径是否与文件实际位置一致。 - 确保XML文件标记为`Resources`类型(IDEA中右键目录 → `Mark Directory as → Resources Root`)[^2][^5]。 2. **插件安装失败** - 确认IDEA版本支持MybatisX插件(需2020.3及以上版本)[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值