mybaits3.2.8 别名包扫描通配符

本文介绍了如何通过自定义MyBatis的TQSqlSessionFactoryBean来实现别名路径的通配符扫描,解决不同包下domain的自动别名配置问题。此外,还讨论了domain中@Alias注解的使用及其影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这几天搭建了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")注解;如果添加此注解,则别名使用此注解所指定的名称。如果没有配置,则默认为类名首字母小写。



此处记录!欢迎大家提出宝贵意见!
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

smatiger

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值