mybaits3.2.8 别名包扫描通配符
<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配置:
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="configLocation" value="classpath:/SqlMapConfig.xml"></property>
- <property name="mapperLocations" value="classpath*:/sqlmaps/**/*-sql.xml"></property>
- <property name="typeAliasesPackage" value="com.demo.domain" />
- </bean>
改造后:applicationContext.xml配置:
- <bean id="sqlSessionFactory" class="com.demo.core.mybatis.TQSqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="configLocation" value="classpath:/SqlMapConfig.xml"></property>
- <property name="mapperLocations" value="classpath*:/sqlmaps/**/*-sql.xml"></property>
- <property name="typeAliasesPackage" value="com.demo.**.domain" />
- </bean>
com.demo.core.mybatis.TQSqlSessionFactoryBean类源码:
- package com.demo.core.mybatis;
- import java.io.File;
- import java.io.IOException;
- import org.mybatis.spring.SqlSessionFactoryBean;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.core.io.Resource;
- import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
- import org.springframework.core.io.support.ResourcePatternResolver;
- import com.demo.core.utils.StringUtil;
- /**
- * @ClassName: TQSqlSessionFactoryBean
- * @Description: mybatis自动扫描别名路径(新增通配符匹配功能)
- * @author wangxiaohu wsmalltiger@163.com
- * @date 2014年12月9日 上午9:36:23
- */
- public class TQSqlSessionFactoryBean extends SqlSessionFactoryBean {
- Logger logger = LoggerFactory.getLogger(getClass());
- private static final String ROOT_PATH = "com" + File.separator + "demo"
- + File.separator;
- private static final String ROOT_PATH_SPLIT = ",";
- private static final String[] PATH_REPLACE_ARRAY = { "]" };
- public void setTypeAliasesPackage(String typeAliasesPackage) {
- if (!StringUtil.isStringAvaliable(typeAliasesPackage)) {
- super.setTypeAliasesPackage(typeAliasesPackage);
- return;
- }
- ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
- StringBuffer typeAliasesPackageStringBuffer = new StringBuffer();
- try {
- for (String location : typeAliasesPackage.split(",")) {
- if (!StringUtil.isStringAvaliable(location)) {
- continue;
- }
- location = "classpath*:"
- + location.trim().replace(".", File.separator);
- typeAliasesPackageStringBuffer.append(getResources(resolver,
- location));
- }
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- }
- if ("".equals(typeAliasesPackageStringBuffer.toString())) {
- throw new RuntimeException(
- "mybatis typeAliasesPackage 路径扫描错误!请检查applicationContext.xml@sqlSessionFactory配置!");
- }
- typeAliasesPackage = replaceResult(
- typeAliasesPackageStringBuffer.toString()).replace(
- File.separator, ".");
- super.setTypeAliasesPackage(typeAliasesPackage);
- }
- private String getResources(ResourcePatternResolver resolver,
- String location) throws IOException {
- StringBuffer resourcePathStringBuffer = new StringBuffer();
- for (Resource resource : resolver.getResources(location)) {
- String description = resource == null ? "" : resource
- .getDescription();
- if (!StringUtil.isStringAvaliable(resource.getDescription())
- || description.indexOf(ROOT_PATH) == -1) {
- continue;
- }
- resourcePathStringBuffer.append(
- description.substring(description.indexOf(ROOT_PATH)))
- .append(ROOT_PATH_SPLIT);
- }
- return resourcePathStringBuffer.toString();
- }
- private String replaceResult(String resultStr) {
- for (String replaceStr : PATH_REPLACE_ARRAY) {
- resultStr = resultStr.replace(replaceStr, "");
- }
- return resultStr;
- }
- }
题外话:
typeAliasesPackage配置路径下的domain中可以添加@org.apache.ibatis.type.Alias(value = "user")注解;如果添加此注解,则别名使用此注解所指定的名称。如果没有配置,则默认为类名首字母小写。
http://blog.csdn.net/wsmalltiger/article/details/41825375
mybaits3.2.8 别名包扫描通配符的更多相关文章
- Mybatis 自定义SqlSessionFactoryBean扫描通配符typeAliasesPackage
typeAliasesPackage 默认只能扫描某一个路径下,或以逗号等分割的 几个路径下的内容,不支持通配符和正则,采用重写的方式解决 package com.xxxx.xxx.util.comm ...
- 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 ...
- Mybatis从认识到了解
目录 MyBatis的介绍 介绍: 为什么选择MyBatis: 与Hibernate的对比: MyBatis的优点: 入门示例 Mybatis核心组件 四大核心组件 SqlSessionFactory ...
- Mybatis-spring 动态代理
1.UserMapper.java package com.cn.mapper; import java.util.List; import com.cn.pojo.User; public inte ...
- Mybatis-spring 传统dao开发
jdbc.properties jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis?chara ...
- ssm学习的第一个demo---crm(1)
这是一个普通的CRM项目 (第一步规划好项目设计路线:导入jar包→配置sqlMapConfig.xml(空文件)→配置applicationContext.xml →配置springMVC.xml→ ...
- SpringMVC+Spring+MyBatis 整合与图片上传简单示例
一.思路: (一) Dao层: 1. SqlMapConfig.xml,空文件即可.需要文件头.2. applicationContext_dao.xml. a) 数据库连接池b) SqlSessio ...
- Mybatis和Spring整合&逆向工程
Mybatis和Spring整合&逆向工程Mybatis和Spring整合mybatis整合Spring的思路目的就是将在SqlMapConfig.xml中的配置移植到Spring的appli ...
- SpringMVC-Mybatis整合和注解开发
SpringMVC-Mybatis整合和注解开发SpringMVC-Mybatis整合整合的思路在mybatis和spring整合的基础上 添加springmvc.spring要管理springmvc ...
随机推荐
- Asp.net操作Excel(终极方法NPOI)(转)
原文:Asp.net操作Excel(终极方法NPOI) 先去官网:http://npoi.codeplex.com/下载需要引入dll(可以选择.net2.0或者.net4.0的dll),然后在网站中 ...
- android 图片缩放抗锯齿
之前用的时候只设置了antialias属性,其实要设置两个flag才行 paint.setFlags(Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG); ...
- 【BZOJ】【1717】【USACO 2006 Dec】Milk Patterns产奶的模式
后缀数组 o(︶︿︶)o 唉傻逼了一下,忘了把后缀数组的字典范围改回20001,直接21交了上去,白白RE了两发……sigh 既然要找出现了K次的子串嘛,那当然要用后缀数组了>_>(因为我 ...
- Python爬取百度贴吧图片
一.获取URL Urllib 模块提供了读取web页面数据的接口,我们可以像读取本地文件一样读取www和ftp上的数据.首先,我们定义了一个getHtml()函数: urllib.urlopen()方 ...
- Java多线程程序设计详细解析
一.理解多线程 多线程是这样一种机制,它允许在程序中并发执行多个指令流,每个指令流都称为一个线程,彼此间互相独立. 线程又称为轻量级进程,它和进程一样拥有独立的执行控制,由操作系统负责调度,区别在于线 ...
- ASP.NET为图片加上水印
为我们发布的图片加上一个水印,也是我们经常要做的事情,那怎么样来做呢,下面就一步步开始吧 首先是一个制作水印的类:ImageHandler,代码如下: using System; using Syst ...
- ExtJS与jQuery的一点细节上的对比
首先说明这不是一篇完整解读ExtJS和jQuery所有方面差异的文章,只是针对我个人刚看了两天的jQuery产生的一些疑问的整理.之前用过一段时间ExtJS,了解ExtJS的一些机制.现在做移动开发, ...
- OpenLayers3 online build
openlayers3使用了一个比较复杂的build工具,从github上下载下来的代码中并没有build之后的版本,要配置build环境又比较繁琐,好在官方的example中提供了在线的版本,下面就 ...
- jQuery1.9.1源码分析--Events模块
var rformElems = /^(?:input|select|textarea)$/i, rkeyEvent = /^key/, rmouseEvent = /^(?:mouse|contex ...
- leetcode single number系列
这个系列一共有三题,第一题是一组数里除了一个数出现一次之外,其他数都是成对出现,求这个数. 第二题是一组数里除了两个数出现一次外,其他数都是成对出现,求这两个数 第三题是一组数里除了一个数出现一次外, ...