Springboot+Mybatis中怎么实现typeAliasesPackage正则扫描
Springboot+Mybatis中怎么实现typeAliasesPackage正则扫描
本篇内容主要讲解“Springboot+Mybatis中怎么实现typeAliasesPackage正则扫描”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Springboot+Mybatis中怎么实现typeAliasesPackage正则扫描”吧!
Mybatis typeAliasesPackage正则扫描
mybatis默认配置typeAliasesPackage是不支持正则扫描package的,因此需要手动继承org.mybatis.spring.SqlSessionFactoryBean,自己实现正则扫描,方法和传统的spring+mybatis没什么区别,不同的是一个需要继承类一个是使用的扫描实现。
对于两个或多个扫描路径,例:
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正则扫描”有了更深的了解,不妨来实际操作一番吧!这里是恰卡编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
推荐阅读
-
mybatis如何编写一个自定义插件(mybatis plus优点)
mybatisplus优点?Mybatis-Plus是一个Mybatis的增强工具,只是在Mybatis的基础上做了增强却不做改...
-
vue动态添加删除输入框(springboot vue怎么让数据库显示出来)
springbootvue怎么让数据库显示出来?一般情况下是前端调阅后端接口,来获取到数据库的数据,后端哪里会把数据库的数据整理...
-
springboot实现基于aop的切面日志
本文实例为大家分享了springboot实现基于aop的切面日志的具体代码,供大家参考,具体内容如下通过aop的切面方式实现日志...
-
SpringBoot定时任务功能怎么实现
-
SpringBoot中的@Import注解怎么使用
-
SpringBoot整合Lombok及常见问题怎么解决
-
MyBatis和jeesite多表查询的方法
MyBatis和jeesite多表查询的方法这篇文章主要介绍了My...
-
Mybatis怎么实现ResultMap结果集
-
springboot图片验证码功能模块怎么实现
-
Springboot+SpringSecurity怎么实现图片验证码登录