refactor(商城): 代码优化

master
wayn 3 years ago
parent 334e17e88d
commit d104557153

@ -17,6 +17,14 @@
</properties>
<dependencies>
<!--阿里数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
@ -95,9 +103,9 @@
<!-- 指定该Main Class为全局的唯一入口 -->
<mainClass>com.wayn.AdminApplication</mainClass>
<layout>ZIP</layout>
<!-- <commandlineArguments>-->
<!-- -parameters-->
<!-- </commandlineArguments>-->
<!-- <commandlineArguments>-->
<!-- -parameters-->
<!-- </commandlineArguments>-->
</configuration>
<executions>
<execution>
@ -108,16 +116,16 @@
</executions>
</plugin>
<plugin>
<!--<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<!--配置基本镜像-->
&lt;!&ndash;配置基本镜像&ndash;&gt;
<from>
<image>${docker-from-image-name}</image>
</from>
<!--配置最终推送的地址,仓库名,镜像名-->
&lt;!&ndash;配置最终推送的地址,仓库名,镜像名&ndash;&gt;
<to>
<image>registry.cn-shanghai.aliyuncs.com/${aliyun-docker-namespace}/${project.artifactId}
</image>
@ -142,7 +150,7 @@
</container>
</configuration>
<!-- 绑定到maven lifecicle-->
&lt;!&ndash; 绑定到maven lifecicle&ndash;&gt;
<executions>
<execution>
<phase>package</phase>
@ -151,7 +159,7 @@
</goals>
</execution>
</executions>
</plugin>
</plugin>-->
</plugins>
</build>

@ -20,7 +20,7 @@ public class DruidConfig {
@Bean
@ConfigurationProperties("spring.datasource.druid.master")
@ConfigurationProperties("spring.datasource.master")
public DataSource dataSource(DruidProperties druidProperties) {
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
return druidProperties.dataSource(dataSource);

@ -44,7 +44,7 @@ public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
}
chain.doFilter(request, response);
} catch (RedisConnectionFailureException exception) {
} catch (RedisConnectionFailureException exception) { // 处理redis连接超时异常
response.setStatus(HttpStatus.OK.value());
response.setContentType("application/json");
response.setCharacterEncoding(Constants.UTF_ENCODING);

@ -0,0 +1,42 @@
# druid数据源配置
spring:
datasource:
druid:
initialSize: 10
# 最小连接池数量
minIdle: 10
# 最大连接池数量
maxActive: 50
# 配置获取连接等待超时的时间
# maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
# 配置一个连接在池中最大生存的时间,单位是毫秒
maxEvictableIdleTimeMillis: 900000
# 配置检测连接是否有效
validationQuery: SELECT 1
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
webStatFilter:
enabled: true
statViewServlet:
enabled: true
# 设置白名单,不填则允许所有访问
allow:
url-pattern: /druid/*
# 控制台管理用户名和密码
login-username:
login-password:
filter:
stat:
enabled: true
# 慢SQL记录
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: true
wall:
config:
multi-statement-allow: true

@ -3,7 +3,7 @@ server:
spring:
profiles:
active: dev
active: dev,druid
thymeleaf:
cache: false
suffix: .html
@ -52,7 +52,7 @@ wayn:
name: wayn
version: 1.1.0
email: 166738430@qq.com
# uploadDir: /opt/wayn/upload # linux下文件上传目录
·# uploadDir: /opt/wayn/upload # linux下文件上传目录
uploadDir: D:/wayn/upload # windows下文件上传目录
adminUrl: http://127.0.0.1:81
mobileUrl: http://127.0.0.1:82

@ -61,13 +61,6 @@
<version>${mybatis-plus.version}</version>
</dependency>
<!--阿里数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>

@ -30,5 +30,5 @@ public interface GoodsMapper extends BaseMapper<Goods> {
List<Goods> selectHomeIndex(Goods goods);
IPage<Goods> selectColumnGoodsPage(Page<Goods> page, List<Long> goodsIdList);
IPage<Goods> selectColumnGoodsPage(Page<Goods> page, Long columnId);
}

@ -95,5 +95,12 @@ public interface IGoodsService extends IService<Goods> {
List<Goods> searchResult(Page<SearchVO> page, SearchVO searchVO);
IPage<Goods> selectColumnGoodsPage(Page<Goods> page, List<Long> goodsIdList);
/**
* ID
*
* @param page
* @param columnId ID
* @return
*/
IPage<Goods> selectColumnGoodsPageByColumnId(Page<Goods> page, Long columnId);
}

@ -237,8 +237,8 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements
}
@Override
public IPage<Goods> selectColumnGoodsPage(Page<Goods> page, List<Long> goodsIdList) {
return goodsMapper.selectColumnGoodsPage(page, goodsIdList);
public IPage<Goods> selectColumnGoodsPageByColumnId(Page<Goods> page, Long columnId) {
return goodsMapper.selectColumnGoodsPage(page, columnId);
}
/**

@ -1,60 +1,20 @@
# 数据源配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
druid:
# 主库数据源
master:
url: jdbc:mysql://127.0.0.1:3306/wayn_shop?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: 123456
# 从库数据源
slave:
# 从数据源开关/默认关闭
enabled: false
url:
username:
password:
# 初始连接数
initialSize: 40
# 最小连接池数量
minIdle: 40
# 最大连接池数量
maxActive: 60
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
# 配置一个连接在池中最大生存的时间,单位是毫秒
maxEvictableIdleTimeMillis: 900000
# 配置检测连接是否有效
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
webStatFilter:
enabled: true
statViewServlet:
enabled: true
# 设置白名单,不填则允许所有访问
allow:
url-pattern: /druid/*
# 控制台管理用户名和密码
login-username:
login-password:
filter:
stat:
enabled: true
# 慢SQL记录
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: true
wall:
config:
multi-statement-allow: true
# 主库数据源
master:
url: jdbc:mysql://127.0.0.1:3306/wayn_shop?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: root
# 从库数据源
slave:
# 从数据源开关/默认关闭
enabled: false
url:
username:
password:
redis:
host: 127.0.0.1
port: 6379
@ -86,6 +46,8 @@ es:
host: huawei.wayn.ltd
port: 9200
scheme: http
shards: 3
replicas: 2
# alipay 需要自行申请支付宝的沙箱账号、申请appID并依次配置AppID、密钥、公钥否则无法发起支付宝支付。
alipay:

@ -165,14 +165,8 @@
<include refid="goods_info"/>
<where>
del_flag = 0
and is_on_sale = 1
<if test="goodsIdList != null and goodsIdList.size() > 0">
and id in (
<foreach collection="goodsIdList" item="item" separator=",">
#{item}
</foreach>
)
</if>
AND is_on_sale = 1
AND id in ( SELECT goods_id FROM shop_column_goods_relation r WHERE r.column_id = #{columnId} )
</where>
order by create_time desc
</select>

@ -1,8 +1,6 @@
package com.wayn.mobile.design.strategy.concretestrategy;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wayn.common.core.domain.shop.ColumnGoodsRelation;
import com.wayn.common.core.domain.shop.Diamond;
import com.wayn.common.core.domain.shop.Goods;
import com.wayn.common.core.service.shop.IColumnGoodsRelationService;
@ -13,7 +11,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.stream.Collectors;
/**
*
@ -29,10 +26,7 @@ public class ColumnStrategy implements DiamondJumpType {
@Override
public List<Goods> getGoods(Page<Goods> page, Diamond diamond) {
List<ColumnGoodsRelation> goodsRelationList = iColumnGoodsRelationService
.list(new QueryWrapper<ColumnGoodsRelation>().eq("column_id", diamond.getValueId()));
List<Long> goodsIdList = goodsRelationList.stream().map(ColumnGoodsRelation::getGoodsId).collect(Collectors.toList());
return iGoodsService.selectColumnGoodsPage(page, goodsIdList).getRecords();
return iGoodsService.selectColumnGoodsPageByColumnId(page, diamond.getValueId()).getRecords();
}
@Override

@ -1,71 +0,0 @@
package com.wayn.mobile.framework.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
import com.alibaba.druid.util.Utils;
import com.wayn.mobile.framework.config.properties.DruidProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.*;
import javax.sql.DataSource;
import java.io.IOException;
@Configuration
public class DruidConfig {
@Bean
@ConfigurationProperties("spring.datasource.druid.master")
public DataSource dataSource(DruidProperties druidProperties) {
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
return druidProperties.dataSource(dataSource);
}
/**
* 广
*/
@SuppressWarnings({"rawtypes", "unchecked"})
@Bean
@ConditionalOnProperty(name = "spring.datasource.druid.statViewServlet.enabled", havingValue = "true")
public FilterRegistrationBean removeDruidFilterRegistrationBean(DruidStatProperties properties) {
// 获取web监控页面的参数
DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
// 提取common.js的配置路径
String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
final String filePath = "support/http/resources/js/common.js";
// 创建filter进行过滤
Filter filter = new Filter() {
@Override
public void init(FilterConfig filterConfig) {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
chain.doFilter(request, response);
// 重置缓冲区,响应头不会被重置
response.resetBuffer();
// 获取common.js
String text = Utils.readFromResource(filePath);
// 正则替换banner, 除去底部的广告信息
text = text.replaceAll("<a.*?banner\"></a><br/>", "");
text = text.replaceAll("powered.*?shrek.wang</a>", "");
response.getWriter().write(text);
}
@Override
public void destroy() {
}
};
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(filter);
registrationBean.addUrlPatterns(commonJsPattern);
return registrationBean;
}
}

@ -0,0 +1,17 @@
package com.wayn.mobile.framework.config;
import com.wayn.mobile.framework.config.properties.HikariProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class HikariCpConfig {
@Bean
public DataSource dataSource(HikariProperties hikariProperties) {
return hikariProperties.dataSource();
}
}

@ -1,69 +0,0 @@
package com.wayn.mobile.framework.config.properties;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
/**
* druid
*
* @author ruoyi
*/
@Configuration
public class DruidProperties {
@Value("${spring.datasource.druid.initialSize}")
private int initialSize;
@Value("${spring.datasource.druid.minIdle}")
private int minIdle;
@Value("${spring.datasource.druid.maxActive}")
private int maxActive;
@Value("${spring.datasource.druid.timeBetweenEvictionRunsMillis}")
private int timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.druid.minEvictableIdleTimeMillis}")
private int minEvictableIdleTimeMillis;
@Value("${spring.datasource.druid.maxEvictableIdleTimeMillis}")
private int maxEvictableIdleTimeMillis;
@Value("${spring.datasource.druid.validationQuery}")
private String validationQuery;
@Value("${spring.datasource.druid.testWhileIdle}")
private boolean testWhileIdle;
@Value("${spring.datasource.druid.testOnBorrow}")
private boolean testOnBorrow;
@Value("${spring.datasource.druid.testOnReturn}")
private boolean testOnReturn;
public DruidDataSource dataSource(DruidDataSource datasource) {
/** 配置初始化大小、最小、最大 */
datasource.setInitialSize(initialSize);
datasource.setMaxActive(maxActive);
datasource.setMinIdle(minIdle);
/** 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 */
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
/** 配置一个连接在池中最小、最大生存的时间,单位是毫秒 */
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
datasource.setMaxEvictableIdleTimeMillis(maxEvictableIdleTimeMillis);
/**
* sqlselect 'x'validationQuerynulltestOnBorrowtestOnReturntestWhileIdle
*/
datasource.setValidationQuery(validationQuery);
/** 建议配置为true不影响性能并且保证安全性。申请连接的时候检测如果空闲时间大于timeBetweenEvictionRunsMillis执行validationQuery检测连接是否有效。 */
datasource.setTestWhileIdle(testWhileIdle);
/** 申请连接时执行validationQuery检测连接是否有效做了这个配置会降低性能。 */
datasource.setTestOnBorrow(testOnBorrow);
/** 归还连接时执行validationQuery检测连接是否有效做了这个配置会降低性能。 */
datasource.setTestOnReturn(testOnReturn);
return datasource;
}
}

@ -0,0 +1,88 @@
package com.wayn.mobile.framework.config.properties;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HikariProperties {
@Value("${spring.datasource.master.url}")
private String jdbcUrl;
@Value("${spring.datasource.master.username}")
private String username;
@Value("${spring.datasource.master.password}")
private String password;
@Value("${spring.datasource.hikari.poolName}")
private String poolName;
@Value("${spring.datasource.hikari.minimumIdle}")
private int minimumIdle;
@Value("${spring.datasource.hikari.maximumPoolSize}")
private int maximumPoolSize;
@Value("${spring.datasource.hikari.autoCommit}")
private boolean autoCommit;
@Value("${spring.datasource.hikari.idleTimeout}")
private int idleTimeout;
@Value("${spring.datasource.hikari.maxLifetime}")
private int maxLifetime;
@Value("${spring.datasource.hikari.connectionTimeout}")
private int connectionTimeout;
@Value("${spring.datasource.hikari.connectionTestQuery}")
private String connectionTestQuery;
@Value("${spring.datasource.hikari.cachePrepStmts}")
private boolean cachePrepStmts;
@Value("${spring.datasource.hikari.prepStmtCacheSize}")
private int prepStmtCacheSize;
@Value("${spring.datasource.hikari.prepStmtCacheSqlLimit}")
private int prepStmtCacheSqlLimit;
@Value("${spring.datasource.hikari.useServerPrepStmts}")
private boolean useServerPrepStmts;
@Value("${spring.datasource.hikari.useLocalSessionState}")
private boolean useLocalSessionState;
@Value("${spring.datasource.hikari.rewriteBatchedStatements}")
private boolean rewriteBatchedStatements;
@Value("${spring.datasource.hikari.cacheResultSetMetadata}")
private boolean cacheResultSetMetadata;
@Value("${spring.datasource.hikari.elideSetAutoCommits}")
private boolean elideSetAutoCommits;
@Value("${spring.datasource.hikari.maintainTimeStats}")
private boolean maintainTimeStats;
public HikariDataSource dataSource() {
HikariConfig config = new HikariConfig();
config.setJdbcUrl(jdbcUrl);
config.setUsername(username);
config.setPassword(password);
config.setPoolName(poolName);
config.setMinimumIdle(minimumIdle);
config.setMaximumPoolSize(maximumPoolSize);
config.setAutoCommit(autoCommit);
config.setIdleTimeout(idleTimeout);
config.setMaxLifetime(maxLifetime);
config.setConnectionTimeout(connectionTimeout);
config.setConnectionTestQuery(connectionTestQuery);
config.addDataSourceProperty("cachePrepStmts", cachePrepStmts);
config.addDataSourceProperty("prepStmtCacheSize", prepStmtCacheSize);
config.addDataSourceProperty("prepStmtCacheSqlLimit", prepStmtCacheSqlLimit);
config.addDataSourceProperty("useServerPrepStmts", useServerPrepStmts);
config.addDataSourceProperty("useLocalSessionState", useLocalSessionState);
config.addDataSourceProperty("rewriteBatchedStatements", rewriteBatchedStatements);
config.addDataSourceProperty("cacheResultSetMetadata", cacheResultSetMetadata);
config.addDataSourceProperty("elideSetAutoCommits", elideSetAutoCommits);
config.addDataSourceProperty("maintainTimeStats", maintainTimeStats);
return new HikariDataSource(config);
}
}

@ -0,0 +1,22 @@
# hikari数据源配置
spring:
datasource:
hikari:
poolName: hikariCP
minimumIdle: 10
maximumPoolSize: 50
autoCommit: true
idleTimeout: 10000
maxLifetime: 30000
connectionTimeout: 30000
connectionTestQuery: SELECT 1
cachePrepStmts: true
prepStmtCacheSize: 250
prepStmtCacheSqlLimit: 2048
useServerPrepStmts: true
useLocalSessionState: true
rewriteBatchedStatements: true
cacheResultSetMetadata: true
cacheServerConfiguration: true
elideSetAutoCommits: true
maintainTimeStats: false

@ -11,7 +11,7 @@ server:
spring:
profiles:
active: dev
active: dev,hikari
thymeleaf:
cache: false
suffix: .html

Loading…
Cancel
Save