<mybatis.version>3.2.8</mybatis.version>
<mybatis.spring.version>1.2.2</mybatis.spring.version>
<mybatis.generator.version>1.3.2</mybatis.generator.version>

MBG

http://www.mybatis.org/spring/apidocs/reference/org/mybatis/spring/SqlSessionFactoryBean.html

这几天搭建了spring4.1.2+mybatis3.2.8一个简单的框架。

发现mybatis的SqlSessionFactoryBean可以配置typeAliasesPackage属性,自动为domain起别名。

如果我的domain在不同包下面,那么这个配置不支持通配符扫描包路径?如下改造:

改造前:applicationContext.xml配置:

  1. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  2. <property name="dataSource" ref="dataSource" />
  3. <property name="configLocation" value="classpath:/SqlMapConfig.xml"></property>
  4. <property name="mapperLocations" value="classpath*:/sqlmaps/**/*-sql.xml"></property>
  5. <property name="typeAliasesPackage" value="com.demo.domain" />
  6. </bean>

改造后:applicationContext.xml配置:

  1. <bean id="sqlSessionFactory" class="com.demo.core.mybatis.TQSqlSessionFactoryBean">
  2. <property name="dataSource" ref="dataSource" />
  3. <property name="configLocation" value="classpath:/SqlMapConfig.xml"></property>
  4. <property name="mapperLocations" value="classpath*:/sqlmaps/**/*-sql.xml"></property>
  5. <property name="typeAliasesPackage" value="com.demo.**.domain" />
  6. </bean>

com.demo.core.mybatis.TQSqlSessionFactoryBean类源码:

  1. package com.demo.core.mybatis;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import org.mybatis.spring.SqlSessionFactoryBean;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import org.springframework.core.io.Resource;
  8. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  9. import org.springframework.core.io.support.ResourcePatternResolver;
  10. import com.demo.core.utils.StringUtil;
  11. /**
  12. * @ClassName: TQSqlSessionFactoryBean
  13. * @Description: mybatis自动扫描别名路径(新增通配符匹配功能)
  14. * @author wangxiaohu wsmalltiger@163.com
  15. * @date 2014年12月9日 上午9:36:23
  16. */
  17. public class TQSqlSessionFactoryBean extends SqlSessionFactoryBean {
  18. Logger logger = LoggerFactory.getLogger(getClass());
  19. private static final String ROOT_PATH = "com" + File.separator + "demo"
  20. + File.separator;
  21. private static final String ROOT_PATH_SPLIT = ",";
  22. private static final String[] PATH_REPLACE_ARRAY = { "]" };
  23. public void setTypeAliasesPackage(String typeAliasesPackage) {
  24. if (!StringUtil.isStringAvaliable(typeAliasesPackage)) {
  25. super.setTypeAliasesPackage(typeAliasesPackage);
  26. return;
  27. }
  28. ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
  29. StringBuffer typeAliasesPackageStringBuffer = new StringBuffer();
  30. try {
  31. for (String location : typeAliasesPackage.split(",")) {
  32. if (!StringUtil.isStringAvaliable(location)) {
  33. continue;
  34. }
  35. location = "classpath*:"
  36. + location.trim().replace(".", File.separator);
  37. typeAliasesPackageStringBuffer.append(getResources(resolver,
  38. location));
  39. }
  40. } catch (IOException e) {
  41. logger.error(e.getMessage(), e);
  42. }
  43. if ("".equals(typeAliasesPackageStringBuffer.toString())) {
  44. throw new RuntimeException(
  45. "mybatis typeAliasesPackage 路径扫描错误!请检查applicationContext.xml@sqlSessionFactory配置!");
  46. }
  47. typeAliasesPackage = replaceResult(
  48. typeAliasesPackageStringBuffer.toString()).replace(
  49. File.separator, ".");
  50. super.setTypeAliasesPackage(typeAliasesPackage);
  51. }
  52. private String getResources(ResourcePatternResolver resolver,
  53. String location) throws IOException {
  54. StringBuffer resourcePathStringBuffer = new StringBuffer();
  55. for (Resource resource : resolver.getResources(location)) {
  56. String description = resource == null ? "" : resource
  57. .getDescription();
  58. if (!StringUtil.isStringAvaliable(resource.getDescription())
  59. || description.indexOf(ROOT_PATH) == -1) {
  60. continue;
  61. }
  62. resourcePathStringBuffer.append(
  63. description.substring(description.indexOf(ROOT_PATH)))
  64. .append(ROOT_PATH_SPLIT);
  65. }
  66. return resourcePathStringBuffer.toString();
  67. }
  68. private String replaceResult(String resultStr) {
  69. for (String replaceStr : PATH_REPLACE_ARRAY) {
  70. resultStr = resultStr.replace(replaceStr, "");
  71. }
  72. return resultStr;
  73. }
  74. }

题外话:

typeAliasesPackage配置路径下的domain中可以添加@org.apache.ibatis.type.Alias(value = "user")注解;如果添加此注解,则别名使用此注解所指定的名称。如果没有配置,则默认为类名首字母小写。

http://blog.csdn.net/wsmalltiger/article/details/41825375

mybaits3.2.8 别名包扫描通配符的更多相关文章

  1. Mybatis 自定义SqlSessionFactoryBean扫描通配符typeAliasesPackage

    typeAliasesPackage 默认只能扫描某一个路径下,或以逗号等分割的 几个路径下的内容,不支持通配符和正则,采用重写的方式解决 package com.xxxx.xxx.util.comm ...

  2. spring整合mybatis错误:class path resource [config/spring/springmvc.xml] cannot be opened because it does not exist

    spring 整合Mybatis 运行环境:jdk1.7.0_17+tomcat 7 + spring:3.2.0 +mybatis:3.2.7+ eclipse 错误:class path reso ...

  3. Mybatis从认识到了解

    目录 MyBatis的介绍 介绍: 为什么选择MyBatis: 与Hibernate的对比: MyBatis的优点: 入门示例 Mybatis核心组件 四大核心组件 SqlSessionFactory ...

  4. Mybatis-spring 动态代理

    1.UserMapper.java package com.cn.mapper; import java.util.List; import com.cn.pojo.User; public inte ...

  5. Mybatis-spring 传统dao开发

    jdbc.properties jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis?chara ...

  6. ssm学习的第一个demo---crm(1)

    这是一个普通的CRM项目 (第一步规划好项目设计路线:导入jar包→配置sqlMapConfig.xml(空文件)→配置applicationContext.xml →配置springMVC.xml→ ...

  7. SpringMVC+Spring+MyBatis 整合与图片上传简单示例

    一.思路: (一) Dao层: 1. SqlMapConfig.xml,空文件即可.需要文件头.2. applicationContext_dao.xml. a) 数据库连接池b) SqlSessio ...

  8. Mybatis和Spring整合&逆向工程

    Mybatis和Spring整合&逆向工程Mybatis和Spring整合mybatis整合Spring的思路目的就是将在SqlMapConfig.xml中的配置移植到Spring的appli ...

  9. SpringMVC-Mybatis整合和注解开发

    SpringMVC-Mybatis整合和注解开发SpringMVC-Mybatis整合整合的思路在mybatis和spring整合的基础上 添加springmvc.spring要管理springmvc ...

随机推荐

  1. 使用usb+preseed在virtualbox上安装ubuntu(一)

    1.制作usb boot盘,在ubuntu上使用startup disk creater将ubuntu-server12.04.iso写入到usb中: 2.修改syslinux文件夹中的syslinu ...

  2. Linux设置:环境变量

    一.在相关配置文件中添加可执行文件/头文件/静态库/动态库的搜索路径 对所有用户有效在/etc/profile添加. 只对当前用户有效在Home目录下的.bashrc或.bash_profile里添加 ...

  3. Android基础整理之四大组件Activity

    最近准备系统的重新整理复习一下Android的各方面的知识,本着知识分享的原则,我就把梳理过程中一些东西给记录下来,权当一个学习笔记吧. 下面步入正题..... 什么是Activity Activit ...

  4. win8.1 cygwin编译java轻量虚拟机avian

    1.背景 昨天在网上看到别人用aauto写本地小程序写的很爽,我觉得如果java的jre能小一点,凭借java庞大的第三方类库写小工具也还算不错的.本人就经常用eclipse+一些commons包写些 ...

  5. 【BZOJ】【1855】【SCOI2010】/【HDOJ】【3401】股票交易

    DP/单调队列优化 题解:http://www.cnblogs.com/jianglangcaijin/p/3799736.html 令f[i][j]表示第 i 天结束后,手里剩下 j 股的最大利润, ...

  6. 树分治&树链剖分相关题目讨论

    预备知识 树分治,树链剖分   poj1741 •一棵有n个节点的树,节点之间的边有长度.方方方想知道,有多少个点对距离不超过m 题解 点分治模板题.详见我早上写的http://www.cnblogs ...

  7. Eclipse新版 syso无法自动补全的解决方法

    症状: 以前输入Syso可以直接自动转化为System.out.println(""); 现如今居然还要让我手动选择一下才可以! 我仔细看了一下Eclipse的插件,发现是新版Ec ...

  8. javascript设计模式-生成器模式(Builder)

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Sqli-labs less 48

    Less-48 本关与less-46的区别在于报错注入不能使用,不进行错误回显,因此其他的方法我们依旧是可以使用的. 可以利用sort=rand(true/false)进行判断. http://127 ...

  10. Android fill_parent、wrap_content和match_parent的区别

    三个属性都用来适应视图的水平或垂直大小,一个以视图的内容或尺寸为基础的布局比精确地指定视图范围更加方便. 1)fill_parent 设置一个构件的布局为fill_parent将强制性地使构件扩展,以 ...