springboot mybatis mapper interface AppsDmMapper extends AppsMapper 怎么实现mapperxml怎么处理
在 Spring Boot 中使用 MyBatis,可以通过注解方式或者 XML 文件方式来实现 Mapper 接口和 Mapper XML 的处理。
-
注解方式:
- 创建一个
AppsDmMapper接口,继承自AppsMapper接口。
@Mapper public interface AppsDmMapper extends AppsMapper { // 可以在这里添加自定义的方法 }- 在
@MapperScan注解中扫描该接口所在的包,以将其注册为 Mapper 接口。
@SpringBootApplication @MapperScan("com.example.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }- 在需要使用
AppsDmMapper的地方,通过@Autowired进行注入使用即可。
- 创建一个
-
XML 文件方式:
- 创建一个
AppsDmMapper接口,继承自AppsMapper接口。
public interface AppsDmMapper extends AppsMapper { // 可以在这里添加自定义的方法 }- 创建一个与
AppsDmMapper对应的 XML 文件AppsDmMapper.xml,并在其中编写 SQL 语句。
<!-- AppsDmMapper.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.mapper.AppsDmMapper"> <!-- 可以在这里编写 SQL 语句 --> </mapper>- 在
application.properties或application.yml中配置 MyBatis 的 XML 文件路径。
# application.properties mybatis.mapper-locations=classpath*:com/example/mapper/*.xml# application.yml mybatis: mapper-locations: classpath*:com/example/mapper/*.xml- 在需要使用
AppsDmMapper的地方,通过@Autowired进行注入使用即可。
- 创建一个
以上是在 Spring Boot 中使用 MyBatis 的一种实现方式,具体的使用方式可以根据项目的需求进行调整。
AI 优化版
原文地址:https://www.cveoy.top/t/topic/i11j 著作权归作者所有。请勿转载和采集!