框架拆分

This commit is contained in:
暮光:城中城
2019-02-26 23:04:24 +08:00
parent 81435c790a
commit c995d3a600
79 changed files with 3063 additions and 548 deletions

View File

@@ -0,0 +1,141 @@
package com.zyplayer.doc.data.config;
import com.atomikos.icatch.jta.UserTransactionImp;
import com.atomikos.icatch.jta.UserTransactionManager;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zyplayer.doc.data.repository.support.interceptor.SqlLogInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.jta.JtaTransactionManager;
import javax.sql.DataSource;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
import java.util.Properties;
/**
* mybatis plus数据库配置
*/
@Configuration
public class MybatisPlusConfig {
/**
* sql日志
**/
private static final SqlLogInterceptor SQL_LOG_INTERCEPTOR;
static {
SQL_LOG_INTERCEPTOR = new SqlLogInterceptor();
Properties properties = new Properties();
SQL_LOG_INTERCEPTOR.setProperties(properties);
}
/**
* 分布式事务配置
*/
@Configuration
static class JTATransactionManagerConfig {
@Bean(name = "userTransaction")
public UserTransaction userTransaction() throws Throwable {
UserTransactionImp userTransactionImp = new UserTransactionImp();
userTransactionImp.setTransactionTimeout(300);
return userTransactionImp;
}
@Bean(name = "atomikosTransactionManager")
public TransactionManager atomikosTransactionManager() {
UserTransactionManager userTransactionManager = new UserTransactionManager();
userTransactionManager.setForceShutdown(true);
return userTransactionManager;
}
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager() throws Throwable {
UserTransaction userTransaction = userTransaction();
TransactionManager atomikosTransactionManager = atomikosTransactionManager();
JtaTransactionManager jtaTransactionManager = new JtaTransactionManager(userTransaction, atomikosTransactionManager);
jtaTransactionManager.setAllowCustomIsolationLevels(true);
jtaTransactionManager.setGlobalRollbackOnParticipationFailure(true);
jtaTransactionManager.setDefaultTimeout(30);
return jtaTransactionManager;
}
}
/**
* 数据库配置
*/
@Configuration
@EnableTransactionManagement
@MapperScan(value = "com.zyplayer.doc.data.repository.manage.mapper", sqlSessionFactoryRef = "manageSqlSessionFactory")
static class ManageMybatisDbConfig {
@Value("${zyplayer.doc.manage.datasource.driverClassName}")
private String driverClassName;
@Value("${zyplayer.doc.manage.datasource.url}")
private String url;
@Value("${zyplayer.doc.manage.datasource.username}")
private String username;
@Value("${zyplayer.doc.manage.datasource.password}")
private String password;
@Bean(name = "manageDatasource")
public DataSource manageDatasource() {
Properties xaProperties = new Properties();
xaProperties.setProperty("driverClassName", driverClassName);
xaProperties.setProperty("url", url);
xaProperties.setProperty("username", username);
xaProperties.setProperty("password", password);
xaProperties.setProperty("maxActive", "500");
xaProperties.setProperty("testOnBorrow", "true");
xaProperties.setProperty("testWhileIdle", "true");
xaProperties.setProperty("validationQuery", "select 'x'");
AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
xaDataSource.setXaProperties(xaProperties);
xaDataSource.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource");
xaDataSource.setUniqueResourceName("manageDatasource");
xaDataSource.setMaxPoolSize(500);
xaDataSource.setMinPoolSize(1);
xaDataSource.setMaxLifetime(60);
return xaDataSource;
}
@Bean(name = "manageSqlSessionFactory")
public MybatisSqlSessionFactoryBean manageSqlSessionFactory() throws Exception {
MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(manageDatasource());
sqlSessionFactoryBean.setPlugins(new Interceptor[]{SQL_LOG_INTERCEPTOR});
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mapper/manage/*Mapper.xml"));
return sqlSessionFactoryBean;
}
}
@Bean
public PerformanceInterceptor performanceInterceptor() {
PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
/* <!-- SQL 执行性能分析,开发环境使用,线上不推荐。 maxTime 指的是 sql 最大执行时长 --> */
performanceInterceptor.setMaxTime(1000);
/* <!--SQL是否格式化 默认false--> */
performanceInterceptor.setFormat(true);
return performanceInterceptor;
}
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}

View File

@@ -0,0 +1,79 @@
package com.zyplayer.doc.data.config.security;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collection;
public class DocUserDetails implements UserDetails {
private static final long serialVersionUID = 1L;
private Long userId;
private String username;
private String password;
private boolean enabled;
private Collection<? extends GrantedAuthority> authorities;
public DocUserDetails(Long userId, String username, String password, boolean enabled) {
super();
this.userId = userId;
this.username = username;
this.password = password;
this.enabled = enabled;
}
public DocUserDetails(Long userId, String username, String password, boolean enabled,
Collection<? extends GrantedAuthority> authorities) {
super();
this.userId = userId;
this.username = username;
this.password = password;
this.enabled = enabled;
this.authorities = authorities;
}
public Long getUserId() {
return this.userId;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return authorities;
}
@Override
public String getPassword() {
return password;
}
@Override
public String getUsername() {
return username;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return enabled;
}
@Override
public String toString() {
return "MyUserDetails [userId=" + userId + ", username=" + username + ", password=" + password + ", enabled="
+ enabled + ", authorities=" + authorities + "]";
}
}

View File

@@ -0,0 +1,23 @@
package com.zyplayer.doc.data.config.security;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
/**
* 用户工具类
*/
public class DocUserUtil {
/**
* 获取当前用户
* @return 用户信息
*/
public static DocUserDetails getCurrentUser() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Object principal = null;
if (authentication != null) {
principal = authentication.getPrincipal();
}
return (DocUserDetails) principal;
}
}