<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

最新文章

  1. XML理解
  2. Git 执行 「fork 出来的仓库」和「最新版本的原仓库」内容同步更新
  3. smarty变量调节器案例
  4. HTML5拖拽功能drag
  5. BZOJ_1833_[ZJOI2010]_数字计数_(数位dp)
  6. (转) 新手入门:C/C++中的结构体
  7. Codeforces Round #411 (Div. 2)
  8. antlr v4 使用指南连载1——简介
  9. leetcode每日刷题计划-简单篇day4
  10. [PHP]命名空间的一些要点
  11. android Glide简单使用
  12. 设置弹窗、遮罩的样式设置(包括:left、heigh等)
  13. shell seq 用法
  14. 什么是EOS(不一样的角度看柚子)
  15. 让你的ASP.NET虚拟主机也支持子网站
  16. C++快速输入输出优化
  17. test20181006 投票
  18. iOS Swift WisdomKeyboardKing 键盘智能管家SDK
  19. 按“块”的方式写dom以及代码注释
  20. [osg]osgDB的加载机制,使用3DS插件做参考(转,整理现有osgDB资料)

热门文章

  1. “我爱淘”第二冲刺阶段Scrum站立会议1
  2. Eclipse的python插件安装
  3. mysql的简单主从复制(ubuntu)
  4. android 自动化压力测试-monkey 1 实践
  5. HDU 5593 ZYB&#39;s Tree 树形dp
  6. 【BZOJ】【3156】防御准备
  7. 【HDOJ】【3068】最长回文
  8. git学习——&lt;一&gt;git安装
  9. Android 添加Button事件
  10. 解决Notice错误,性能竟然提升了1000多倍!