博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot快速入门_CodingPark编程公园
阅读量:1887 次
发布时间:2019-04-26

本文共 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); }}

在这里插入图片描述

转载地址:http://hizdf.baihongyu.com/

你可能感兴趣的文章
千人千面Elasticsearch实战学习笔记
查看>>
最大子数组问题(递归)(java)
查看>>
2021年第十二届蓝桥杯软件赛省赛第二场 C/C++ 大学 A 组
查看>>
2020年哨兵数据批量下载(USGS)
查看>>
简单3步快速生成千万级别mysql测试数据库,模拟电商数据
查看>>
EasyDSS平台接入设备量过多的情况下如何进行批量推流测试?
查看>>
mysql数据库操作基础
查看>>
Mariadb基础管理
查看>>
kolla-ansible部署openstack+ceph高可用集群queens版本--- 部署说明
查看>>
kolla-ansible部署openstack+ceph高可用集群queens版本--- 环境准备及初始化
查看>>
kolla-ansible部署openstack+ceph高可用集群queens版本---docker私有镜像仓库配置
查看>>
mysql 中com.mysql.jdbc.PacketTooBigException 解决办法
查看>>
awk 的内置变量 NF、NR、FNR、FS、OFS、RS、ORS
查看>>
CentOS系统内核升级攻略
查看>>
linux系统时区修改(Debian的主机和docker)
查看>>
docker-compose 安装
查看>>
crontab 定时任务
查看>>
查看docker veth pair与宿主机上网卡的对应关系
查看>>
使用 GitLab CI 进行持续集成的一些踩坑
查看>>
企业云盘给贸易业带来新的效益
查看>>