第一个spring-boot项目

Posted qqq齐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第一个spring-boot项目相关的知识,希望对你有一定的参考价值。

1.配置文件  application.yml
spring:
profiles:
active: dev
datasource:
driver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@localhost:1521:xe
username: system
password: 123456
jpa:
hibernate:
ddl-auto: update
show-sql: true

1.读取配置文件的内容.

server:
  port: 8081
  context-path: /boot

service:
  name: freya
version: 1.1

 (1)[email protected]("${service.name}") 

      private String name ;

  (2).使用Environment,env.getProperty("键名")

           @Autowired
    private Environment env;

    @RequestMapping(value = "/hello")
    public String hello() {
        return "hello spring boot! " + env.getProperty("service.name");
    }
(3)
自定义一个实体类
@Component
@ConfigurationProperties(prefix = "project")  //配置文件中前缀
@PropertySource(value = "classpath:config/config.properties") //文件位置
public class MyConfig {
    private String version;
    private String name;

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
在controller中获取
RestController
public class HelloController {

    @Autowired
    private MyConfig myConfig;

    @RequestMapping(value = "/hello")
    public String hello() {
        return "hello spring boot! " myConfig.getVersion() + " " +  myConfig.getName() ;
    }
}

2.表单提交

controller层
@PostMapping("/save")
public Girl save(@Valid Girl girl, BindingResult bindingResult){
if(bindingResult.hasErrors()){
System.out.print(bindingResult.getFieldError().getDefaultMessage());
return null;
}
girl.setAge(girl.getAge());
girl.setCupSize(girl.getCupSize());
girl.setMoney(girl.getMoney());
return girlRepository.save(girl);
}

  实体类

@Entity  //对应数据库中的表
public class Girl{

@Id //主键
@GeneratedValue 自增,从1开始
private Integer id;

@Min(value = 18,message = "未成年") //最小值为18,不满足返回message的值
private Integer age;
private String cupSize;

@Min(value = 0,message = "金额必传")
private Double money;

 3.自定义异常  (codeEnum为枚举类型,方便管理异常状态码与返回信息)

public class GirlException extends RuntimeException{
private String respCode;

public GirlException(CodeEnum codeEnum) {
super(codeEnum.getMsg());
this.respCode = codeEnum.getCode();
}
public GirlException(String message,String code) {
super(message);
this.respCode = code;
}
public String getRespCode() {
return respCode;
}

public void setRespCode(String respCode) {
this.respCode = respCode;
}
}

//枚举实体public enum CodeEnum {
    ERROR_CODE("-1","系统异常"),
OK_CODE("0","OK"),
PRE_SCHOOL("100","你可能还在上小学吧"),
HIGH_SCHOOL("101","你可能还在上中学吧");

CodeEnum(String code, String msg) {
this.code = code;
this.msg = msg;
}
private String code;
private String msg;

public String getCode() {
return code;
}

public String getMsg() {
return msg;
}
}
/**
 * 捕捉异常类  (handle)
*/
@ControllerAdvice
public class ExceptionHandle {

@ExceptionHandler
@ResponseBody
public ResultMap handle(Exception e){
if(e instanceof GirlException){
GirlException girlException = (GirlException) e;
return ResultMap.error(girlException.getRespCode(),girlException.getMessage(),"");
}
return ResultMap.error(e.getMessage());
}
}

/**
* spring-boot ao
*/
@Aspect
@Component
public class HttpAspect {

private static final Logger log = LoggerFactory.getLogger(HttpAspect.class); //spring中自带的日志包

@Pointcut("execution(* com.girl.controller.GirlController.*(..))")
public void log(){}

@Before("log()")
public void log1(){
//获取request请求
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
HttpServletRequest request = sra.getRequest();
log.info("id={}",request.getRemoteAddr()); //日志信息将输出在{}中

}
}

多模块部署:
https://www.imooc.com/video/16354

以上是关于第一个spring-boot项目的主要内容,如果未能解决你的问题,请参考以下文章

使用 Spring-Boot 创建一个简单的 JAR 而不是可执行的 JAR

第一个spring-boot项目

Spring Boot Maven Plugin -- repackage目标

intellij idea 2017 gradle构建spring-boot web项目

如何解决tomcat的问题? Spring-Boot“扫描失败”

现有前面迁移到Spring-boot