7 changed files with 180 additions and 79 deletions
@ -0,0 +1,167 @@ |
|||||
|
package com.elink.esua.epdc.commons.mybatis.config; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer; |
||||
|
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties; |
||||
|
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusPropertiesCustomizer; |
||||
|
import com.baomidou.mybatisplus.autoconfigure.SpringBootVFS; |
||||
|
import com.baomidou.mybatisplus.core.MybatisConfiguration; |
||||
|
import com.baomidou.mybatisplus.core.config.GlobalConfig; |
||||
|
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; |
||||
|
import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator; |
||||
|
import com.baomidou.mybatisplus.core.injector.ISqlInjector; |
||||
|
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; |
||||
|
import io.seata.rm.datasource.DataSourceProxy; |
||||
|
import org.apache.ibatis.mapping.DatabaseIdProvider; |
||||
|
import org.apache.ibatis.plugin.Interceptor; |
||||
|
import org.apache.ibatis.session.SqlSessionFactory; |
||||
|
import org.slf4j.Logger; |
||||
|
import org.slf4j.LoggerFactory; |
||||
|
import org.springframework.beans.factory.InitializingBean; |
||||
|
import org.springframework.beans.factory.ObjectProvider; |
||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
||||
|
import org.springframework.context.ApplicationContext; |
||||
|
import org.springframework.context.annotation.Bean; |
||||
|
import org.springframework.context.annotation.Configuration; |
||||
|
import org.springframework.core.io.Resource; |
||||
|
import org.springframework.core.io.ResourceLoader; |
||||
|
import org.springframework.util.Assert; |
||||
|
import org.springframework.util.CollectionUtils; |
||||
|
import org.springframework.util.ObjectUtils; |
||||
|
import org.springframework.util.StringUtils; |
||||
|
|
||||
|
import javax.sql.DataSource; |
||||
|
import java.util.Iterator; |
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* seata分布式事务数据源 |
||||
|
* |
||||
|
* @author rongchao |
||||
|
* @Date 19-8-21 |
||||
|
*/ |
||||
|
@Configuration |
||||
|
@EnableConfigurationProperties({MybatisPlusProperties.class}) |
||||
|
public class DataSourceConfig implements InitializingBean { |
||||
|
|
||||
|
private final Logger logger = LoggerFactory.getLogger(getClass()); |
||||
|
|
||||
|
private final MybatisPlusProperties properties; |
||||
|
private final Interceptor[] interceptors; |
||||
|
private final ResourceLoader resourceLoader; |
||||
|
private final DatabaseIdProvider databaseIdProvider; |
||||
|
private final List<ConfigurationCustomizer> configurationCustomizers; |
||||
|
private final List<MybatisPlusPropertiesCustomizer> mybatisPlusPropertiesCustomizers; |
||||
|
private final ApplicationContext applicationContext; |
||||
|
|
||||
|
public DataSourceConfig(MybatisPlusProperties properties, ObjectProvider<Interceptor[]> interceptorsProvider, ResourceLoader resourceLoader, ObjectProvider<DatabaseIdProvider> databaseIdProvider, ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider, ObjectProvider<List<MybatisPlusPropertiesCustomizer>> mybatisPlusPropertiesCustomizerProvider, ApplicationContext applicationContext) { |
||||
|
this.properties = properties; |
||||
|
this.interceptors = (Interceptor[]) interceptorsProvider.getIfAvailable(); |
||||
|
this.resourceLoader = resourceLoader; |
||||
|
this.databaseIdProvider = (DatabaseIdProvider) databaseIdProvider.getIfAvailable(); |
||||
|
this.configurationCustomizers = (List) configurationCustomizersProvider.getIfAvailable(); |
||||
|
this.mybatisPlusPropertiesCustomizers = (List) mybatisPlusPropertiesCustomizerProvider.getIfAvailable(); |
||||
|
this.applicationContext = applicationContext; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void afterPropertiesSet() { |
||||
|
if (!CollectionUtils.isEmpty(this.mybatisPlusPropertiesCustomizers)) { |
||||
|
this.mybatisPlusPropertiesCustomizers.forEach((i) -> { |
||||
|
i.customize(this.properties); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
this.checkConfigFileExists(); |
||||
|
} |
||||
|
|
||||
|
private void checkConfigFileExists() { |
||||
|
if (this.properties.isCheckConfigLocation() && StringUtils.hasText(this.properties.getConfigLocation())) { |
||||
|
Resource resource = this.resourceLoader.getResource(this.properties.getConfigLocation()); |
||||
|
Assert.state(resource.exists(), "Cannot find config location: " + resource + " (please add config file or check your Mybatis configuration)"); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
@Bean |
||||
|
@ConditionalOnMissingBean |
||||
|
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { |
||||
|
DataSourceProxy dataSourceProxy = new DataSourceProxy(dataSource); |
||||
|
MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean(); |
||||
|
factory.setDataSource(dataSourceProxy); |
||||
|
factory.setVfs(SpringBootVFS.class); |
||||
|
if (StringUtils.hasText(this.properties.getConfigLocation())) { |
||||
|
factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation())); |
||||
|
} |
||||
|
|
||||
|
this.applyConfiguration(factory); |
||||
|
if (this.properties.getConfigurationProperties() != null) { |
||||
|
factory.setConfigurationProperties(this.properties.getConfigurationProperties()); |
||||
|
} |
||||
|
|
||||
|
if (!ObjectUtils.isEmpty(this.interceptors)) { |
||||
|
factory.setPlugins(this.interceptors); |
||||
|
} |
||||
|
|
||||
|
if (this.databaseIdProvider != null) { |
||||
|
factory.setDatabaseIdProvider(this.databaseIdProvider); |
||||
|
} |
||||
|
|
||||
|
if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) { |
||||
|
factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage()); |
||||
|
} |
||||
|
|
||||
|
if (this.properties.getTypeAliasesSuperType() != null) { |
||||
|
factory.setTypeAliasesSuperType(this.properties.getTypeAliasesSuperType()); |
||||
|
} |
||||
|
|
||||
|
if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) { |
||||
|
factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage()); |
||||
|
} |
||||
|
|
||||
|
if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) { |
||||
|
factory.setMapperLocations(this.properties.resolveMapperLocations()); |
||||
|
} |
||||
|
|
||||
|
if (StringUtils.hasLength(this.properties.getTypeEnumsPackage())) { |
||||
|
factory.setTypeEnumsPackage(this.properties.getTypeEnumsPackage()); |
||||
|
} |
||||
|
|
||||
|
GlobalConfig globalConfig = this.properties.getGlobalConfig(); |
||||
|
if (this.applicationContext.getBeanNamesForType(MetaObjectHandler.class, false, false).length > 0) { |
||||
|
MetaObjectHandler metaObjectHandler = (MetaObjectHandler) this.applicationContext.getBean(MetaObjectHandler.class); |
||||
|
globalConfig.setMetaObjectHandler(metaObjectHandler); |
||||
|
} |
||||
|
|
||||
|
if (this.applicationContext.getBeanNamesForType(IKeyGenerator.class, false, false).length > 0) { |
||||
|
IKeyGenerator keyGenerator = (IKeyGenerator) this.applicationContext.getBean(IKeyGenerator.class); |
||||
|
globalConfig.getDbConfig().setKeyGenerator(keyGenerator); |
||||
|
} |
||||
|
|
||||
|
if (this.applicationContext.getBeanNamesForType(ISqlInjector.class, false, false).length > 0) { |
||||
|
ISqlInjector iSqlInjector = (ISqlInjector) this.applicationContext.getBean(ISqlInjector.class); |
||||
|
globalConfig.setSqlInjector(iSqlInjector); |
||||
|
} |
||||
|
|
||||
|
factory.setGlobalConfig(globalConfig); |
||||
|
return factory.getObject(); |
||||
|
} |
||||
|
|
||||
|
private void applyConfiguration(MybatisSqlSessionFactoryBean factory) { |
||||
|
MybatisConfiguration configuration = this.properties.getConfiguration(); |
||||
|
if (configuration == null && !StringUtils.hasText(this.properties.getConfigLocation())) { |
||||
|
configuration = new MybatisConfiguration(); |
||||
|
} |
||||
|
|
||||
|
if (configuration != null && !CollectionUtils.isEmpty(this.configurationCustomizers)) { |
||||
|
Iterator var3 = this.configurationCustomizers.iterator(); |
||||
|
|
||||
|
while (var3.hasNext()) { |
||||
|
ConfigurationCustomizer customizer = (ConfigurationCustomizer) var3.next(); |
||||
|
customizer.customize(configuration); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
factory.setConfiguration(configuration); |
||||
|
} |
||||
|
} |
@ -1,54 +0,0 @@ |
|||||
/** |
|
||||
* Copyright (c) 2018 人人开源 All rights reserved. |
|
||||
* |
|
||||
* https://www.renren.io
|
|
||||
* |
|
||||
* 版权所有,侵权必究! |
|
||||
*/ |
|
||||
|
|
||||
package com.elink.esua.epdc.resolver; |
|
||||
|
|
||||
import com.elink.esua.epdc.annotation.LoginUser; |
|
||||
import com.elink.esua.epdc.annotation.LoginUser; |
|
||||
import com.elink.esua.epdc.entity.UserEntity; |
|
||||
import com.elink.esua.epdc.interceptor.AuthorizationInterceptor; |
|
||||
import com.elink.esua.epdc.service.UserService; |
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||
import org.springframework.core.MethodParameter; |
|
||||
import org.springframework.stereotype.Component; |
|
||||
import org.springframework.web.bind.support.WebDataBinderFactory; |
|
||||
import org.springframework.web.context.request.NativeWebRequest; |
|
||||
import org.springframework.web.context.request.RequestAttributes; |
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; |
|
||||
import org.springframework.web.method.support.ModelAndViewContainer; |
|
||||
|
|
||||
/** |
|
||||
* 有@LoginUser注解的方法参数,注入当前登录用户 |
|
||||
* |
|
||||
* @author Mark sunlightcs@gmail.com |
|
||||
*/ |
|
||||
@Component |
|
||||
public class LoginUserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver { |
|
||||
@Autowired |
|
||||
private UserService userService; |
|
||||
|
|
||||
@Override |
|
||||
public boolean supportsParameter(MethodParameter parameter) { |
|
||||
return parameter.getParameterType().isAssignableFrom(UserEntity.class) && parameter.hasParameterAnnotation(LoginUser.class); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container, |
|
||||
NativeWebRequest request, WebDataBinderFactory factory) throws Exception { |
|
||||
//获取用户ID
|
|
||||
Object object = request.getAttribute(AuthorizationInterceptor.USER_KEY, RequestAttributes.SCOPE_REQUEST); |
|
||||
if(object == null){ |
|
||||
return null; |
|
||||
} |
|
||||
|
|
||||
//获取用户信息
|
|
||||
UserEntity user = userService.getUserByUserId((Long)object); |
|
||||
|
|
||||
return user; |
|
||||
} |
|
||||
} |
|
Loading…
Reference in new issue