In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to solve the Mybatis entity alias support wildcard scanning problem, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Introduction to Mybatis
MyBatis is an excellent persistence layer framework that supports customized SQL, stored procedures, and advanced mapping.
MyBatis avoids almost all JDBC code and manually setting parameters and getting result sets.
MyBatis can use simple XML or annotations to configure and map native information, mapping interfaces and Java's POJOs (Plain Old Java Objects, ordinary Java objects) to records in the database.
problem
In a project where Spring integrates Mybatis, you can alias the entity under the specified package, so that the full path name of the entity class can be omitted in the Mapper xml file, and only the class name can be written. However, in a multi-module project, you may need to put the entity under different modules, so you need to configure multipath scanning.
Method 1: configure multiple paths separated by comma separators
Classpath*:mybatis/mapper/*.xml
Method 2: use wildcard configuration, which is not supported by default. You need to rewrite SqlSessionFactoryBean to support wildcard scanning.
Package com.dongzz.cms.common.plugin.mybatis;import org.apache.commons.lang3.StringUtils;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 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.HashSet;import java.util.Set;/** * Custom SqlSessionFactoryBean * / public class SqlSessionFactoryBean extends org.mybatis.spring.SqlSessionFactoryBean {public static final Logger logger = LoggerFactory.getLogger (SqlSessionFactoryBean.class); public static final String DEFAULT_RESOURCE_PATTERN = "* * / * .class" / / Alias wildcard / * rewrite entity alias extension supports wildcard * * @ param typeAliasesPackage format: com.dongzz.cms.modules.**.entity * / @ Override public void setTypeAliasesPackage (String typeAliasesPackage) {/ / Resource path parser ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver (); / / metadata read MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory (resolver) / / Resolution path typeAliasesPackage = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath (typeAliasesPackage) + "/" + DEFAULT_RESOURCE_PATTERN; logger.debug ("alias package wildcard:" + typeAliasesPackage); try {Set result = new HashSet (); / / alias package path collection Resource [] resources = resolver.getResources (typeAliasesPackage) / / read all class resources if (resources! = null & & resources.length > 0) {MetadataReader metadataReader = null; for (Resource resource: resources) {if (resource.isReadable ()) {metadataReader = metadataReaderFactory.getMetadataReader (resource) according to the path / / read the information of the class. Each Resource is a class resource try {result.add (Class.forName (metadataReader.getClassMetadata (). GetClassName ()). GetPackage (). GetName ()); / / the package path corresponding to the storage class} catch (ClassNotFoundException e) {e.printStackTrace () } if (result.size () > 0) {logger.debug ("alias package path collection:" + StringUtils.join (result.toArray (), ","); super.setTypeAliasesPackage (StringUtils.join (result.toArray (), ",") } else {logger.warn ("Parameter typeAliasesPackage:" + typeAliasesPackage + ", not found any packages.");}} catch (IOException e) {e.printStackTrace ();}
Xml configuration:
Classpath*:mybatis/mapper/*.xml
Mybatis supports wildcard alias scanning through custom SqlSessionFactoryBean, so that in multi-module projects, the scanning path needs to be configured only once.
The above is all the contents of the article "how to solve the problem of Mybatis entity aliases supporting wildcard scanning". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.