Spring Boot学习(一)

来源:互联网 发布:地下交通站知乎 编辑:程序博客网 时间:2024/05/16 16:10
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">   <modelVersion>4.0.0</modelVersion>   <groupId>com.imooc</groupId>   <artifactId>girl</artifactId>   <version>0.0.1-SNAPSHOT</version>   <packaging>jar</packaging>   <name>girl</name>   <description>Demo project for Spring Boot</description>   <parent>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-parent</artifactId>      <version>1.5.6.RELEASE</version>      <relativePath/> <!-- lookup parent from repository -->   </parent>   <properties>      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>      <java.version>1.8</java.version>   </properties>   <dependencies>      <dependency>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-web</artifactId>      </dependency>      <dependency>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-test</artifactId>         <scope>test</scope>      </dependency>      <dependency>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-data-jpa</artifactId>      </dependency>        <dependency>         <groupId>mysql</groupId>         <artifactId>mysql-connector-java</artifactId>      </dependency>   </dependencies>   <build>      <plugins>         <plugin>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-maven-plugin</artifactId>         </plugin>      </plugins>   </build></project>
package com.imooc.girl;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;@Entitypublic class Girl {    @Id    @GeneratedValue    private Integer id;    private String cupSize;    private Integer age;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getCupSize() {        return cupSize;    }    public void setCupSize(String cupSize) {        this.cupSize = cupSize;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }//    public Girl() {//    }}

package com.imooc.girl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import java.util.List;@RestControllerpublic class GirlController {    @Autowired    private GirlRepository girlRepository;    @RequestMapping("/findAll" )    public List<Girl> findAll(){    return girlRepository.findAll();    }    @PostMapping("/add")    public Girl add(@RequestParam("cupSize")String cupSize,@RequestParam("age")Integer age){        Girl girl=new Girl();        girl.setCupSize(cupSize);        girl.setAge(age);        return girlRepository.save(girl);    }    @GetMapping("/query/{id}")    public Girl query(@PathVariable("id") Integer id){        return girlRepository.findOne(id);    }    @PutMapping("/update/{id}/{cupSize}/{age}")    public Girl update(@PathVariable("id") Integer id,@PathVariable("cupSize")String cupSize,                       @PathVariable("age")Integer age){       Girl girl = new Girl();       girl.setId(id);       girl.setAge(age);       girl.setCupSize(cupSize);        return girlRepository.save(girl);    }    @DeleteMapping("/update/{id}")    public void delete(@PathVariable("id") Integer id){        girlRepository.delete(id);    }}


application.yml配置
spring: profiles:   active: dev datasource:   driver-class-name: com.mysql.jdbc.Driver   url: jdbc:mysql://192.168.1.178:3306/girl   password: root   username: root jpa:    hibernate:      ddl-auto: update    show-sql: true
application-dev.yml
server:  port: 8080



原创粉丝点击