springboot框架学习(1)

前言

下面简单介绍学习过程

一、model(或者entity)模型层或者实体层

package com.xncoding.pos.model;

/**
 * dog对象:
 */
public class Dog {

    private String color;
    private Long id;
    private String name;
    private Integer age;


    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }




}

二、controller控制层

控制访问层接口,例如网站网址,就是每个功能的入口处,一个功能一个路径

@RestController
@RequestMapping(value = "/dogs")
public class DogController {
    /**
     *  @Autowired:意思是注入DogService,然后就可以使用他
     */
    @Autowired
    private DogService dogService;


    @RequestMapping(value = "/dogeat/{type}", method = RequestMethod.GET)
    public int dogeat(@PathVariable String type) {

        int a = dogService.eatWeight(type);//调用dogService接口的eatWeight方法
        return a;
    }
}

 注解解释:

(1)@RestController:这个注解是标志这个class是一个控制层的类

 (2)@RequestMapping(value = "/dogeat/{type}", method = RequestMethod.GET):接口路径注解,value是具体路径名,method是请求的方式(GET或者POST)

(3) @Autowired:注入注解,注入就可以使用

三、service业务层

interface接口类(三大概念之一)接口定义的方法有具体的一个实现类,比如定义了一个方法eatWeight方法,他的实现类就在serviceImpl包下面,实现了这个DogService接口的DogServiceImpl业务类中。

接口类的关键字就是interface

实现接口的关键字是implements

/**
 * dog接口(三大概念之一)
 */
public interface DogService {
    /**
     * 根据type类型返回吃重量(方法)
     * @param type 传入的类型
     * @return 返回int类型的重量
     */
    int eatWeight(String type);
}

serviceImpl业务具体实现类层

@Service
public class DogServiceImpl implements DogService {

    /**
     *  @Override:这个意思是重写父类的方法
     * @param type 传入的类型
     * @return
     */
    @Override
    public int eatWeight(String type) {
        if (type.equalsIgnoreCase("饭")) {
            return 1;
        } else if (type.equalsIgnoreCase("水")) {
            return 2;
        }
        return 0;
    }
}

 注解解释

(1)@Service:表明是个业务类

(2) @Override:是个重写父类方法的注解

四、配置文件application.yml


###################  项目启动端口  ###################
server.port: 8092

###################  spring配置  ###################
spring:
  profiles:
    active: dev

logging:
  level:
    org.springframework.web.servlet: ERROR

---

#####################################################################
########################  开发环境profile  ##########################
#####################################################################
spring:
  profiles: dev

logging:
  level:
    ROOT: INFO
    com:
      xncoding: DEBUG
  file: E:/logs/app.log

---


spring:
  profiles: test

logging:
  level:
    ROOT: INFO
    com:
      xncoding: DEBUG
  file: /var/logs/app.log

端口号8092

五、启动项目演示

(1)启动类

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

@SpringBootApplication启动类固定注解

(2)浏览器访问接口

localhost:8092/dogs/dogeat/米

 

 localhost:8092/dogs/dogeat/饭

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值