diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2af7cef --- /dev/null +++ b/.gitignore @@ -0,0 +1,24 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +nbproject/private/ +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..7b8f45f --- /dev/null +++ b/pom.xml @@ -0,0 +1,107 @@ + + + 4.0.0 + + com.fjy + springboot + 0.0.1-SNAPSHOT + jar + + springboot + Learn project for Spring Boot Base + + + org.springframework.boot + spring-boot-starter-parent + 2.0.0.M7 + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.boot + spring-boot-starter-data-jpa + 1.5.9.RELEASE + + + + mysql + mysql-connector-java + 5.1.45 + + + + org.springframework.boot + spring-boot-starter-aop + 1.5.9.RELEASE + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + diff --git a/src/main/java/com/fjy/springboot/SpringbootApplication.java b/src/main/java/com/fjy/springboot/SpringbootApplication.java new file mode 100644 index 0000000..6dc613c --- /dev/null +++ b/src/main/java/com/fjy/springboot/SpringbootApplication.java @@ -0,0 +1,12 @@ +package com.fjy.springboot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringbootApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringbootApplication.class, args); + } +} diff --git a/src/main/java/com/fjy/springboot/aspect/HttpAspect.java b/src/main/java/com/fjy/springboot/aspect/HttpAspect.java new file mode 100644 index 0000000..c404b1a --- /dev/null +++ b/src/main/java/com/fjy/springboot/aspect/HttpAspect.java @@ -0,0 +1,43 @@ +package com.fjy.springboot.aspect; + +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.annotation.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import javax.servlet.http.HttpServletRequest; + +@Aspect +@Component +public class HttpAspect { + private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class); + + @Pointcut("execution(public * com.fjy.springboot.controller.PersonController.*(..))") + public void log(){ + } + @Before("log()") + public void doBefore(JoinPoint joinPoint){ + logger.info("请求执行PersonController"); + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + HttpServletRequest request = attributes.getRequest(); + logger.info("url={}",request.getRequestURL()); + logger.info("method={}",request.getMethod()); + logger.info("ip={}",request.getRemoteAddr()); + logger.info("class_method={}",joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName()); + logger.info("args={}",joinPoint.getArgs()); + //System.out.println("请求执行PersonController"); + } + @After("log()") + public void doAfter(){ + logger.info("结束对PersonController的请求"); + //System.out.println("结束对PersonController的请求"); + } + + @AfterReturning(returning = "object",pointcut = "log()") + public void doAfterReturning(Object object){ + logger.info("response={}",object.toString()); + } +} diff --git a/src/main/java/com/fjy/springboot/controller/HelloController.java b/src/main/java/com/fjy/springboot/controller/HelloController.java new file mode 100644 index 0000000..9476e35 --- /dev/null +++ b/src/main/java/com/fjy/springboot/controller/HelloController.java @@ -0,0 +1,38 @@ +package com.fjy.springboot.controller; + +import com.fjy.springboot.properties.PersonProperties; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.*; + +@RestController +public class HelloController { + /** + * 从yml配置文件注入 + */ + @Value("${content}") + private String content; + /** + * 按组注入,推荐 + */ + @Autowired + private PersonProperties personProperties; + + @RequestMapping(value = {"/hello"},method = RequestMethod.GET) + public String say(){ + // return content; + return personProperties.toString(); + } + @RequestMapping(value = {"/tell/{id}"},method = RequestMethod.POST) + public String tell(@PathVariable("id") Integer id){ + // return content; + return "id="+id; + } + //@RequestMapping(value = {"/tell"},method = RequestMethod.POST) + //@GetMapping("/tell") + @PostMapping("/tell") + public String tellParam(@RequestParam(value = "id",required = false,defaultValue = "0") Integer param){ + // return content; + return "id="+param; + } +} diff --git a/src/main/java/com/fjy/springboot/controller/LoginController.java b/src/main/java/com/fjy/springboot/controller/LoginController.java new file mode 100644 index 0000000..79d40d9 --- /dev/null +++ b/src/main/java/com/fjy/springboot/controller/LoginController.java @@ -0,0 +1,13 @@ +package com.fjy.springboot.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; + +@Controller +public class LoginController { + @GetMapping("/login") + public String doLogin(){ + return "/index"; + } + +} diff --git a/src/main/java/com/fjy/springboot/domain/Result.java b/src/main/java/com/fjy/springboot/domain/Result.java new file mode 100644 index 0000000..a8d7ee6 --- /dev/null +++ b/src/main/java/com/fjy/springboot/domain/Result.java @@ -0,0 +1,34 @@ +package com.fjy.springboot.domain; + +/** + * Http请求返回的最外层对象 + */ +public class Result { + private Integer code;//错误码 + private String message;//错误信息 + private T data;//具体内容 + + public Integer getCode() { + return code; + } + + public void setCode(Integer code) { + this.code = code; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } +} diff --git a/src/main/java/com/fjy/springboot/domain/person.java b/src/main/java/com/fjy/springboot/domain/person.java new file mode 100644 index 0000000..421b8bf --- /dev/null +++ b/src/main/java/com/fjy/springboot/domain/person.java @@ -0,0 +1,70 @@ +package com.fjy.springboot.domain; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.validation.constraints.Min; + +@Entity +public class person { + /** + * 加入主键和自增的注解,用于自动建表 + */ + @Id + @GeneratedValue + private Integer id; + + private String name; + @Min(value = 0,message = "年龄必须大于0") + private Integer age; + + private String content; + + public Integer getId() { + return id; + } + + public String getName() { + return name; + } + + public Integer getAge() { + return age; + } + + public String getContent() { + return content; + } + + public void setId(Integer id) { + this.id = id; + } + + public void setName(String name) { + this.name = name; + } + + public void setAge(Integer age) { + this.age = age; + } + + public void setContent(String content) { + this.content = content; + } + + @Override + public String toString() { + return "person{" + + "id=" + id + + ", name='" + name + '\'' + + ", age=" + age + + ", content='" + content + '\'' + + '}'; + } + + /** + * 必须显示指定空的构造方法 + */ + public person() { + } +} diff --git a/src/main/java/com/fjy/springboot/enums/ResultEnum.java b/src/main/java/com/fjy/springboot/enums/ResultEnum.java new file mode 100644 index 0000000..be531e8 --- /dev/null +++ b/src/main/java/com/fjy/springboot/enums/ResultEnum.java @@ -0,0 +1,25 @@ +package com.fjy.springboot.enums; + +public enum ResultEnum { + UNKNOW_ERROR(-1,"未知错误"), + SUCCESS(0,"成功"), + PRIMARY_SCHOOL(100,"小学生~"), + MIDDLE_SCHOOL(101,"中学生!") + ; + + private Integer code; + private String msg; + + ResultEnum(Integer code, String msg) { + this.code = code; + this.msg = msg; + } + + public Integer getCode() { + return code; + } + + public String getMsg() { + return msg; + } +} diff --git a/src/main/java/com/fjy/springboot/exception/PersonException.java b/src/main/java/com/fjy/springboot/exception/PersonException.java new file mode 100644 index 0000000..9b8f79f --- /dev/null +++ b/src/main/java/com/fjy/springboot/exception/PersonException.java @@ -0,0 +1,21 @@ +package com.fjy.springboot.exception; + +import com.fjy.springboot.enums.ResultEnum; + +public class PersonException extends RuntimeException { + + private Integer code; + + public PersonException(ResultEnum resultEnum) { + super(resultEnum.getMsg()); + this.code = resultEnum.getCode(); + } + + public Integer getCode() { + return code; + } + + public void setCode(Integer code) { + this.code = code; + } +} diff --git a/src/main/java/com/fjy/springboot/handle/ExceptionHandle.java b/src/main/java/com/fjy/springboot/handle/ExceptionHandle.java new file mode 100644 index 0000000..cd98b64 --- /dev/null +++ b/src/main/java/com/fjy/springboot/handle/ExceptionHandle.java @@ -0,0 +1,32 @@ +package com.fjy.springboot.handle; + +import com.fjy.springboot.exception.PersonException; +import com.fjy.springboot.domain.Result; +import com.fjy.springboot.untils.ResultUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; + + + +@ControllerAdvice +public class ExceptionHandle { + + private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class); + + @ExceptionHandler(value = Exception.class)//指定捕获的异常类 + @ResponseBody//统一返回json格式 + public Result handle(Exception e){ + if (e instanceof PersonException){ + PersonException personException = (PersonException)e; + return ResultUtil.error(personException.getCode(),personException.getMessage()); + }else { + logger.error("系统异常{}",e); + return ResultUtil.error(-1,"未知错误"); + } + + } +} + diff --git a/src/main/java/com/fjy/springboot/properties/PersonProperties.java b/src/main/java/com/fjy/springboot/properties/PersonProperties.java new file mode 100644 index 0000000..491f7e5 --- /dev/null +++ b/src/main/java/com/fjy/springboot/properties/PersonProperties.java @@ -0,0 +1,47 @@ +package com.fjy.springboot.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +@Component +@ConfigurationProperties(prefix = "person") +public class PersonProperties { + private String name; + + private Integer age; + + private String content; + + public String getName() { + return name; + } + + public Integer getAge() { + return age; + } + + public String getContent() { + return content; + } + + public void setName(String name) { + this.name = name; + } + + public void setAge(Integer age) { + this.age = age; + } + + public void setContent(String content) { + this.content = content; + } + + @Override + public String toString() { + return "PersonProperties{" + + "name='" + name + '\'' + + ", age=" + age + + ", content='" + content + '\'' + + '}'; + } +} diff --git a/src/main/java/com/fjy/springboot/repository/PersonRepository.java b/src/main/java/com/fjy/springboot/repository/PersonRepository.java new file mode 100644 index 0000000..7f94cf2 --- /dev/null +++ b/src/main/java/com/fjy/springboot/repository/PersonRepository.java @@ -0,0 +1,13 @@ +package com.fjy.springboot.repository; + +import com.fjy.springboot.domain.person; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface PersonRepository extends JpaRepository { + /** + * 继承jpa,泛型为类名和主键类型 + */ + public List findByAge(Integer age); +} diff --git a/src/main/java/com/fjy/springboot/service/PersonService.java b/src/main/java/com/fjy/springboot/service/PersonService.java new file mode 100644 index 0000000..9fb61bb --- /dev/null +++ b/src/main/java/com/fjy/springboot/service/PersonService.java @@ -0,0 +1,37 @@ +package com.fjy.springboot.service; + +import com.fjy.springboot.enums.ResultEnum; +import com.fjy.springboot.exception.PersonException; +import com.fjy.springboot.domain.person; +import com.fjy.springboot.repository.PersonRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Optional; + +@Service +public class PersonService { + + @Autowired + private PersonRepository personRepository; + + public void getAge(Integer id)throws Exception{ + Optional opt = personRepository.findById(id); + person per = (person)opt.get(); + Integer age = per.getAge(); + if (age < 10){ + throw new PersonException(ResultEnum.PRIMARY_SCHOOL); + }else if(age>10&&age <16) { + throw new PersonException(ResultEnum.MIDDLE_SCHOOL); + } + } + + /** + * 通过ID查一个人 + * @param id + * @return + */ + public person findOne(Integer id){ + return personRepository.findById(id).get(); + } +} diff --git a/src/main/java/com/fjy/springboot/untils/ResultUtil.java b/src/main/java/com/fjy/springboot/untils/ResultUtil.java new file mode 100644 index 0000000..4f088b0 --- /dev/null +++ b/src/main/java/com/fjy/springboot/untils/ResultUtil.java @@ -0,0 +1,30 @@ +package com.fjy.springboot.untils; + +import com.fjy.springboot.domain.Result; + +public class ResultUtil { + public static Result success(Object object){ + Result result = new Result(); + result.setMessage("成功"); + result.setCode(0); + result.setData(object); + return result; + } + public static Result success(){ + return success(null); + } + public static Result error(Integer code,String msg){ + Result result = new Result(); + result.setMessage("失败"); + result.setCode(code); + result.setData(msg); + return result; + } + public static Result info(Integer code,String msg){ + Result result = new Result(); + result.setMessage("提示"); + result.setCode(code); + result.setData(msg); + return result; + } +} diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml new file mode 100644 index 0000000..f1755f4 --- /dev/null +++ b/src/main/resources/application-dev.yml @@ -0,0 +1,25 @@ +#开发环境配置文件 +server: + servlet: + context-path: /spring + port: 8080 +name: fjy +age: 18 +content: "My name is ${name},age ${age}!" +person: + name: fjy8018 + age: 18 + content: Spring Boot! +spring: + datasource: + driver-class-name: com.mysql.jdbc.Driver + url: jdbc:mysql://localhost:3306/db_school?useUnicode=true&characterEncoding=utf-8&useSSL=true + username: vantenuser + password: vanten + + jpa: + hibernate: + ddl-auto: update + show-sql: true +debug: true + diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml new file mode 100644 index 0000000..0bb439c --- /dev/null +++ b/src/main/resources/application-prod.yml @@ -0,0 +1,13 @@ +#运行环境配置文件 +server: + servlet: + context-path: /spring + port: 8080 +name: fjy +age: 20 +content: "My name is ${name},age ${age}!" + +person: + name: fjy8018 + age: 20 + content: Spring Boot \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..c9209b1 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +#server.servlet.context-path=/spring \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..9efaa1c --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,4 @@ +#控制配置文件调用 +spring: + profiles: + active: dev \ No newline at end of file diff --git a/src/main/resources/static/index.html b/src/main/resources/static/index.html new file mode 100644 index 0000000..0ab3572 --- /dev/null +++ b/src/main/resources/static/index.html @@ -0,0 +1,10 @@ + + + + + 登录页面 + + +

登录页面

+ + \ No newline at end of file diff --git a/src/test/java/com/fjy/springboot/PersonServiceTest.java b/src/test/java/com/fjy/springboot/PersonServiceTest.java new file mode 100644 index 0000000..2e487b8 --- /dev/null +++ b/src/test/java/com/fjy/springboot/PersonServiceTest.java @@ -0,0 +1,28 @@ +package com.fjy.springboot; + +import com.fjy.springboot.domain.person; +import com.fjy.springboot.service.PersonService; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * 单元测试类 + * 加入注解表示要在测试环境中运行 + * 第二个表示启动整个Spring工程 + */ +@RunWith(SpringRunner.class) +@SpringBootTest +public class PersonServiceTest { + @Autowired + private PersonService personService; + + @Test + public void findOneTest(){ + person per = personService.findOne(8); + Assert.assertEquals(new Integer(14),per.getAge());//断言,写入期望值 + } +} diff --git a/src/test/java/com/fjy/springboot/SpringbootApplicationTests.java b/src/test/java/com/fjy/springboot/SpringbootApplicationTests.java new file mode 100644 index 0000000..c4c33fa --- /dev/null +++ b/src/test/java/com/fjy/springboot/SpringbootApplicationTests.java @@ -0,0 +1,16 @@ +package com.fjy.springboot; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class SpringbootApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/src/test/java/com/fjy/springboot/controller/PersonControllerTest.java b/src/test/java/com/fjy/springboot/controller/PersonControllerTest.java new file mode 100644 index 0000000..c477f10 --- /dev/null +++ b/src/test/java/com/fjy/springboot/controller/PersonControllerTest.java @@ -0,0 +1,30 @@ +package com.fjy.springboot.controller; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; + +import static org.junit.Assert.*; + +/** + * API测试控制类 + */ +@RunWith(SpringRunner.class) +@SpringBootTest +@AutoConfigureMockMvc +public class PersonControllerTest { + + @Autowired + private MockMvc mvc; + + @Test + public void personList()throws Exception { + //mvc.perform(MockMvcRequestBuilders.get("/persons")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().string("")); + } +} \ No newline at end of file diff --git a/src/test/java/com/fjy/springboot/service/PersonServiceTest.java b/src/test/java/com/fjy/springboot/service/PersonServiceTest.java new file mode 100644 index 0000000..65c2070 --- /dev/null +++ b/src/test/java/com/fjy/springboot/service/PersonServiceTest.java @@ -0,0 +1,15 @@ +package com.fjy.springboot.service; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * IDEA自动生成的测试类 + */ +public class PersonServiceTest { + + @Test + public void findOne() { + } +} \ No newline at end of file