「已注销」 2019-03-19 00:26 采纳率: 0%
浏览 433

springboot整合mybatis时,使用MBG插件问题

数据库连接的url:

jdbc.url = jdbc:mysql://localhost:3306/test

使用通用Mapper和MBG时,在generatorConfig.xml中配置:

<table tableName="%"  />

会将所有的数据库的所有表对应的文件都生成出来,而不只是test数据库,请问为什么?

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-08 09:48
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    在Spring Boot整合MyBatis时,使用MBG插件可能会导致一些特定的SQL语句被错误地处理。这种情况下,通常的原因是由于某些特定的SQL语句被误认为是通用的,而没有按照指定的方式进行处理。

    具体来说,如果你在一个项目中使用了<table>标签来指定查询的所有表,并且你想要自动生成这些表对应的映射文件,那么MBG插件可能会默认生成所有表的映射文件,而不是仅限于与当前应用相关的表。

    例如,如果你有一个名为users的表,你在generatorConfig.xml中的<table>标签中定义为<table tableName="%"/>,则MBG插件会生成一个名为users的所有表的映射文件。

    解决这个问题的方法有很多,但最常见的是调整你的generatorConfig.xml以只生成特定表的映射文件。你可以这样做:

    1. generatorConfig.xml中添加一个注解(如@Target、@Include等)来指定要生成映射文件的表名。
    2. 使用@SqlSource注解来指定SQL源,这样可以控制哪些SQL语句会被执行。

    以下是一个示例,展示了如何在generatorConfig.xml中设置这个:

    <configuration>
        <mappers>
            <!-- 这里添加你需要生成映射文件的表 -->
            <include name="**/*.sql" />
        </mappers>
    
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <mapperAnnotationPrefix>com.example.mapper.</mapperAnnotationPrefix></mapperAnnotationPrefix>
                    <targetTablePrefix>tb_</targetTablePrefix>
                    <skip>true</skip>
                    <exclude>true</exclude>
                    <include>true</include>
                    <sourceMap>true</sourceMap>
                    <addDefaultSqlSource>true</addDefaultSqlSource>
                    <defaultSqlSourceName>default</defaultSqlSourceName>
                    <defaultSqlSourceId>default</defaultSqlSourceId>
                    <useJavaType>true</useJavaType>
                    <javaTypePackage>com.example.model</javaTypePackage>
                    <generateWrapperClasses>true</generateWrapperClasses>
                    <modelClassPrefix>Model</modelClassPrefix>
                    <modelObjectPrefix>Obj</modelObjectPrefix>
                    <noGeneratedSql>true</noGeneratedSql>
                    <trimGeneratedSql>true</trimGeneratedSql>
                    <addCommentsToColumns>true</addCommentsToColumns>
                    <commentsPrefix>/**
     * Generated by MyBatis Generator
     */</commentsPrefix>
                    <insertColumnPrefix>
                        INSERT INTO %T%B%T%B ( %s, ... )
                        VALUES ( %s, ... );
                    </insertColumnPrefix>
                    <updateColumnPrefix>
                        UPDATE %T%B%T%B SET %s WHERE %s;
                    </updateColumnPrefix>
                    <deleteColumnPrefix>
                        DELETE FROM %T%B%T%B WHERE %s;
                    </deleteColumnPrefix>
                    <selectColumnPrefix>
                        SELECT %s FROM %T%B%T%B ;
                    </selectColumnPrefix>
                    <resultMapPrefix>
                        <resultMap id="%T%B%T%B_%T%B">
                            <id column="%s" property="id"/>
                            <result column="%s" property="name"/>
                            ...
                        </resultMap>
                    </resultMapPrefix>
                    <resultListPrefix>
                        <resultList id="%T%B%T%B_%T%B">
                            <result property="list"/>
                        </resultList>
                    </resultListPrefix>
                    <parameterTypePrefix>
                        <parameterType id="%T%B%T%B_%T%B">
                            <type>java.lang.Object</type>
                        </parameterType>
                    </parameterTypePrefix>
                    <resultTypePrefix>
                        <resultType id="%T%B%T%B_%T%B">
                            <type>java.lang.Object</type>
                        </resultType>
                    </resultTypePrefix>
                </configuration>
            </plugin>
        </plugins>
    </configuration>
    

    在这个例子中,我们设置了<mapperAnnotationPrefix>com.example.mapper.</mapperAnnotationPrefix></mapperAnnotationPrefix>来指定了我们要使用的命名空间,以及<targetTablePrefix>tb_</targetTablePrefix>来指定我们的目标表前缀。同时,我们也设置了<skip>true</skip>来跳过不感兴趣的表。

    然后,我们在<include>元素中添加了**/*.sql来包含所有表的SQL文件,而在<exclude>元素中添加了**/*.xml来排除任何已存在的XML映射文件。

    最后,我们在<sqlSource>元素中指定了SQL源,这将决定哪些SQL语句会被生成。

    通过这种方式,你可以确保只有与你的应用相关联的表才被生成映射文件,从而避免了不必要的重复工作。

    评论

报告相同问题?