Mybatis学习问题整理

1.测试写好的UserMapper接口,运行时出现如下错误


2.原因是编译之后未在target生成userMapper.xml


userMapper.xml有两个位置可以放
第一个是放在resources文件夹下
此时需要在application.yml配置文件中加入location参数

mybatis:
  configuration:
    use-generated-keys: true
    map-underscore-to-camel-case: true
  type-aliases-package: com.example.community.entity
  mapper-locations: classpath:mapper/*.xml

第二个可以直接放在UserMapper路径下,会造成的问题就是java目录下的xml资源在项目编译打包时时会被忽略掉,无法在target中生成所以,如果userMapper.xml放在该路径下,需要在pom.xml文件中再添加如下配置,避免XML文件在编译时自动被忽略掉

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
</build>

同时需要在application.yml中添加xml所在路径

mybatis:
  mapper-locations:  classpath*:com/example/community/Mapper/xml/*.xml

两个方法选其一,同时在调试的时候需要clean maven编译生成的target,再运行
成功运行


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容