Springboot+Mybatis中怎么实现typeAliasesPackage正则扫描

Springboot+Mybatis中怎么实现typeAliasesPackage正则扫描

本篇内容主要讲解“Springboot+Mybatis中怎么实现typeAliasesPackage正则扫描”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Springboot+Mybatis中怎么实现typeAliasesPackage正则扫描”吧!

Mybatis typeAliasesPackage正则扫描

mybatis默认配置typeAliasesPackage是不支持正则扫描package的,因此需要手动继承org.mybatis.spring.SqlSessionFactoryBean,自己实现正则扫描,方法和传统的spring+mybatis没什么区别,不同的是一个需要继承类一个是使用的扫描实现。

Springboot+Mybatis中怎么实现typeAliasesPackage正则扫描

对于两个或多个扫描路径,例:

cn.com.onethird.integration.entity

cn.com.onethird.business.entity

application.properties配置Mybatis 如下

mybatis.typeAliasesPackage=cn.com.onethird.*.*.entitymybatis.mapperLocations=classpath:mapper/*.xml

packagecn.com.onethird.nursinghome;importjava.io.IOException;importjava.util.ArrayList;importjava.util.HashSet;importjava.util.List;importjava.util.Properties;importjavax.sql.DataSource;importorg.apache.ibatis.session.SqlSessionFactory;importorg.mybatis.spring.SqlSessionFactoryBean;importorg.mybatis.spring.annotation.MapperScan;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.SpringApplication;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.context.annotation.PropertySource;importorg.springframework.core.env.Environment;importorg.springframework.core.io.Resource;importorg.springframework.core.io.support.PathMatchingResourcePatternResolver;importorg.springframework.core.io.support.ResourcePatternResolver;importorg.springframework.core.type.classreading.CachingMetadataReaderFactory;importorg.springframework.core.type.classreading.MetadataReader;importorg.springframework.core.type.classreading.MetadataReaderFactory;importorg.springframework.util.ClassUtils;importcom.Application;/****@Title:MyBatisConfig.java**@Package*@Description:mybtis实现正则扫描javabean包**@author*@date*@versionV1.0*/@Configuration@PropertySource("classpath:application.properties")publicclassMyBatisConfig{@AutowiredprivateEnvironmentenv;staticfinalStringDEFAULT_RESOURCE_PATTERN="**/*.class";publicstaticStringsetTypeAliasesPackage(StringtypeAliasesPackage){ResourcePatternResolverresolver=(ResourcePatternResolver)newPathMatchingResourcePatternResolver();MetadataReaderFactorymetadataReaderFactory=newCachingMetadataReaderFactory(resolver);typeAliasesPackage=ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX+ClassUtils.convertClassNameToResourcePath(typeAliasesPackage)+"/"+DEFAULT_RESOURCE_PATTERN;try{List<String>result=newArrayList<String>();Resource[]resources=resolver.getResources(typeAliasesPackage);if(resources!=null&&resources.length>0){MetadataReadermetadataReader=null;for(Resourceresource:resources){if(resource.isReadable()){metadataReader=metadataReaderFactory.getMetadataReader(resource);try{result.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage().getName());}catch(ClassNotFoundExceptione){e.printStackTrace();}}}}if(result.size()>0){HashSet<String>h=newHashSet<String>(result);result.clear();result.addAll(h);typeAliasesPackage=String.join(",",(String[])result.toArray(newString[0]));}else{thrownewRuntimeException("mybatistypeAliasesPackage路径扫描错误,参数typeAliasesPackage:"+typeAliasesPackage+"未找到任何包");}}catch(IOExceptione){e.printStackTrace();}returntypeAliasesPackage;}@BeanpublicSqlSessionFactorysqlSessionFactory(DataSourcedataSource)throwsException{System.out.println(">>>>>>>>>>>配置[typeAliasesPackage,mapperLocations]START>>>>>>>>>>>>>>");Propertiesprops=newProperties();StringtypeAliasesPackage=env.getProperty("mybatis.typeAliasesPackage");StringmapperLocations=env.getProperty("mybatis.mapperLocations");typeAliasesPackage=setTypeAliasesPackage(typeAliasesPackage);finalSqlSessionFactoryBeansessionFactory=newSqlSessionFactoryBean();sessionFactory.setDataSource(dataSource);sessionFactory.setTypeAliasesPackage(typeAliasesPackage);sessionFactory.setMapperLocations(newPathMatchingResourcePatternResolver().getResources(mapperLocations));System.out.println(">>>>>>>>>>>配置[typeAliasesPackage,mapperLocations]END>>>>>>>>>>>>>>");returnsessionFactory.getObject();}publicstaticvoidmain(String[]args)throwsException{setTypeAliasesPackage("cn.com.onethird.*.*.entity");}}

Mybatis 自定义扫描通配符typeAliasesPackage

typeAliasesPackage 默认只能扫描某一个路径下,或以逗号等分割的 几个路径下的内容,不支持通配符和正则,采用重写的方式解决

packagecom.xxxx.xxx.util.common;importcom.xxxx.xxx.util.LogUtil;importorg.apache.commons.lang3.StringUtils;importorg.mybatis.spring.SqlSessionFactoryBean;importorg.slf4j.Logger;importorg.springframework.core.io.Resource;importorg.springframework.core.io.support.PathMatchingResourcePatternResolver;importorg.springframework.core.io.support.ResourcePatternResolver;importorg.springframework.core.type.classreading.CachingMetadataReaderFactory;importorg.springframework.core.type.classreading.MetadataReader;importorg.springframework.core.type.classreading.MetadataReaderFactory;importorg.springframework.util.ClassUtils;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;/***CreatedbyAdministratoron2015/10/6.*/publicclassPackagesSqlSessionFactoryBeanextendsSqlSessionFactoryBean{staticfinalStringDEFAULT_RESOURCE_PATTERN="**/*.class";privatestaticLoggerlogger=LogUtil.get();@OverridepublicvoidsetTypeAliasesPackage(StringtypeAliasesPackage){ResourcePatternResolverresolver=(ResourcePatternResolver)newPathMatchingResourcePatternResolver();MetadataReaderFactorymetadataReaderFactory=newCachingMetadataReaderFactory(resolver);typeAliasesPackage=ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX+ClassUtils.convertClassNameToResourcePath(typeAliasesPackage)+"/"+DEFAULT_RESOURCE_PATTERN;//将加载多个绝对匹配的所有Resource//将首先通过ClassLoader.getResource("META-INF")加载非模式路径部分//然后进行遍历模式匹配try{List<String>result=newArrayList<String>();Resource[]resources=resolver.getResources(typeAliasesPackage);if(resources!=null&&resources.length>0){MetadataReadermetadataReader=null;for(Resourceresource:resources){if(resource.isReadable()){metadataReader=metadataReaderFactory.getMetadataReader(resource);try{result.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage().getName());}catch(ClassNotFoundExceptione){e.printStackTrace();}}}}if(result.size()>0){super.setTypeAliasesPackage(StringUtils.join(result.toArray(),","));}else{logger.warn("参数typeAliasesPackage:"+typeAliasesPackage+",未找到任何包");}//logger.info("d");}catch(IOExceptione){e.printStackTrace();}}}

<beanid="sqlSession"class="com.xxxx.xxxx.util.common.PackagesSqlSessionFactoryBean"><propertyname="configLocation"value="classpath:config/sqlmap/sqlmap-config.xml"/><propertyname="dataSource"ref="dataSource"/><!--<propertyname="mapperLocations"--><!--value="classpath*:com/xxxx/xxxx/merchant/**/domain/mapper/*.xml"/>--><propertyname="typeAliasesPackage"value="com.xxxx.xxxx.custom.*.domain"/><propertyname="plugins"><array><refbean="pageInterceptor"/></array></property></bean><beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer"><propertyname="basePackage"value="com.xxxx.xxxx.**.dao"/></bean>

到此,相信大家对“Springboot+Mybatis中怎么实现typeAliasesPackage正则扫描”有了更深的了解,不妨来实际操作一番吧!这里是恰卡编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

发布于 2022-04-03 22:32:52
收藏
分享
海报
0 条评论
35
上一篇:springBoot mybatis包扫描问题怎么解决 下一篇:Springboot怎么实现通用Auth认证
目录

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码