SpringBoot快速入门_CodingPark编程公园
发布日期:2021-06-29 15:47:17 浏览次数:2 分类:技术文章

本文共 9068 字,大约阅读时间需要 30 分钟。

基础知识

在这里插入图片描述

在这里插入图片描述
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

我们通过Spring Boot搭建Web项目的后端服务

实战项目

在这里插入图片描述

在这里插入图片描述

  • Main - 【GirlApplication】
  • Controller - 与前端连接 【GirlController】
  • domain - 实体类(与数据库对应)【Girldb】

等等


按学习与配置顺序书写✍️

在这里插入图片描述

pom.xml

4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.2.RELEASE
com.imooc
girl
0.0.1-SNAPSHOT
girl
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-configuration-processor
true
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
org.springframework.boot
spring-boot-maven-plugin

application-dev.yml (开发版本)

server:  port: 8080cupSize: Bage: 18content: "cuoSize: ${cupSize}, age: ${age}"girl:  cupSize: B  age: 18

application-prod.yml (正式版本)

server:  port: 8081cupSize: Bage: 18content: "cuoSize: ${cupSize}, age: ${age}"girl:  cupSize: F  age: 18

application.yml (正式抉择)

spring:  profiles:    active: dev  datasource:      driver-class-name: com.mysql.cj.jdbc.Driver      url: jdbc:mysql://localhost:3306/girl      username: root      password: root  jpa:    hibernate:      ddl-auto: update    show-sql: true

HelloController.java

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

package com.imooc.girl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.*;/** * @Author: TEAM-AG * @Description: 与前端连接 
* @Date: Created in 21:55 2020-08-10 * @Modified By: */@RestController@RequestMapping("/hello")public class HelloController {
// @Value("${cupSize}")// private String cupSize;// @Value("${age}")// private Integer age;// @Value("${content}")// private String content; @Autowired private GirlProperties girlProperties; @GetMapping(value = {
"/say{id}"}) public String say(@PathVariable("id") Integer id){
return "TEAM-AG " + id + girlProperties.getCupSize() + girlProperties.getAge(); }// @RequestMapping(value = {"/say"}, method = RequestMethod.GET)// public String say(@RequestParam(value = "id", required = false, defaultValue = "0") Integer id){ // 默认值// return "TEAM-AG " + id + girlProperties.getCupSize() + girlProperties.getAge();// }}

GirlProperties

在这里插入图片描述

package com.imooc.girl;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.stereotype.Component;/** * @Author: TEAM-AG * @Description: 实体类 
* @Date: Created in 22:25 2020-08-10 * @Modified By: */@Component@ConfigurationProperties(prefix = "girl") // yml 对应的映射public class GirlProperties {
private String cupSize; private Integer age; 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; }}

Girldb

package com.imooc.girl;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;/** * @Author: TEAM-AG * @Description: 与数据库对应 
* @Date: Created in 19:42 2020-08-13 * @Modified By: */@Entitypublic class Girldb {
@Id @GeneratedValue private Integer id; private String cupSize; private Integer age; public Girldb() {
// 必须有一个无参 构造方法 } 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; }}

GirlRepository

在这里插入图片描述

package com.imooc.girl;import org.springframework.data.jpa.repository.JpaRepository;import java.util.List;/** * @Author: TEAM-AG * @Description: 接口继承 Jpa * @Date: Created in 20:01 2020-08-13 * @Modified By: */public interface GirlRepository extends JpaRepository
{
// 接口本口 对数据库的操作都靠它! // 查年龄 public List
findByAge(Integer age);}

GirlController

在这里插入图片描述

package com.imooc.girl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import java.util.List;import java.util.Optional;/** * @Author: TEAM-AG * @Description: 与前端连接 
* @Date: Created in 19:54 2020-08-13 * @Modified By: */@RestControllerpublic class GirlController {
@Autowired private GirlRepository girlRepository; @Autowired // 事务 private GirlService girlService;/** * * @Description: * 查询 女生列表 * @auther: TEAM-AG * @date: 20:10 2020-08-13 * @param: [] * @return: java.util.List
*【important】=> 用哪个,开哪个! */// @GetMapping(value = "/girls") // @查所有// public List
girldbList(){
// return girlRepository.findAll();//// }// // 查询某个女生// @GetMapping(value = "/girls") // @查某个id// public Girldb Onegirldb(@RequestParam("id") Integer id){
// return girlRepository.findById(id).orElse(null);//// }// // 查询某个女生// @GetMapping(value = "/girls") // @查某个age // 需要在 Girlrepository中加个接口// public List
Onegirldb(@RequestParam("age") Integer age){
// return girlRepository.findByAge(age);//// }/** * * @Description: * 插入 * @auther: TEAM-AG * @date: 21:15 2020-08-13 * @param: [cupSize, age] * @return: java.util.List
*【important】=> 用哪个,开哪个! */// @PostMapping(value = "/girls") // @插入后看所有// public List
addgirl(@RequestParam("cupSize") String cupSize,// @RequestParam("age") Integer age){ // 需要参数咯//// Girldb girl = new Girldb();// girl.setCupSize(cupSize);// girl.setAge(age);// girlRepository.save(girl);//// return girlRepository.findAll();//// }// @PostMapping(value = "/girls") // @插入后看那个// public Girldb addgirl(@RequestParam("cupSize") String cupSize,// @RequestParam("age") Integer age){ // 需要参数咯//// Girldb girl = new Girldb();// girl.setCupSize(cupSize);// girl.setAge(age);//// return girlRepository.save(girl);//// } @PostMapping(value = "/girls/cooinserttwo") public void girlTwo(){ girlService.insertTwo(); }/** * * @Description: * 更新 * @auther: TEAM-AG * @date: 21:30 2020-08-13 * @param: * @return: * *///@PostMapping(value = "/girls")//public Girldb updategirl(@RequestParam("id") Integer id,// @RequestParam("cupSize") String cupSize,// @RequestParam("age") Integer age){ // 需要参数咯//// Girldb girl = new Girldb();// girl.setId(id);// girl.setCupSize(cupSize);// girl.setAge(age);//// return girlRepository.save(girl);//////}/** * * @Description: * 删除 * @auther: TEAM-AG * @date: 21:30 2020-08-13 * @param: * @return: * *///@DeleteMapping(value = "/girls")// public void girlDelete(@RequestParam("id") Integer id){ // girlRepository.deleteById(id);////}}

GirlService

package com.imooc.girl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;/** * @Author: TEAM-AG * @Description: 事务管理 * 买商品与扣钱,两者都OK则OK,任何一个不通过交易都算失败 * @Date: Created in 10:29 2020-08-14 * @Modified By: */@Servicepublic class GirlService {
@Autowired private GirlRepository girlRepository; @Transactional //事务 public void insertTwo(){
Girldb girlA = new Girldb(); girlA.setCupSize("A"); girlA.setAge(18); girlRepository.save(girlA); Girldb girlB = new Girldb(); girlA.setCupSize("C"); girlA.setAge(18); girlRepository.save(girlB); }}

在这里插入图片描述

转载地址:https://codingpark.blog.csdn.net/article/details/108012813 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:命名实体识别_CodingPark编程公园
下一篇:词性标注_CodingPark编程公园

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月03日 10时10分40秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章