首页 > 代码库 > mybaits3.2.8 别名包扫描通配符
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配置:
[html] view plain copy
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="configLocation" value=http://www.mamicode.com/"classpath:/SqlMapConfig.xml"></property>
- <property name="mapperLocations" value=http://www.mamicode.com/"classpath*:/sqlmaps/**/*-sql.xml"></property>
- <property name="typeAliasesPackage" value=http://www.mamicode.com/"com.demo.domain" />
- </bean>
改造后:applicationContext.xml配置:
[html] view plain copy
- <bean id="sqlSessionFactory" class="com.demo.core.mybatis.TQSqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="configLocation" value=http://www.mamicode.com/"classpath:/SqlMapConfig.xml"></property>
- <property name="mapperLocations" value=http://www.mamicode.com/"classpath*:/sqlmaps/**/*-sql.xml"></property>
- <property name="typeAliasesPackage" value=http://www.mamicode.com/"com.demo.**.domain" />
- </bean>
com.demo.core.mybatis.TQSqlSessionFactoryBean类源码:
[java] view plain copy
- 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 = "http://www.mamicode.com/user")注解;如果添加此注解,则别名使用此注解所指定的名称。如果没有配置,则默认为类名首字母小写。
http://blog.csdn.net/wsmalltiger/article/details/41825375
mybaits3.2.8 别名包扫描通配符
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。