经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring Boot » 查看文章
Spring Boot
来源:cnblogs  作者:BBBone  时间:2019/5/5 8:35:39  对本文有异议

SpringBoot的优点

  1. 简化配置
  2. 下一代Java Web框架
  3. 微服务的入门级微框架

课程目录

  1. 开发第一个SpringBoot程序
  2. 自定义属性配置
  3. Controller的使用
  4. spring-data-jpa(用于操作数据库,很方便)
  5. 事务管理(数据库)

结果: 可以开发一个小型的Java Web项目

前置知识:

  1. 利用maven构建项目
  2. Spring注解
  3. RESTful API

注意:

  1. 具备必要的前置知识
  2. 不需要去学习SpringMVC
  3. Java、Maven等版本保持一致

pom.xml配置.

Spring Boot优点: 部署,配置,编码,监控都变得很简单

课程开始

  1. 开发第一个SpringBoot程序 创建SpringBoot工程的两种方式: 1) IDEA(专业版的IDEA,社区版不自带SpringBoot的创建) 2) https://start.spring.io下创建,下载zip包,在IDEA中打开 工程启动的三种方式: (可以直接去Web UI上查看结果) http://localhost:8080 默认页面 http://localhost:8080/hello 写完hello程序的页面 1) 直接IDEA启动 2) 在终端找到项目目录, 运行 mvn spring-boot:run
    3) 进行项目打包, mvn clean package,在 target目录下找到打包文件,用java -jar target/ruby-0.0.1-SNAPSHOT.jar 运行

  2. 配置 (书写以及如何使用) spring.datasource.url: jdbc:mysql://127.0.0.1:3306/ spring.datasource.username: root spring.datasource.password: 123456 spring.datasource.driver-class-name: com.mysql.jdbc

    (使用 .yml 格式的属性, .properties格式的属性 ,前一种可以折叠重复的部分,十分简洁明了,推荐前一种.后一种,它不折叠重复的字段) application.yml

    1. server:
    2. port: 8888
    3. servlet:
    4. context-path: /ruby

屏幕快照 2019-04-11 18.36.13?

LimitConfig.class

  1. ```
  2. import lombok.Data;
  3. import org.springframework.boot.context.properties.ConfigurationProperties;
  4. import org.springframework.stereotype.Component;
  5. import java.math.BigDecimal;
  6. @Component //注入成分
  7. @ConfigurationProperties(prefix = "limit") // 注入配置,前缀是limit
  8. @Data //注入数据
  9. public class LimitConfig {
  10. private BigDecimal minMoney;
  11. private BigDecimal maxMoney;
  12. private String description;
  13. }
  14. ```

小结:

单个配置引入,使用 @Value 多个配置引入,使用 @Component @ConfigurationProperties

多环境的配置。测试环境(dev),生产环境(prod)

Controller的使用

@Controller 处理http请求 @RestController Spring4之后新加的注解,原来返回json需要@ResponseBody配合@Controller @RequestMapping 配置url映射,有些特殊需求,还是需要它的 @GetMapping 直接调用Get请求的方式,配置url映射, spring-boot2.0版本后开始支持

@RequestMapping({"/hello","hi","foo"}) // 访问 hello hi foo 都是一样的 @GetMapping("/say") // 获取say

@GetMapping("/say") // 支持Get请求方式 新版本的写法 2.0往上 @PostMapping("/say") // 支持Post请求方式 @RequestMapping("/say") // 同时支持Get和Post两种方式 要么用Get/Post,只用一种,就不会起冲 注意: 一般选用一种就好,做人要有原则,服务器请求也是. http://localhost:8888/ruby/hi/say

@PathVariable 获取url中的数据 @GetMapping("/say/{id}") sayHello(@RequestParam(value = "id") http://localhost:8888/ruby/hello/say/100 @RequestParam 获取请求参数的值 @GetMapping("/say") http://localhost:8888/ruby/hello/say/?id=100
两者的调用方法不同,书写也不同,注意区分,一般使用 @RequestParam

学会使用@RequestParam注解方法即可

  1. @GetMapping("/say")
  2. // 可以只设置id, required和defaultValue可以不设置,但是,一般设置比较好
  3. public String sayHello(@RequestParam(value = "id", required = false, defaultValue = "0") Integer id) {
  4. // return "说明: " + limitConfig.getDescription();
  5. return "id: " + id;
  6. }

HelloController.class的标准写法

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.web.bind.annotation.*;
  3. @RestController
  4. @RequestMapping({"/hello","/hi","/foo"}) // 这块可以写成数组,也可以写成单个值. command+P弹出提示
  5. public class HelloController {
  6. @Autowired
  7. private LimitConfig limitConfig;
  8. @GetMapping("/say") // 新版本的写法 2.0往上
  9. // @PostMapping("/say")
  10. public String sayHello(@RequestParam(value = "id", required = false, defaultValue = "0") Integer id) {
  11. // return "说明: " + limitConfig.getDescription();
  12. return "id: " + id;
  13. }
  14. }

postman工具测试http请求的测试方法

屏幕快照 2019-04-11 19.46.26?

连接数据库:

在application-dev.yml中添加mysql配置信息

  1. spring:
  2. datasource:
  3. driver-class-name: com.mysql.jdbc.Driver
  4. url: jdbc:mysql://localhost:3306/ruby?serverTimezone=Asia/Shanghai&characterEncoding=utf-8
  5. username: root
  6. password: root
  7. jpa:
  8. hibernate:
  9. ddl-auto: create
  10. show-sql: true

Notes: 链接数据库时报这个错怎么解决啊 2018-05-22 21:27:04.985 ERROR 7960 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization. 要在url中添加时区,不然会报错的.

url: jdbc:mysql://localhost:3306/testOne?serverTimezone=Asia/Shanghai 加编码格式,不然,插入中文数据,会出现错误???

RubyMoney.class

  1. package com.imooc.ruby;
  2. import lombok.Data;
  3. import javax.persistence.Entity;
  4. import javax.persistence.GeneratedValue;
  5. import javax.persistence.Id;
  6. import javax.persistence.Table;
  7. import java.math.BigDecimal;
  8. /**
  9. * 创建ruby_money表,在表中创建id,money,producer,consumer四个字段
  10. * 注意,这里的id要设置成Long类型,不然会在创建新数据的时候,创建数据失败
  11. */
  12. @Entity // 实体
  13. @Table
  14. @Data // 生成方法
  15. public class RubyMoney {
  16. @Id // 设置主键
  17. @GeneratedValue // Id是自增的
  18. private Integer id; // Long类型,需要重点注意
  19. private BigDecimal money;
  20. /**
  21. * 发送方
  22. */
  23. private String producer;
  24. /**
  25. * 接收方
  26. */
  27. private String consumer;
  28. }

RubyMoneyRepository.class

  1. package com.imooc.ruby;
  2. import org.springframework.data.jpa.repository.JpaRepository;
  3. /**
  4. * DAO层
  5. */
  6. public interface RubyMoneyRepository extends JpaRepository<RubyMoney,Integer> {
  7. }

RubyMoneyController.class

  1. package com.imooc.ruby;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.*;
  4. import javax.websocket.server.PathParam;
  5. import java.math.BigDecimal;
  6. import java.util.List;
  7. import java.util.Optional;
  8. /**
  9. *
  10. * Controller层
  11. */
  12. @RestController
  13. public class RubyMoneyController {
  14. @Autowired
  15. private RubyMoneyRepository rubymoneyRepository;
  16. /**
  17. * 获取红包列表
  18. * @return 查询全表
  19. */
  20. @GetMapping("/ruby_1")
  21. // 查询表中所有数据
  22. public List<RubyMoney> list() {
  23. return rubymoneyRepository.findAll();
  24. }
  25. /**
  26. * 创建红包
  27. * @param producer
  28. * @param money
  29. * @return
  30. */
  31. @PostMapping("/ruby_2")
  32. // create是不需要主键这一列的,因为主键id设置的是自增
  33. public RubyMoney create(@RequestParam("producer") String producer,
  34. @RequestParam("money")BigDecimal money) {
  35. RubyMoney rubyMoney = new RubyMoney();
  36. rubyMoney.setProducer(producer);
  37. rubyMoney.setMoney(money);
  38. return rubymoneyRepository.save(rubyMoney);
  39. }
  40. /**
  41. * 通过id查询红包
  42. * @param id
  43. * @return
  44. */
  45. @GetMapping("/ruby_3/{id}")
  46. // findById这块是通过主键,进行查询的
  47. public RubyMoney findById(@PathVariable("id") Integer id) {
  48. return rubymoneyRepository.findById(id).orElse(null);
  49. }
  50. @PutMapping("/ruby_4/{id}")
  51. // update这块是通过主键,进行更新的
  52. public RubyMoney update(@PathVariable("id") Integer id,
  53. @PathParam("consumer") String consumer) {
  54. Optional<RubyMoney> optional = rubymoneyRepository.findById(id);
  55. if (optional.isPresent()) {
  56. RubyMoney rubyMoney = new RubyMoney();
  57. rubyMoney.setConsumer(consumer);
  58. return rubymoneyRepository.save(rubyMoney);
  59. }
  60. return null;
  61. }
  62. }

RubyApplication.class

  1. package com.imooc.ruby;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. /**
  5. *
  6. * 主程序入口
  7. */
  8. @SpringBootApplication
  9. public class RubyApplication {
  10. public static void main(String[] args) {
  11. SpringApplication.run(RubyApplication.class, args);
  12. }
  13. }

补充: 屏幕快照 2019-04-29 00.59.48?

原文链接:http://www.cnblogs.com/suixingc/p/spring-boot-xue-xi.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号