软件编程
位置:首页>> 软件编程>> java编程>> mybatis中mapper-locations的作用

mybatis中mapper-locations的作用

作者:还能坚持  发布时间:2023-11-10 18:02:58 

标签:mybatis,mapper-locations

mapper-locations的作用说明

1、mapper-locations

mapper-locations是一个定义mapper接口位置的属性,在xxx.yml或xxx.properties下配置,作用是实现mapper接口配置

2、使用场景

当mapper接口和mapper接口对应的配置文件在 命名上相同 、所在的路径相同,则mapper-locations可以不用配置,配置也不会生效。

mybatis中mapper-locations的作用

如果mapper接口和mapper接口对应的配置文件在命名上不同或所在的路径不同,配置mapper-locations才能实现接口的绑定。实现mapper接口的绑定需要在配置文件中配置:

mybatis.mapper-locations=classpath:mapper/*Mapper.xml
  • mapper接口:com.liuzeyu.mapper.UserMapper.java

  • mapper接口对应的配置文件:mapper.UserMapper.xml

mybatis中mapper-locations的作用

3、补充

实现接口绑定还可以在mapper接口上,直接使用注解实现,类似:

mybatis中mapper-locations的作用

mybatis.mapper-locations作用和Invalid bound statement (not found)错误

mybatis.mapper-locations作用:加载我们的mapper xml文件,用于解决当我们的mapper映射文件所在位置不同于mapper接口所在位置,如果位置相同就可以忽略这条代码

对应错误:mapper.xml没被加载到

环境:SpringBoot+Mybatis+MySQL

环境准备:

  1. 创建springboot 的demo

  2. 引入依赖和插件

  3. 逆向工程生成相应的pojo,mapper接口,mapper映射xml文件

  4. 逆向工程生成相应的pojo,mapper接口,mapper映射xml文件

  5. 创建controller层

以上1-5层,目录截图,期中 resources下的mybatis文件夹只是我用来存放逆向工程的配置文件(忽略) 

mybatis中mapper-locations的作用

编写application.properties

server.port=8080
server.servlet.context-path=/demo
spring.datasource.username=root
spring.datasource.password=
spring.datasource.url=jdbc:mysql://localhost:3307/studentdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.profiles.active=dev
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
#mybatis.mapper-locations=classpath:com/example/demo/mapper1/*.xml
mybatis.type-aliases-package=com.example.demo.pojo
logging.level.com.example.demo.mapper=debug
logging.level.web=debug

注意:此时没有启用#mybatis.mapper-locations

接下来修改DemoApplication.java

mybatis中mapper-locations的作用

在@SpringBootApplication 后面加注解 

@MapperScan(“com.example.demo.mapper”)

扫描mybatis的相关文件,此时的作用是加载"com.example.demo.mapper"下的mapper接口,如果对应的mapper映射文件恰好所在的目录是在resources下的"com.example.demo.mapper",那么xml映射文件也会被

加载到配置中,此时mapper接口和xml文件恰好同放在"com.example.demo.mapper"中

mybatis中mapper-locations的作用

在controller层编写controller调用 mapper 访问数据库查询信息

@Controller
@RequestMapping("student")
public class StudentController {
   @Autowired
   StudentMapper studentMapper;
   @RequestMapping("/listStudents")
   public String getStudentList(@RequestParam(defaultValue = "1",value = "page")int page, HttpServletRequest request){
       List<Student> list = studentMapper.selectByExample(new StudentExample());
       request.setAttribute("list",list);
       return "student/listStudents";
   }
}

运行,显示成功

mybatis中mapper-locations的作用

如果我们的mapper接口和mapper映射文件不在相同的目录下 该案例同为"com.example.demo.mapper",则xml映射文件不能被加载到

我们把xml映射文件所在的文件夹改为mapper1

mybatis中mapper-locations的作用

然后重启项目,访问地址,报错

mybatis中mapper-locations的作用

  • Failed to complete request: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

  • org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.example.demo.mapper.StudentMapper.selectByExample

由于xml所在路径不同于mapper接口的所在路径,所以xml映射文件没被读取到,调用的时候就自然报错,解决路径不同的读取方法:

mybatis.mapper-locations

回到application.properties,我们加入一行:

mybatis.mapper-locations=classpath:com/example/demo/mapper1/*.xml

(我自己打开注释) 

这一行代码的作用是加载我们的mapper xml文件,用于解决当我们的mapper映射文件所在位置不同于mapper接口所在位置,如果位置相同就可以忽略这条代码

指定了xml的新位置(com/example/demo/mapper1)后,重启再次访问就成功了, 

注意resources下的目录路径是以"/&ldquo;为分隔,而不是.,java下的才是&rdquo;."

来源:https://blog.csdn.net/qq_35091353/article/details/119802245

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com