mybatis 如何使用通配符配置 mapper

本文介绍如何在Spring MVC + MyBatis项目中正确配置Mapper路径,避免使用时需要为每个XML文件单独添加配置的问题,并提供了一种通配符方式来批量加载所有Mapper文件。
springmvc+mybatis+mysql.我想配置mapper路径向下面这样。不用每次新建了一个 xml文件就添加一个。但是系统启动的时候报  Could not find resource cn/com/vobile/**/*_sqlmap_mysql.xml错误
<mappers>
<mapper resource="cn/com/vobile/**/*_sqlmap_mysql.xml" />
</mappers>

怎么解决?


回答:

你的那个不能使用统配(ant匹配模式)。

例子:文档

XML/HTML code
?
1
2
3
4
< bean  id = "sqlSessionFactory"  class = "org.mybatis.spring.SqlSessionFactoryBean" >
   < property  name = "dataSource"  ref = "dataSource"  />
   < property  name = "mapperLocations"  value = "classpath*:sample/config/mappers/**/*.xml"  />
</ bean >
关于通用匹配是spring提供的ant匹配法,mybatis 没有这个功能。具体实现见: AntPathMatcher

例子如下:


public class AntPathMatcher
extends Object
implements PathMatcher
PathMatcher implementation for Ant-style path patterns.

Part of this mapping code has been kindly borrowed from Apache Ant.

The mapping matches URLs using the following rules:

  • ? matches one character
  • * matches zero or more characters
  • ** matches zero or more directories in a path
  • {spring:[a-z]+} matches the regexp [a-z]+ as a path variable named "spring"

Examples

  • com/t?st.jsp — matches com/test.jsp but also com/tast.jsp or com/txst.jsp
  • com/*.jsp — matches all .jsp files in the com directory
  • com/**/test.jsp — matches all test.jsp files underneath the com path
  • org/springframework/**/*.jsp — matches all .jsp files underneath the org/springframework path
  • org/**/servlet/bla.jsp — matches org/springframework/servlet/bla.jsp but also org/springframework/testing/servlet/bla.jsp and org/servlet/bla.jsp
  • com/{filename:\\w+}.jsp will match com/test.jsp and assign the value test to the filename variable


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值