mybaits3.2.8 别名包扫描通配符
Posted 沧海一滴
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 [email protected]
- * @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 路径扫描错误!请检查[email protected]配置!");
- }
- 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 别名包扫描通配符的主要内容,如果未能解决你的问题,请参考以下文章
8.1 shell介绍8.2 命令历史8.3 命令补全和别名8.4 通配符8.5 输入输出重定向