mybatis-plus的mapper.xml在src/main/java路径下如何配置pom.xml和application.yml

pom.xml

    <build>
        <resources>
            <resource>
                <!-- xml放在java目录下-->
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <!--<filtering>false</filtering> 
                filtering设为true可替换*.xml、*.properties配置文件中的占位符(${jdbc.url})-->
            </resource>
    </build>

“**” 表示任意级目录,“*.xml” 表示任意xml文件

maven的<resources>标签里<include>**/*.xml</include>配置是什么意思_汉口君呐的博客-CSDN博客_<includes>*.*</includes>

这样配置后,maven才能将 src/main/java中的非java文件打包到target中

Maven学习笔记(十二)-maven打包之resource配置_码农致富的博客-CSDN博客_maven resource

 

二、Maven项目的标准目录结构

  • src
    • main
      • java         源文件 
      • resources    资源文件
      • filters   资源过滤文件
      • config   配置文件
      • scripts   脚本文件
      • webapp   web应用文件
    • test
      • java    测试源文件
      • resources    测试资源文件
      • filters    测试资源过滤文件
    • it       集成测试
    • assembly    assembly descriptors
    • site    Site
  • target
    • generated-sources
    • classes
    • generated-test-sources
    • test-classes
    • xxx.jar
  • pom.xml
  • LICENSE.txt
  • NOTICE.txt
  • README.txt

我遇到过target中已经打包mapper.xml文件但是mybatis还是找不到配置的情况,这是需要再配置application.yml文件:

mybatis-plus:
  mapper-locations: classpath:com/kakarote/admin/mapper/xml/*.xml

这个路径与target中的目录结果一一对应。

如果把mapper.xml放在resource目录下:

maven打包时会在target/class中生成一个对应resource中路径的新路径

这时application.yml中的配置需要改为:

mybatis-plus:
  mapper-locations: classpath:/mapper/*.xml

 或

mybatis-plus:
  mapper-locations: classpath:mapper/*.xml

 

有一种情况特殊,当mapper.xml放在同名接口java文件的相同路径时

application.yml中配置的路径不用写绝对地址可以简写为:

mybatis-plus:
  mapper-locations: classpath:/mapper/*.xml

 或

mybatis-plus:
  mapper-locations: classpath:mapper/*.xml

但是mapper.xml与java接口文件(如图中的AdminUserMapper.java)不在同一目录下,比如:

application.yml就不能简写为 mapper-locations: classpath:mapper/xml/*.xml

必须写全路径:mapper-locations: classpath:com/kakarote/admin/mapper/xml/*.xml

 

 

 mybatis-plus配置xml文件位置_qq_42800468的博客-CSDN博客_mybatis-plus xml配置

 

 maven中resource配置详解_2021不再有雨的博客-CSDN博客_maven resource

maven Filtering true 作用_滕青山YYDS的博客-CSDN博客

maven - 使用 Maven Profile 和 Filtering 打各种环境的包_个人文章 - SegmentFault 思否

  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Java Spring Boot 是一个快速开发框架,MyBatisMyBatis-Plus 是两个流行的 ORM 框架,Spring Boot 与 MyBatis/MyBatis-Plus 的整合可以帮助我们更快更方便地进行开发。 下面是使用Java Spring Boot整合MyBatis/MyBatis-Plus的步骤: 1. 在 pom.xml 文件中添加 MyBatis/MyBatis-Plus 和 MySQL 驱动的依赖: ```xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> ``` 2. 在 application.properties/application.yml 文件中配置数据源和 MyBatis/MyBatis-Plus配置信息: ```yaml spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver mybatis.type-aliases-package=com.example.demo.entity mybatis-plus.mapper-locations=classpath:/mapper/*.xml ``` 3. 在 Spring Boot 启动类上添加 `@MapperScan` 注解,指定 MyBatis/MyBatis-PlusMapper 所在的包: ```java @SpringBootApplication @MapperScan("com.example.demo.mapper") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 4. 创建实体类和 Mapper 接口: ```java public class User { private Long id; private String name; private Integer age; // 省略 getter 和 setter 方法 } @Mapper public interface UserMapper extends BaseMapper<User> { } ``` 5. 创建 MapperXML 文件(如果使用 MyBatis-Plus 可以省略此步骤): ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.demo.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.example.demo.entity.User"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="age" property="age" /> </resultMap> <select id="selectById" resultMap="BaseResultMap"> select * from user where id = #{id} </select> </mapper> ``` 6. 在 Service 中使用 Mapper: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User getUserById(Long id) { return userMapper.selectById(id); } } ``` 这样就完成了 Java Spring Boot 整合 MyBatis/MyBatis-Plus 的基本步骤。需要注意的是,在使用 MyBatis-Plus 的情况下,Mapper 接口无需自己编写 CRUD 操作的方法,直接继承 `BaseMapper` 接口即可。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值