In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to realize typeAliasesPackage regular scanning in Springboot+Mybatis". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to achieve typeAliasesPackage regular scanning in Springboot+Mybatis".
Mybatis typeAliasesPackage regular scan
Mybatis default configuration typeAliasesPackage does not support regular scanning package, so you need to manually inherit org.mybatis.spring.SqlSessionFactoryBean and implement regular scanning by yourself. The method is no different from traditional spring+mybatis, except that one needs to inherit the class and the other is the scanning implementation that is used.
For two or more scan paths, for example:
Cn.com.onethird.integration.entity
Cn.com.onethird.business.entity
The application.properties configuration Mybatis is as follows mybatis.typeAliasesPackage=cn.com.onethird.*.*.entitymybatis.mapperLocations=classpath:mapper/*.xmlpackage cn.com.onethird.nursinghome; import java.io.IOException;import java.util.ArrayList;import java.util.HashSet;import java.util.List;import java.util.Properties; import javax.sql.DataSource; import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.annotation.MapperScan;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication Import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import org.springframework.core.env.Environment;import org.springframework.core.io.Resource;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.core.io.support.ResourcePatternResolver;import org.springframework.core.type.classreading.CachingMetadataReaderFactory;import org.springframework.core.type.classreading.MetadataReader;import org.springframework.core.type.classreading.MetadataReaderFactory;import org.springframework.util.ClassUtils Import com.Application; / * @ Title: MyBatisConfig.java * * @ Package * @ Description: mybtis implements regular scanning of the java bean package * * @ author * @ date * @ version V1.0 * / @ Configuration@PropertySource ("classpath:application.properties") public class MyBatisConfig {@ Autowired private Environment env; static final String DEFAULT_RESOURCE_PATTERN = "* * / * .class" Public static String setTypeAliasesPackage (String typeAliasesPackage) {ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver (); MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory (resolver); typeAliasesPackage = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath (typeAliasesPackage) + "/" + DEFAULT_RESOURCE_PATTERN; try {List result = new ArrayList () Resource [] resources = resolver.getResources (typeAliasesPackage); if (resources! = null & & resources.length > 0) {MetadataReader metadataReader = null; for (Resource resource: resources) {if (resource.isReadable ()) {metadataReader = metadataReaderFactory .getMetadataReader (resource) Try {result.add (Class .forName (metadataReader.getClassMetadata (). GetClassName ()) .getPackage (). GetName ());} catch (ClassNotFoundException e) {e.printStackTrace () } if (result.size () > 0) {HashSet h = new HashSet (result); result.clear (); result.addAll (h) TypeAliasesPackage = String.join (",", (String []) result.toArray (new String [0]));} else {throw new RuntimeException ("mybatis typeAliasesPackage path scan error, parameter typeAliasesPackage:" + typeAliasesPackage + "No packets found");}} catch (IOException e) {e.printStackTrace () } return typeAliasesPackage;} @ Bean public SqlSessionFactory sqlSessionFactory (DataSource dataSource) throws Exception {System.out.println ("> configure [typeAliasesPackage,mapperLocations] START >"); Properties props = new Properties (); String typeAliasesPackage= env.getProperty ("mybatis.typeAliasesPackage"); String mapperLocations = env.getProperty ("mybatis.mapperLocations"); typeAliasesPackage=setTypeAliasesPackage (typeAliasesPackage) Final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean (); sessionFactory.setDataSource (dataSource); sessionFactory.setTypeAliasesPackage (typeAliasesPackage); sessionFactory.setMapperLocations (new PathMatchingResourcePatternResolver (). GetResources (mapperLocations)); System.out.println ("> configure [typeAliasesPackage,mapperLocations] END >"); return sessionFactory.getObject ();} public static void main (String [] args) throws Exception {setTypeAliasesPackage ("cn.com.onethird.*.*.entity") }} Mybatis Custom scan wildcard typeAliasesPackage
By default, typeAliasesPackage can only scan the content under a certain path or under several paths separated by commas, and does not support wildcards and regularities. It can be solved by rewriting.
Package com.xxxx.xxx.util.common; import com.xxxx.xxx.util.LogUtil; import org.apache.commons.lang3.StringUtils; import org.mybatis.spring.SqlSessionFactoryBean; import org.slf4j.Logger; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.core.type.classreading.CachingMetadataReaderFactory; import org.springframework.core.type.classreading.MetadataReader Import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.util.ClassUtils; import java.io.IOException; import java.util.ArrayList; import java.util.List; / * Created by Administrator on 2015-10-6. * / public class PackagesSqlSessionFactoryBean extends SqlSessionFactoryBean {static final String DEFAULT_RESOURCE_PATTERN = "* * / * .class"; private static Logger logger = LogUtil.get () @ Override public void setTypeAliasesPackage (String typeAliasesPackage) {ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver (); MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory (resolver); typeAliasesPackage = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath (typeAliasesPackage) + "/" + DEFAULT_RESOURCE_PATTERN / / all Resource with multiple absolute matches will be loaded / / the part of the non-pattern path will be loaded first via ClassLoader.getResource ("META-INF") / / and then traversing the pattern matching try {List result = new ArrayList (); Resource [] resources = resolver.getResources (typeAliasesPackage) If (resources! = null & & resources.length > 0) {MetadataReader metadataReader = null; for (Resource resource: resources) {if (resource.isReadable ()) {metadataReader = metadataReaderFactory.getMetadataReader (resource) Try {result.add (Class.forName (metadataReader.getClassMetadata (). GetClassName ()). GetPackage (). GetName ());} catch (ClassNotFoundException e) {e.printStackTrace () }} if (result.size () > 0) {super.setTypeAliasesPackage (StringUtils.join (result.toArray (), ",")) } else {logger.warn ("parameter typeAliasesPackage:" + typeAliasesPackage+ ", no package found");} / / logger.info ("d");} catch (IOException e) {e.printStackTrace () At this point, I believe you have a deeper understanding of "how to achieve typeAliasesPackage regular scanning in Springboot+Mybatis". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.