Spring boot整合mybatis,xml资源文件放置及路径配置问题

对你有帮助的话,亲动动小手指,留言点赞o( ̄▽ ̄)d哦​​​​!给我一份坚持写博客的力量​​​

网上各种解决方案,我试了好久,整合了几篇文章才凑出来,在这里分享一下,实在不想网友们在这里面绕圈子,毕竟,写代码的时间是愉快的,解决bug也是愉快的,但也是一直在bug里面绕圈子就不爽了。

亲自试验:

1)  我的mapper和xml是这样子放置的

2) 在.xml中namespace是这样的:

3) application.properties中mybatis.mapper-locations得这么配置到xml

4) 最后呢,你只要在pom.xml中build下这样配置

5) 按照我这种位置防止,把响应的包名换成自己的,就基本上不会没有什么问题的。

也不用去加什么*Application中去加什么注解@MapperScan(value = "com.acme.shop.mapper")什么的了,不需要呢
  • 28
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
### 回答1: Spring Boot可以很方便地与MyBatis进行整合,只需要在pom.xml文件中添加相关依赖,配置数据源和MyBatis配置文件即可。 1. 添加依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> ``` 2. 配置数据源 在application.properties文件中配置数据源: ``` spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false spring.datasource.username=root spring.datasource.password=root ``` 3. 配置MyBatis 在application.properties文件中配置MyBatis: ``` mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.example.demo.entity ``` 其中,mapper-locations表示MyBatis的映射文件所在的位置,type-aliases-package表示实体类所在的包名。 4. 编写Mapper接口和映射文件 在Mapper接口中定义SQL语句,例如: ``` public interface UserMapper { @Select("SELECT * FROM user WHERE id = #{id}") User getUserById(Integer id); } ``` 在映射文件中编写SQL语句,例如: ``` <mapper namespace="com.example.demo.mapper.UserMapper"> <select id="getUserById" parameterType="java.lang.Integer" resultType="com.example.demo.entity.User"> SELECT * FROM user WHERE id = #{id} </select> </mapper> ``` 5. 使用Mapper接口 在需要使用Mapper接口的地方注入Mapper接口即可使用,例如: ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User getUserById(Integer id) { return userMapper.getUserById(id); } } ``` 以上就是Spring Boot整合MyBatis的基本步骤。 ### 回答2: Spring Boot是一个Java web应用程序框架,它可以自动配置和快速构建应用程序。MyBatis是一种流行的持久化框架,使用XML或注释配置映射器和SQL语句。 要整合Spring BootMyBatis,需要进行以下步骤: 1. 添加mybatis-spring-boot-starter依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>x.x.x</version> </dependency> ``` 2. 配置数据源 在application.properties或application.yml文件中配置数据源。例如: ``` spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase spring.datasource.username=root spring.datasource.password=password ``` 3. 配置MyBatis 在application.properties或application.yml文件中添加以下配置: ``` mybatis.mapper-locations=classpath:mapper/*.xml ``` 这将告诉MyBatis mapper文件所在的位置。 4. 创建DAO接口 创建DAO接口,使用MyBatis注解或XML配置SQL语句。例如: ``` @Mapper public interface UserMapper { @Select("SELECT * FROM user WHERE id = #{id}") User findById(Long id); } ``` 或者: ``` public interface UserMapper { User findById(@Param("id") Long id); } ``` 在MyBatis-Spring中,@Mapper注解会自动扫描接口并创建DAO实现类。 5. 注入DAO Bean 使用自动注入或手动注入将DAO Bean注入到服务中。例如: ``` @Service public class UserService { @Autowired private UserMapper userMapper; public User findById(Long id) { return userMapper.findById(id); } } ``` 6. 启动应用程序 使用Spring Boot应用程序的主类启动应用程序。 综上所述,将Spring BootMyBatis整合是相当简单的,只需要添加依赖项,配置数据源和MyBatis,编写DAO接口以及将其注入到服务中即可。整个过程需要保持良好的代码规范和配置文件的正确性。 ### 回答3: Spring BootSpring Framework的一个扩展,它提供了一种快速、方便的开发方式。MyBatis是一个流行的ORM框架,它是基于Java的持久化框架,可以轻松地将Java对象映射到关系型数据库中。 Spring Boot整合MyBatis可以方便地构建Web应用程序,使得操作数据库变得更加容易。下面是Spring Boot整合MyBatis的步骤: 1. 添加依赖 添加Maven或Gradle依赖项,包括Spring BootMyBatis: Maven: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> ``` Gradle: ```groovy dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.0' } ``` 2. 配置数据源 在application.properties中配置数据库连接池: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/myDatabase spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 3. 配置MyBatis配置文件中添加以下配置: ```properties mybatis.mapper-locations=classpath:mapper/*.xml ``` 这将加载在类路径下的所有mapper文件。 4. 创建Mapper接口 创建Mapper接口: ```java @Repository public interface UserMapper { User findById(int id); } ``` 5. 创建Mapper.xml 文件 在resources/mapper目录下创建Mapper.xml文件: ```xml <mapper namespace="com.example.demo.mapper.UserMapper"> <select id="findById" parameterType="int" resultType="com.example.demo.entity.User"> SELECT * FROM user WHERE id = #{id} </select> </mapper> ``` 6. 编写Service 创建Service类: ```java @Service public class UserService { @Autowired private UserMapper userMapper; public User findById(int id){ return userMapper.findById(id); } } ``` 7. 编写Controller 创建Controller类: ```java @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/{id}") public User findById(@PathVariable int id) { return userService.findById(id); } } ``` 当访问/user/{id}时,将通过UserService来获取User对象,并将其作为JSON响应返回。 以上就是Spring Boot整合MyBatis的步骤。简单易懂,且使用方便、灵活。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值