提交 306db79e 编写于 作者: 夏天。。's avatar 夏天。。

代码规范部署

ps:代码规范为先前定制,这次提交是为博客做准备
上级 8e2cbcf4
...@@ -84,6 +84,18 @@ ...@@ -84,6 +84,18 @@
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--aop依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
...@@ -46,6 +46,4 @@ public class RestResult { ...@@ -46,6 +46,4 @@ public class RestResult {
} }
((Map) this.data).put("total", total); ((Map) this.data).put("total", total);
} }
} }
...@@ -4,11 +4,19 @@ import org.springframework.context.annotation.Bean; ...@@ -4,11 +4,19 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration @Configuration
public class MyWebConfig implements WebMvcConfigurer { public class MyWebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//资源映射
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}
// 设置允许跨域请求 // 设置允许跨域请求
@Bean @Bean
public WebMvcConfigurer corsConfigurer() { public WebMvcConfigurer corsConfigurer() {
......
package olympicserver.config.aop; package com.hocztms.config.aop;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
......
package com.hocztms.config.aop;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
@Aspect
@Component
@Slf4j
public class WebLogAspect {
@Pointcut("execution(* com.hocztms.controller.*.*(..))")
private void weblogPointcut() {
}
@Before("weblogPointcut()")
private void doBefore(JoinPoint joinPoint) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
assert attributes != null;
HttpServletRequest request = attributes.getRequest();
log.info("URL: " + request.getRequestURL().toString() + " HTTP_METHOD: " + request.getMethod() + " IP: " + request.getRemoteAddr() );
log.info("data: " + Arrays.toString(joinPoint.getArgs()));
}
@AfterReturning(returning = "result", pointcut = "weblogPointcut()")
public void doAfterReturning(Object result) {
log.info("RESPONSE: " + result);
}
}
package com.hocztms.controller; package com.hocztms.controller;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hocztms.common.RestResult; import com.hocztms.common.RestResult;
import com.hocztms.entity.MedalEntity;
import com.hocztms.service.MedalService;
import com.hocztms.service.OlympicService; import com.hocztms.service.OlympicService;
import com.hocztms.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.Date; import java.util.Date;
import java.util.List;
@RestController @RestController
public class OlympicController { public class OlympicController {
...@@ -25,6 +19,11 @@ public class OlympicController { ...@@ -25,6 +19,11 @@ public class OlympicController {
return olympicService.getOlympicTotalMedals(page,size); return olympicService.getOlympicTotalMedals(page,size);
} }
@GetMapping("/medal")
public RestResult getCountryMedals(String countryName){
return olympicService.getOlympicMedalByCountryName(countryName);
}
@GetMapping("/getItems") @GetMapping("/getItems")
public RestResult getItems(){ public RestResult getItems(){
return olympicService.getOlympicItems(); return olympicService.getOlympicItems();
...@@ -42,6 +41,11 @@ public class OlympicController { ...@@ -42,6 +41,11 @@ public class OlympicController {
schedule = new Date(date); schedule = new Date(date);
}catch (Exception ignored){ }catch (Exception ignored){
} }
return olympicService.findOlympicMatchByKeyword(schedule,item,site,new Page<>(page,size)); return olympicService.getOlympicMatchByKeyword(schedule,item,site,new Page<>(page,size));
}
@GetMapping("/pride")
public RestResult getPrideByPage(int page, int size){
return olympicService.getOlympicPridesByPage(page,size);
} }
} }
package com.hocztms.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class SiteController {
@GetMapping("/index")
public String index(){
return "index";
}
}
...@@ -24,4 +24,5 @@ public class MatchEntity { ...@@ -24,4 +24,5 @@ public class MatchEntity {
private String awayname; private String awayname;
private String title; private String title;
private String venuename; private String venuename;
private String documentcode;
} }
package com.hocztms.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("tb_pride")
public class PrideEntity {
@TableId(value = "pride_id",type = IdType.AUTO)
private Integer prideId;
private String title;
private String item;
private String image;
private String url;
}
package com.hocztms.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hocztms.entity.PrideEntity;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface PrideMapper extends BaseMapper<PrideEntity> {
}
...@@ -10,11 +10,11 @@ public interface MatchService { ...@@ -10,11 +10,11 @@ public interface MatchService {
void insertMatch(MatchEntity matchEntity); void insertMatch(MatchEntity matchEntity);
List<MatchEntity> findMatchesByDate(Date date, Page page); List<MatchEntity> getMatchesByDate(Date date, Page page);
List<String> findMatchesItems(); List<String> getMatchesItems();
List<String> findMatchesVenuename(); List<String> getMatchesVenuename();
Page<MatchEntity> findMatchByKeyword(Date date,String item,String site,Page<MatchEntity> page); Page<MatchEntity> getMatchByKeyword(Date date, String item, String site, Page<MatchEntity> page);
} }
...@@ -10,5 +10,7 @@ public interface MedalService { ...@@ -10,5 +10,7 @@ public interface MedalService {
void insertMedals(MedalEntity medalEntity); void insertMedals(MedalEntity medalEntity);
Page<MedalEntity> findMedalsByPage(Page<MedalEntity> page); Page<MedalEntity> getMedalsByPage(Page<MedalEntity> page);
MedalEntity getMedalByCountryName(String countryName);
} }
...@@ -14,5 +14,9 @@ public interface OlympicService { ...@@ -14,5 +14,9 @@ public interface OlympicService {
RestResult getOlympicSite(); RestResult getOlympicSite();
RestResult findOlympicMatchByKeyword(Date date, String item, String site, Page<MatchEntity> page); RestResult getOlympicMatchByKeyword(Date date, String item, String site, Page<MatchEntity> page);
RestResult getOlympicMedalByCountryName(String countryName);
RestResult getOlympicPridesByPage(int page, int size);
} }
package com.hocztms.service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hocztms.entity.PrideEntity;
public interface PrideService {
void insertPride(PrideEntity prideEntity);
Page<PrideEntity> getPridesByPage(Page<PrideEntity> page);
}
...@@ -24,23 +24,23 @@ public class MatchServiceImpl implements MatchService { ...@@ -24,23 +24,23 @@ public class MatchServiceImpl implements MatchService {
} }
@Override @Override
public List<MatchEntity> findMatchesByDate(Date date, Page page) { public List<MatchEntity> getMatchesByDate(Date date, Page page) {
QueryWrapper<MatchEntity> wrapper = new QueryWrapper<>(); QueryWrapper<MatchEntity> wrapper = new QueryWrapper<>();
return matchMapper.selectPage(page,wrapper).getRecords(); return matchMapper.selectPage(page,wrapper).getRecords();
} }
@Override @Override
public List<String> findMatchesItems() { public List<String> getMatchesItems() {
return matchMapper.selectItemcodenames(); return matchMapper.selectItemcodenames();
} }
@Override @Override
public List<String> findMatchesVenuename() { public List<String> getMatchesVenuename() {
return matchMapper.selectVenuenames(); return matchMapper.selectVenuenames();
} }
@Override @Override
public Page<MatchEntity> findMatchByKeyword(Date date, String item, String site, Page<MatchEntity> page) { public Page<MatchEntity> getMatchByKeyword(Date date, String item, String site, Page<MatchEntity> page) {
QueryWrapper<MatchEntity> wrapper = new QueryWrapper<>(); QueryWrapper<MatchEntity> wrapper = new QueryWrapper<>();
if (date!=null){ if (date!=null){
wrapper.ge("startdatecn",date); wrapper.ge("startdatecn",date);
......
...@@ -21,8 +21,15 @@ public class MedalServiceImpl implements MedalService { ...@@ -21,8 +21,15 @@ public class MedalServiceImpl implements MedalService {
} }
@Override @Override
public Page<MedalEntity> findMedalsByPage(Page<MedalEntity> page) { public Page<MedalEntity> getMedalsByPage(Page<MedalEntity> page) {
QueryWrapper<MedalEntity> wrapper = new QueryWrapper<>(); QueryWrapper<MedalEntity> wrapper = new QueryWrapper<>();
return medalMapper.selectPage(page,wrapper); return medalMapper.selectPage(page,wrapper);
} }
@Override
public MedalEntity getMedalByCountryName(String countryName) {
QueryWrapper<MedalEntity> wrapper = new QueryWrapper<>();
wrapper.eq("country_name",countryName);
return medalMapper.selectOne(wrapper);
}
} }
...@@ -7,6 +7,7 @@ import com.hocztms.entity.MedalEntity; ...@@ -7,6 +7,7 @@ import com.hocztms.entity.MedalEntity;
import com.hocztms.service.MatchService; import com.hocztms.service.MatchService;
import com.hocztms.service.MedalService; import com.hocztms.service.MedalService;
import com.hocztms.service.OlympicService; import com.hocztms.service.OlympicService;
import com.hocztms.service.PrideService;
import com.hocztms.utils.DateUtils; import com.hocztms.utils.DateUtils;
import com.hocztms.utils.ResultUtils; import com.hocztms.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -20,35 +21,77 @@ public class OlympicServiceImpl implements OlympicService { ...@@ -20,35 +21,77 @@ public class OlympicServiceImpl implements OlympicService {
private MatchService matchService; private MatchService matchService;
@Autowired @Autowired
private MedalService medalService; private MedalService medalService;
@Autowired
private PrideService prideService;
@Override @Override
public RestResult getOlympicTotalMedals(Integer page, Integer size) { public RestResult getOlympicTotalMedals(Integer page, Integer size) {
Page<MedalEntity> medalsByPage = medalService.findMedalsByPage(new Page<>(page, size)); try {
RestResult result = new RestResult(); Page<MedalEntity> medalsByPage = medalService.getMedalsByPage(new Page<>(page, size));
result.putList(medalsByPage.getRecords()); RestResult result = new RestResult();
result.putTotal(medalsByPage.getTotal()); result.putList(medalsByPage.getRecords());
return result; result.putTotal(medalsByPage.getTotal());
return result;
}catch (Exception e){
e.printStackTrace();
return ResultUtils.systemError();
}
} }
@Override @Override
public RestResult getOlympicItems() { public RestResult getOlympicItems() {
return ResultUtils.success(matchService.findMatchesItems()); try {
return ResultUtils.success(matchService.getMatchesItems());
}catch (Exception e){
e.printStackTrace();
return ResultUtils.systemError();
}
} }
@Override @Override
public RestResult getOlympicSite() { public RestResult getOlympicSite() {
return ResultUtils.success(matchService.findMatchesVenuename()); try {
return ResultUtils.success(matchService.getMatchesVenuename());
}catch (Exception e){
e.printStackTrace();
return ResultUtils.systemError();
}
}
@Override
public RestResult getOlympicMatchByKeyword(Date date, String item, String site, Page<MatchEntity> page) {
try {
if (date != null) {
date = DateUtils.getZeroTime(date);
}
Page<MatchEntity> matchByKeyword = matchService.getMatchByKeyword(date, item, site, page);
RestResult result = new RestResult();
result.putList(matchByKeyword.getRecords());
result.putTotal(matchByKeyword.getTotal());
return result;
}catch (Exception e){
e.printStackTrace();
return ResultUtils.systemError();
}
}
@Override
public RestResult getOlympicMedalByCountryName(String countryName) {
try {
MedalEntity medalByCountryName = medalService.getMedalByCountryName(countryName);
return ResultUtils.success(medalByCountryName);
}catch (Exception e){
e.printStackTrace();
return ResultUtils.systemError();
}
} }
@Override @Override
public RestResult findOlympicMatchByKeyword(Date date, String item, String site, Page<MatchEntity> page) { public RestResult getOlympicPridesByPage(int page, int size) {
if (date!=null){ try {
date = DateUtils.getZeroTime(date); return ResultUtils.success(prideService.getPridesByPage(new Page<>(page,size)));
}catch (Exception e){
return ResultUtils.systemError();
} }
Page<MatchEntity> matchByKeyword = matchService.findMatchByKeyword(date, item, site, page);
RestResult result = new RestResult();
result.putList(matchByKeyword.getRecords());
result.putTotal(matchByKeyword.getTotal());
return result;
} }
} }
package com.hocztms.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hocztms.entity.PrideEntity;
import com.hocztms.mapper.PrideMapper;
import com.hocztms.service.PrideService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class PrideServiceImpl implements PrideService {
@Autowired
private PrideMapper prideMapper;
@Override
public void insertPride(PrideEntity prideEntity) {
prideMapper.insert(prideEntity);
}
@Override
public Page<PrideEntity> getPridesByPage(Page<PrideEntity> page) {
return prideMapper.selectPage(page,new QueryWrapper<>());
}
}
package com.hocztms.utils; package com.hocztms.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ClientProtocolException;
...@@ -13,44 +14,47 @@ import org.apache.http.util.EntityUtils; ...@@ -13,44 +14,47 @@ import org.apache.http.util.EntityUtils;
import java.io.IOException; import java.io.IOException;
@Slf4j
public class HttpUtils { public class HttpUtils {
//模拟代理 //模拟代理
private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"; private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36";
public static String sendGet(String url){ public static String sendGet(String url){
//1.生成httpclient,相当于该打开一个浏览器 //生成httpclient,相当于该打开一个浏览器
CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpClient httpClient = HttpClients.createDefault();
//设置请求和传输超时时间 //设置请求和传输超时时间(工厂模式)
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build(); RequestConfig requestConfig = RequestConfig
.custom()
.setSocketTimeout(2000)
.setConnectTimeout(2000)
.build();
CloseableHttpResponse response = null; CloseableHttpResponse response = null;
String html = null; String result = null;
//2.创建get请求,相当于在浏览器地址栏输入 网址 //创建get请求
HttpGet request = new HttpGet(url); HttpGet request = new HttpGet(url);
try { try {
//设置用户代理
request.setHeader("User-Agent",USER_AGENT); request.setHeader("User-Agent",USER_AGENT);
request.setConfig(requestConfig); request.setConfig(requestConfig);
//3.执行get请求,相当于在输入地址栏后敲回车键 //执行get请求
response = httpClient.execute(request); response = httpClient.execute(request);
//4.判断响应状态为200,进行处理 //判断响应状态是否为200,进行处理
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//5.获取响应内容 //获取响应内容
HttpEntity httpEntity = response.getEntity(); HttpEntity httpEntity = response.getEntity();
html = EntityUtils.toString(httpEntity, "GBK"); result = EntityUtils.toString(httpEntity, "GBK");
} else { } else {
//如果返回状态不是200,比如404(页面不存在)等,根据情况做处理,这里略 //如果返回状态不是200,比如404(页面不存在)等,根据情况做处理,这里略
System.out.println("返回状态不是200"); log.info("请求码为:"+response.getStatusLine().getStatusCode());
System.out.println(EntityUtils.toString(response.getEntity(), "utf-8")); log.info(EntityUtils.toString(response.getEntity(), "utf-8"));
} }
} catch (ClientProtocolException e) { } catch (Exception e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
//6.关闭 //finally关闭
HttpClientUtils.closeQuietly(response); HttpClientUtils.closeQuietly(response);
HttpClientUtils.closeQuietly(httpClient); HttpClientUtils.closeQuietly(httpClient);
} }
return html; return result;
} }
} }
...@@ -7,23 +7,12 @@ spring: ...@@ -7,23 +7,12 @@ spring:
password: a1598753aA password: a1598753aA
hikari: hikari:
max-lifetime: 500000 max-lifetime: 500000
thymeleaf:
prefix: classpath:/static/
# # cache: mvc:
# # type: redis throw-exception-if-no-handler-found: true # 告诉 SpringBoot 当出现 404 错误时, 直接抛出异常
# redis: resources:
# host: 47.106.84.138 add-mappings: false # 告诉 SpringBoot 不要为我们工程中的资源文件建立映射
# port: 6378
# jedis:
# pool:
# max-active: 8
# max-wait: 1000
# max-idle: 30
# min-idle: 10
# lettuce:
# shutdown-timeout: 0
# timeout: 1200
# password: password
server: server:
port: 8080 port: 8080
tomcat: tomcat:
......
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite App</title>
<script type="module" crossorigin src="/assets/index.99cb95ac.js"></script>
<link rel="modulepreload" href="/assets/echarts.033126e5.js">
<link rel="stylesheet" href="/assets/index.01e6d014.css">
</head>
<body>
<div id="app"></div>
</body>
</html>
...@@ -19,7 +19,7 @@ class OlympicServerApplicationTests { ...@@ -19,7 +19,7 @@ class OlympicServerApplicationTests {
private MatchMapper matchMapper; private MatchMapper matchMapper;
@Test @Test
void contextLoads() { void contextLoads() {
// olympicUtils.initiateOlympicMedals(); /*olympicUtils.initiateOlympicMedals();*/
// System.out.println(matchMapper.selectItemcodenames()); // System.out.println(matchMapper.selectItemcodenames());
// olympicUtils.initiateOlympicMatches(); // olympicUtils.initiateOlympicMatches();
} }
......
## Java后端代码规范 (来源:阿里巴巴Java开发手册 1.3.0)
### 1命名风格
**1.1**类名采用使用 UpperCamelCase (首字母大写)风格,遵守驼峰命名形式,但以下情况例外:DO / BO /
DTO / VO / AO
**1.2**方法名、参数名、成员变量、局部变量都统一使用 lowerCamelCase 风格,必须遵从驼峰形式
**1.3**常量命名全部大写,单词间用下划线隔开,力求语义表达完整清楚,不要嫌名字长
**1.4**抽象类命名使用 Abstract 或 Base 开头;异常类命名使用 Exception 结尾;测试类命名以它要测试的类的名称开始,以 Test 结尾。
**1.5**对于 Service 和 DAO 类,基于 SOA 的理念,暴露出来的服务一定是接口,内部实现类用 Impl 的后缀与接口区别。
**1.6**各层命名规约:
A) Service/DAO 层方法命名规约
1) 获取单个对象的方法用 get 做前缀。
2) 获取多个对象的方法用 list 做前缀。
3) 获取统计值的方法用 count 做前缀。
4) 插入的方法用 save/insert 做前缀。
5) 删除的方法用 remove/delete 做前缀。
6) 修改的方法用 update 做前缀。
注意:
- 代码中的命名严禁使用拼音与英文混合的方式,更不允许直接使用中文的方式。
- POJO 类中布尔类型的变量,都不要加 is,否则部分框架解析会引起序列化错误。
- 常量都必须是 static final 成员,但并不是所有的 static final 成员都是常量。
- 不要使用类似 C 语言的数组变量声明。
## 2代码风格
**2.1**大括号的使用约定。如果是大括号内为空,则简洁地写成{}即可,不需要换行;如果
是非空代码块则:
1) 左大括号前不换行。
2) 左大括号后换行。
3) 右大括号前换行。
4) 右大括号后还有 else 等代码则不换行;表示终止的右大括号后必须换行。
**2.2**if/for/while/switch/do 等保留字与括号之间都必须加空格。
**2.3**采用 4 个空格缩进,如果使用 tab 字符需要设置4空格缩进
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册