Skip to content

Commit ef2f863

Browse files
committed
新增-慕课网《SpringBoot进阶之Web进阶》学习源码
1 parent 6d8d1de commit ef2f863

File tree

28 files changed

+898
-0
lines changed

28 files changed

+898
-0
lines changed

07-my-myspringbootweb/.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
12+
### IntelliJ IDEA ###
13+
.idea
14+
*.iws
15+
*.iml
16+
*.ipr
17+
18+
### NetBeans ###
19+
nbproject/private/
20+
build/
21+
nbbuild/
22+
dist/
23+
nbdist/
24+
.nb-gradle/

07-my-myspringbootweb/pom.xml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.zccoder</groupId>
7+
<artifactId>myspringboot</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>myspringboot</name>
12+
<description>MySpringBoot Project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.1.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-web</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-data-jpa</artifactId>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-starter-aop</artifactId>
52+
</dependency>
53+
54+
<dependency>
55+
<groupId>mysql</groupId>
56+
<artifactId>mysql-connector-java</artifactId>
57+
<version>5.1.38</version>
58+
</dependency>
59+
60+
</dependencies>
61+
62+
<build>
63+
<plugins>
64+
<plugin>
65+
<groupId>org.springframework.boot</groupId>
66+
<artifactId>spring-boot-maven-plugin</artifactId>
67+
</plugin>
68+
</plugins>
69+
</build>
70+
71+
72+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.myimooc.springbootweb;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class MyspringbootApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(MyspringbootApplication.class, args);
11+
}
12+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.myimooc.springbootweb.aspect;
2+
3+
import org.aspectj.lang.JoinPoint;
4+
import org.aspectj.lang.annotation.*;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.stereotype.Component;
8+
import org.springframework.web.context.request.RequestContextHolder;
9+
import org.springframework.web.context.request.ServletRequestAttributes;
10+
11+
import javax.servlet.http.HttpServletRequest;
12+
13+
/**
14+
*
15+
* @author ZhangCheng
16+
* @date 2017-03-19
17+
* @version V1.0
18+
*
19+
*/
20+
@Aspect
21+
@Component
22+
public class HttpAspect {
23+
24+
/** 日志 */
25+
private final static Logger LOGGER = LoggerFactory.getLogger(HttpAspect.class);
26+
27+
/** 定义切点 */
28+
@Pointcut("execution(public * com.myimooc.springbootweb.web.controller.GirlController.*(..))")
29+
public void log(){}
30+
31+
/** 前置通知 */
32+
@Before("log()")
33+
public void doBefore(JoinPoint joinPoint){
34+
ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
35+
HttpServletRequest request = attributes.getRequest();
36+
37+
// url
38+
LOGGER.info("url={}",request.getRequestURI());
39+
40+
// method
41+
LOGGER.info("method={}",request.getMethod());
42+
43+
// ip
44+
LOGGER.info("ip={}",request.getRemoteAddr());
45+
46+
// 类方法
47+
LOGGER.info("class_method={}",joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
48+
49+
// 参数
50+
LOGGER.info("args={}",joinPoint.getArgs());
51+
}
52+
53+
/** 后置通知 */
54+
@After("log()")
55+
public void doAfter(){
56+
LOGGER.info("方法执行之后执行");
57+
}
58+
59+
/** 正常返回通知 */
60+
@AfterReturning(returning = "object",pointcut = "log()")
61+
public void doAfterReturning(Object object){
62+
// LOGGER.info("response={}",object.toString());
63+
}
64+
65+
}
66+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.myimooc.springbootweb.exception;
2+
3+
import com.myimooc.springbootweb.utils.ResultResp;
4+
5+
/**
6+
* 自定义项目异常类
7+
* @author ZhangCheng
8+
* @date 2017-03-19
9+
* @version V1.0
10+
*
11+
*/
12+
public class RespException extends RuntimeException{
13+
/** 响应编号 */
14+
private Integer respCode;
15+
16+
public RespException(ResultResp resultResp) {
17+
super(resultResp.getRespMsg());
18+
this.respCode = resultResp.getRespCode();
19+
}
20+
21+
public Integer getRespCode() {
22+
return respCode;
23+
}
24+
25+
public void setRespCode(Integer respCode) {
26+
this.respCode = respCode;
27+
}
28+
}
29+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.myimooc.springbootweb.handle;
2+
3+
import com.myimooc.springbootweb.exception.RespException;
4+
import com.myimooc.springbootweb.model.entity.Result;
5+
import com.myimooc.springbootweb.utils.ResultUtil;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.web.bind.annotation.ControllerAdvice;
9+
import org.springframework.web.bind.annotation.ExceptionHandler;
10+
import org.springframework.web.bind.annotation.ResponseBody;
11+
12+
/**
13+
* 异常捕获类
14+
* @author ZhangCheng
15+
* @date 2017-03-19
16+
* @version V1.0
17+
*
18+
*/
19+
@ControllerAdvice
20+
public class ExceptionHandle {
21+
22+
private final static Logger LOGGER = LoggerFactory.getLogger(ExceptionHandle.class);
23+
24+
/**
25+
* 全局异常返回处理
26+
* @param e 异常
27+
* @return 处理后的返回结果
28+
*/
29+
@ExceptionHandler(value = Exception.class)
30+
@ResponseBody
31+
public Result handle(Exception e){
32+
if (e instanceof RespException){
33+
RespException respException = (RespException)e;
34+
return ResultUtil.error(respException.getRespCode(),respException.getMessage());
35+
}
36+
LOGGER.error("【系统异常】{}",e);
37+
return ResultUtil.error(ResultUtil.RESPCODE_ERROR_SERVICE,"未知错误");
38+
}
39+
40+
}
41+
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.myimooc.springbootweb.model.entity;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.Id;
6+
import javax.validation.constraints.Min;
7+
import javax.validation.constraints.NotNull;
8+
9+
/**
10+
* Created by ChengComputer on 2017/2/18.
11+
*/
12+
@Entity(name= "TAM_GIRL_INFO")
13+
public class Girl {
14+
15+
@Id
16+
@GeneratedValue
17+
private Integer id;
18+
19+
private String cupSize;
20+
21+
@Min(value = 18,message = "未成年少女禁止入内")
22+
private Integer age;
23+
24+
@NotNull(message = "金额必传")
25+
private Integer money;
26+
27+
public Girl() {
28+
}
29+
30+
public Integer getId() {
31+
return id;
32+
}
33+
34+
public void setId(Integer id) {
35+
this.id = id;
36+
}
37+
38+
public String getCupSize() {
39+
return cupSize;
40+
}
41+
42+
public void setCupSize(String cupSize) {
43+
this.cupSize = cupSize;
44+
}
45+
46+
public Integer getAge() {
47+
return age;
48+
}
49+
50+
public void setAge(Integer age) {
51+
this.age = age;
52+
}
53+
54+
public Integer getMoney() {
55+
return money;
56+
}
57+
58+
public void setMoney(Integer money) {
59+
this.money = money;
60+
}
61+
62+
@Override
63+
public String toString() {
64+
return "Girl{" +
65+
"id=" + id +
66+
", cupSize='" + cupSize + '\'' +
67+
", age=" + age +
68+
", money=" + money +
69+
'}';
70+
}
71+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.myimooc.springbootweb.model.entity;
2+
3+
/**
4+
* http请求返回的实体Bean
5+
* @author ZhangCheng
6+
* @date 2017-03-19
7+
* @version V1.0
8+
*/
9+
public class Result<T> {
10+
11+
/** 响应编号 */
12+
private Integer respCode;
13+
14+
/** 响应消息 */
15+
private String respMsg;
16+
17+
/** 具体的内容 */
18+
private T data;
19+
20+
public Integer getRespCode() {
21+
return respCode;
22+
}
23+
24+
public void setRespCode(Integer respCode) {
25+
this.respCode = respCode;
26+
}
27+
28+
public String getRespMsg() {
29+
return respMsg;
30+
}
31+
32+
public void setRespMsg(String respMsg) {
33+
this.respMsg = respMsg;
34+
}
35+
36+
public T getData() {
37+
return data;
38+
}
39+
40+
public void setData(T data) {
41+
this.data = data;
42+
}
43+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.myimooc.springbootweb.repository;
2+
3+
import com.myimooc.springbootweb.model.entity.Girl;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
import java.util.List;
7+
8+
/**
9+
* Created by ChengComputer on 2017/2/18.
10+
* @author zhangcheng
11+
* @version v1.0
12+
* @date 2017-02-18
13+
*/
14+
public interface GirlRepository extends JpaRepository<Girl,Integer>{
15+
16+
/**
17+
* 通过年龄来查询
18+
* @param age
19+
* @return
20+
*/
21+
public List<Girl> findByAge(Integer age);
22+
}

0 commit comments

Comments
 (0)