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