Spring boot 学习笔记
Posted work hard work smart
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring boot 学习笔记相关的知识,希望对你有一定的参考价值。
1.spring boot 特点
2. 创建工程
最后的文件结构如下图
3. Gril.java
package girl; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; /** * Created by Think on 2017/10/3. */ @Entity public class Girl { @Id @GeneratedValue private Integer id; private String cupSize; private Integer age; public Girl(){} 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; } }
4. ComApplication
package girl; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ComApplication { public static void main(String[] args) { SpringApplication.run(ComApplication.class, args); } }
5. 创建接口
package girl; import org.springframework.data.jpa.repository.JpaRepository; /** * Created by Think on 2017/10/3. */ public interface GirlRepository extends JpaRepository<Girl, Integer>{ }
5. GirlProperties.java
package girl; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * Created by Think on 2017/10/3. */ @Component @ConfigurationProperties(prefix = "girl") public class GirlProperties { private String cupSize; public String getCupSize() { return cupSize; } public void setCupSize(String cupSize) { this.cupSize = cupSize; } private Integer age; public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
6. 配置文件
使用的数据库为mysql
application.yml
spring: profiles: active: prod datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/dbgirl username: root password: 123456 jpa: hibernate: ddl-auto: update show-sql: true
application-dev.yml
server: port: 8080 girl: cupSize: B age: 18
application-prod.yml
server: port: 8082 girl: cupSize: F age: 18
7. Controller文件
package girl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * Created by Think on 2017/10/3. */ @RestController public class GirlController { @Autowired private GirlRepository girlRepository; @Autowired private GirlService girlService; /** * 查詢所以女生列表 * @return */ @GetMapping(value = "/girls") public List<Girl> girlList(){ return girlRepository.findAll(); } /** * 添加一個女生 * @param cupSize * @param age * @return */ @PostMapping(value = "/girls") public Girl girlAdd(@RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age){ Girl girl = new Girl(); girl.setCupSize(cupSize); girl.setAge(age); return girlRepository.save(girl); } @PostMapping(value = "/girls/two") public void girlTwo(){ girlService.insertTow(); } }
8. pom配置
<?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>girl</groupId> <artifactId>com</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>com</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.7.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>
以上是关于Spring boot 学习笔记的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot 学习笔记一(Spring Boot 介绍)