第一个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).@Value("${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
posted @ 2018-06-28 20:29  qqq齐  阅读(116)  评论(0编辑  收藏  举报