springboot 使依赖中的mybatis.XML 和 dao类生效

springboot Notes

在项目中可能面临着这样的需求,开发第三方依赖,实现log.error日志 or other method 的拦截,并向数据库中插入出错的消息记录

本着解耦的原则,动最少的旧项目代码。因此,需要我们在第三方工程中编写mybatis的xml文件 和 mapper 接口将拦截的数据插入数据库中

此时要向在旧项目中加入如下配置使该第三方工程依赖中的mybatis 配置文件和dao类生效

旧项目启动类添加如下注解

该注解会替换mybatis的默认dao类扫描方式 ,默认的扫描方式只扫描本项目中mybatis/*.xml中dao类

修改mybatis全局配置

根据自己jar中的路径进行配置

路径不同时,添加多个路径方式如下

mapper-locations:classpath:mybatis/*Mapper.xml, classpath*:config or other/*Mapper.xml
复制代码

转载于:https://juejin.im/post/5c2dd15c5188257ed57eb884

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot集成MyBatis,可以通过以下步骤实现: 1. 添加MyBatisMyBatis-Spring-Boot-Starter依赖: ``` <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.5</version> </dependency> ``` 2. 配置数据源 在application.yml文件配置数据源,例如: ``` spring: datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver ``` 3. 配置MyBatis 在application.yml文件配置MyBatis,例如: ``` mybatis: mapper-locations: classpath:mapper/*.xml ``` 4. 编写Mapper接口 编写Mapper接口,例如: ``` @Mapper public interface UserMapper { User getUserById(Long id); } ``` 5. 编写Mapper XML文件 编写Mapper XML文件,例如: ``` <mapper namespace="com.example.demo.mapper.UserMapper"> <select id="getUserById" parameterType="java.lang.Long" resultType="com.example.demo.model.User"> select * from user where id = #{id} </select> </mapper> ``` 6. 注入Mapper 在需要使用Mapper的地方注入Mapper,例如: ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User getUserById(Long id) { return userMapper.getUserById(id); } } ``` 通过以上步骤,就可以在Spring Boot集成MyBatis了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值