mybatis 如何使用通配符配置 mapper

springmvc+mybatis+mysql.我想配置mapper路径向下面这样。不用每次新建了一个 xml文件就添加一个。但是系统启动的时候报  Could not find resource cn/com/vobile/**/*_sqlmap_mysql.xml错误



怎么解决?


回答:

你的那个不能使用统配(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


你可能感兴趣的:(spring,mybatis)