5 changed files with 191 additions and 0 deletions
-
33src/main/java/com/example/demo/config/CacheConfig.java
-
50src/main/java/com/example/demo/config/Mysql1DataSourceConfig.java
-
46src/main/java/com/example/demo/config/Mysql2DataSourceConfig.java
-
44src/main/java/com/example/demo/security/UploadFilter.java
-
18src/test/java/com/example/demo/TestConnection.java
@ -0,0 +1,33 @@ |
|||
package com.example.demo.config; |
|||
|
|||
import org.springframework.cache.CacheManager; |
|||
import org.springframework.cache.annotation.EnableCaching; |
|||
import org.springframework.cache.concurrent.ConcurrentMapCache; |
|||
import org.springframework.cache.support.SimpleCacheManager; |
|||
import org.springframework.context.annotation.Bean; |
|||
import org.springframework.context.annotation.Configuration; |
|||
import org.springframework.data.redis.cache.RedisCacheConfiguration; |
|||
import org.springframework.data.redis.cache.RedisCacheManager; |
|||
import org.springframework.data.redis.connection.RedisConnectionFactory; |
|||
|
|||
import java.time.Duration; |
|||
import java.util.Arrays; |
|||
|
|||
@Configuration |
|||
@EnableCaching |
|||
public class CacheConfig { |
|||
|
|||
@Bean |
|||
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { |
|||
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() |
|||
.entryTtl(Duration.ofHours(1)) // 设置默认过期时间为 1 小时 |
|||
.disableCachingNullValues(); // 禁用空值缓存 |
|||
|
|||
return RedisCacheManager.builder(redisConnectionFactory) |
|||
.cacheDefaults(config) |
|||
.withCacheConfiguration("statistics", config) // 注册缓存名称 |
|||
.withCacheConfiguration("recharge", config) // 注册缓存名称 |
|||
.withCacheConfiguration("consume", config) // 注册缓存名称 |
|||
.build(); |
|||
} |
|||
} |
@ -0,0 +1,50 @@ |
|||
package com.example.demo.config; |
|||
|
|||
import com.zaxxer.hikari.HikariDataSource; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.apache.ibatis.session.SqlSessionFactory; |
|||
import org.mybatis.spring.SqlSessionFactoryBean; |
|||
import org.mybatis.spring.SqlSessionTemplate; |
|||
import org.springframework.beans.factory.annotation.Qualifier; |
|||
import org.springframework.boot.context.properties.ConfigurationProperties; |
|||
import org.springframework.boot.jdbc.DataSourceBuilder; |
|||
import org.springframework.context.annotation.Bean; |
|||
import org.springframework.context.annotation.Configuration; |
|||
import org.springframework.context.annotation.Primary; |
|||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver; |
|||
|
|||
import javax.sql.DataSource; |
|||
|
|||
@Slf4j |
|||
@Configuration |
|||
public class Mysql1DataSourceConfig { |
|||
|
|||
@Bean(name = "mysql1DataSource") |
|||
@Primary |
|||
public DataSource mysql1DataSource() { |
|||
HikariDataSource dataSource = new HikariDataSource(); |
|||
dataSource.setJdbcUrl("jdbc:mysql://54.251.137.151:10701/hwgold?serverTimezone=Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=UTF-8"); |
|||
dataSource.setUsername("hwgold"); |
|||
dataSource.setPassword("AGX4Z3YMxiCG3GR2"); |
|||
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); |
|||
dataSource.setMaximumPoolSize(10); |
|||
dataSource.setPoolName("mysql1HikariCP"); |
|||
log.info("mysql1 DataSource initialized with pool-name: {}", dataSource.getPoolName()); |
|||
return dataSource; |
|||
} |
|||
|
|||
@Bean(name = "mysql1SqlSessionFactory") |
|||
@Primary |
|||
public SqlSessionFactory mysql1SqlSessionFactory(@Qualifier("mysql1DataSource") DataSource dataSource) throws Exception { |
|||
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); |
|||
sessionFactory.setDataSource(dataSource); |
|||
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml")); |
|||
return sessionFactory.getObject(); |
|||
} |
|||
|
|||
@Bean(name = "mysql1SqlSessionTemplate") |
|||
@Primary |
|||
public SqlSessionTemplate mysql1SqlSessionTemplate(@Qualifier("mysql1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) { |
|||
return new SqlSessionTemplate(sqlSessionFactory); |
|||
} |
|||
} |
@ -0,0 +1,46 @@ |
|||
package com.example.demo.config; |
|||
|
|||
import com.zaxxer.hikari.HikariDataSource; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.apache.ibatis.session.SqlSessionFactory; |
|||
import org.mybatis.spring.SqlSessionFactoryBean; |
|||
import org.mybatis.spring.SqlSessionTemplate; |
|||
import org.springframework.beans.factory.annotation.Qualifier; |
|||
import org.springframework.boot.context.properties.ConfigurationProperties; |
|||
import org.springframework.boot.jdbc.DataSourceBuilder; |
|||
import org.springframework.context.annotation.Bean; |
|||
import org.springframework.context.annotation.Configuration; |
|||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver; |
|||
|
|||
import javax.sql.DataSource; |
|||
|
|||
@Slf4j |
|||
@Configuration |
|||
public class Mysql2DataSourceConfig { |
|||
|
|||
@Bean(name = "mysql2DataSource") |
|||
public DataSource mysql2DataSource() { |
|||
HikariDataSource dataSource = new HikariDataSource(); |
|||
dataSource.setJdbcUrl("jdbc:mysql://39.101.133.168:3306/hwgold?serverTimezone=Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=UTF-8"); |
|||
dataSource.setUsername("hljw"); |
|||
dataSource.setPassword("5dmWCCKfEk3TTeyn"); |
|||
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); |
|||
dataSource.setMaximumPoolSize(10); |
|||
dataSource.setPoolName("mysql2HikariCP"); |
|||
log.info("mysql2 DataSource initialized with pool-name: {}", dataSource.getPoolName()); |
|||
return dataSource; |
|||
} |
|||
|
|||
@Bean(name = "mysql2SqlSessionFactory") |
|||
public SqlSessionFactory mysql2SqlSessionFactory(@Qualifier("mysql2DataSource") DataSource dataSource) throws Exception { |
|||
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); |
|||
sessionFactory.setDataSource(dataSource); |
|||
// sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapperLink/*.xml")); |
|||
return sessionFactory.getObject(); |
|||
} |
|||
|
|||
@Bean(name = "mysql2SqlSessionTemplate") |
|||
public SqlSessionTemplate mysql2SqlSessionTemplate(@Qualifier("mysql2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) { |
|||
return new SqlSessionTemplate(sqlSessionFactory); |
|||
} |
|||
} |
@ -0,0 +1,44 @@ |
|||
package com.example.demo.security; |
|||
|
|||
import jakarta.servlet.FilterChain; |
|||
import jakarta.servlet.ServletException; |
|||
import jakarta.servlet.http.HttpServletRequest; |
|||
import jakarta.servlet.http.HttpServletResponse; |
|||
import org.springframework.core.annotation.Order; |
|||
import org.springframework.stereotype.Component; |
|||
import org.springframework.web.filter.OncePerRequestFilter; |
|||
import org.springframework.web.multipart.MultipartResolver; |
|||
|
|||
import java.io.IOException; |
|||
|
|||
|
|||
@Component |
|||
public class UploadFilter extends OncePerRequestFilter { |
|||
|
|||
private final MultipartResolver multipartResolver; |
|||
|
|||
public UploadFilter(MultipartResolver multipartResolver) { |
|||
this.multipartResolver = multipartResolver; |
|||
} |
|||
|
|||
@Override |
|||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) |
|||
throws ServletException, IOException { |
|||
|
|||
// 检查请求是否为上传请求,这里假设上传请求的路径以 "/upload" 开头 |
|||
boolean isUploadRequest = request.getRequestURI().startsWith("/upload"); |
|||
System.out.println(isUploadRequest); |
|||
System.out.println("MultipartResolver: " + multipartResolver); |
|||
if (isUploadRequest ) { |
|||
System.out.println("执行upload-------------------------------"); |
|||
// 如果是上传请求且Content-Type为multipart/form-data,直接将请求传递给下一个过滤器或目标资源 |
|||
filterChain.doFilter(request, response); |
|||
} else { |
|||
// 如果不是上传请求,执行一些自定义逻辑 |
|||
// 例如,可以在这里添加令牌验证或其他安全检查 |
|||
|
|||
// 继续执行过滤器链 |
|||
filterChain.doFilter(request, response); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,18 @@ |
|||
package com.example.demo; |
|||
|
|||
import java.sql.Connection; |
|||
import java.sql.DriverManager; |
|||
|
|||
public class TestConnection { |
|||
public static void main(String[] args) { |
|||
String jdbcUrl = "jdbc:mysql://54.251.137.151:10701/hwgold?serverTimezone=Asia/Shanghai"; |
|||
String username = "hwgold"; |
|||
String password = "AGX4Z3YMxiCG3GR2"; |
|||
|
|||
try (Connection connection = DriverManager.getConnection(jdbcUrl, username, password)) { |
|||
System.out.println("Connection successful!"); |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue