春风项目四线(合箱线、总装线)
yyt
2024-06-06 45f4602070d7219fe00b098b6c0471ca42a8553f
提交 | 用户 | 时间
fd2207 1 package com.jcdm.framework.config;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.HashSet;
7 import java.util.List;
8 import javax.sql.DataSource;
9 import org.apache.ibatis.io.VFS;
10 import org.apache.ibatis.session.SqlSessionFactory;
11 import org.mybatis.spring.SqlSessionFactoryBean;
12 import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
13 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.context.annotation.Bean;
15 import org.springframework.context.annotation.Configuration;
16 import org.springframework.core.env.Environment;
17 import org.springframework.core.io.DefaultResourceLoader;
18 import org.springframework.core.io.Resource;
19 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
20 import org.springframework.core.io.support.ResourcePatternResolver;
21 import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
22 import org.springframework.core.type.classreading.MetadataReader;
23 import org.springframework.core.type.classreading.MetadataReaderFactory;
24 import org.springframework.util.ClassUtils;
25 import com.jcdm.common.utils.StringUtils;
26
27 /**
28  * Mybatis支持*匹配扫描包
29  * 
30  * @author jc
31  */
32 @Configuration
33 public class MyBatisConfig
34 {
35     @Autowired
36     private Environment env;
37
38     static final String DEFAULT_RESOURCE_PATTERN = "**/*.class";
39
40     public static String setTypeAliasesPackage(String typeAliasesPackage)
41     {
42         ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver();
43         MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver);
44         List<String> allResult = new ArrayList<String>();
45         try
46         {
47             for (String aliasesPackage : typeAliasesPackage.split(","))
48             {
49                 List<String> result = new ArrayList<String>();
50                 aliasesPackage = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
51                         + ClassUtils.convertClassNameToResourcePath(aliasesPackage.trim()) + "/" + DEFAULT_RESOURCE_PATTERN;
52                 Resource[] resources = resolver.getResources(aliasesPackage);
53                 if (resources != null && resources.length > 0)
54                 {
55                     MetadataReader metadataReader = null;
56                     for (Resource resource : resources)
57                     {
58                         if (resource.isReadable())
59                         {
60                             metadataReader = metadataReaderFactory.getMetadataReader(resource);
61                             try
62                             {
63                                 result.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage().getName());
64                             }
65                             catch (ClassNotFoundException e)
66                             {
67                                 e.printStackTrace();
68                             }
69                         }
70                     }
71                 }
72                 if (result.size() > 0)
73                 {
74                     HashSet<String> hashResult = new HashSet<String>(result);
75                     allResult.addAll(hashResult);
76                 }
77             }
78             if (allResult.size() > 0)
79             {
80                 typeAliasesPackage = String.join(",", (String[]) allResult.toArray(new String[0]));
81             }
82             else
83             {
84                 throw new RuntimeException("mybatis typeAliasesPackage 路径扫描错误,参数typeAliasesPackage:" + typeAliasesPackage + "未找到任何包");
85             }
86         }
87         catch (IOException e)
88         {
89             e.printStackTrace();
90         }
91         return typeAliasesPackage;
92     }
93
94     public Resource[] resolveMapperLocations(String[] mapperLocations)
95     {
96         ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
97         List<Resource> resources = new ArrayList<Resource>();
98         if (mapperLocations != null)
99         {
100             for (String mapperLocation : mapperLocations)
101             {
102                 try
103                 {
104                     Resource[] mappers = resourceResolver.getResources(mapperLocation);
105                     resources.addAll(Arrays.asList(mappers));
106                 }
107                 catch (IOException e)
108                 {
109                     // ignore
110                 }
111             }
112         }
113         return resources.toArray(new Resource[resources.size()]);
114     }
115
116     @Bean
117     public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception
118     {
119         String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage");
120         String mapperLocations = env.getProperty("mybatis.mapperLocations");
121         String configLocation = env.getProperty("mybatis.configLocation");
122         typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage);
123         VFS.addImplClass(SpringBootVFS.class);
124
125         final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
126         sessionFactory.setDataSource(dataSource);
127         sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
128         sessionFactory.setMapperLocations(resolveMapperLocations(StringUtils.split(mapperLocations, ",")));
129         sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
130         return sessionFactory.getObject();
131     }
132 }