1.SpringBoot和SpringMVC的关系
答:他们之间没有必然联系。可以直接学习SpringBoot。
2.SpringBoot的特点
化繁为简,简化配置
备受关注,是下一代框架
微服务的入门级微框架
做微服务往往用SpringCloud,想要会使用SpringCloud的话,必须会使用SpingBoot。
SpringCloud是建立在SpringBoot的基础之上的。
3.课程介绍
第一个SpringBoot程序
自定义属性配置
Controller的使用
spring-data-jpa
事务管理
<Spring入门篇>
http://www.imooc.com/learn/196
项目管理利器maven
http://www.imooc.com/learn/443
版本和老师保持一致:
java -version 1.8.0_111
mvn -version 3.5.4
启动项目的方法:
- IDEA中启动
- 命令行启动
打开项目所在的目录
mvn spring-boot:run
- 项目部署时,打包之后启动
mvn clean package
打包之后,会有target/某某,.jar
java -jar target/luckmoney-0.0.1-SNAPSHOT.jar
4.项目的配置
配置举例:
application.properties
server.port=8081
server.servlet.context-path=/luckymoney
application.yml (我们用这种方法)
server:
port: 8081
servlet:
context-path: /luckymoney
minMoney: 1
description: 最少要发1元
如何在配置里面使用配置
server:
port: 8081
servlet:
context-path: /luckymoney
minMoney: 2
description: 最少要发${minMoney}元
载入配置:
import java.math.BigDcimal;
@RestController
public class HelloController {
@Value("${minMoney}")
prinvate BigDecimal minMoney;
@Value("${description}")
private String description;
@GetMapping("/hello")
pubilc String say() {
return "minMoney:" + minMoney + ", 说明:“ + description;
}
}
启动后,地址访问,
localhost:8081/luckymoney/hello
我们每次都写@Value的话,是不是有点烦
配置里每加一个,都需要重新增加@Value
加个limit
server:
port: 8081
servlet:
context-path: /luckymoney
limit:
minMoney: 2
maxMoney: 9999
description: 最少要发${minMoney}元
我们再写个LimitConfig对象,写个配置类
把字段写完,get set
加注解,把配置注入进来
@Component
@ConfigurationProperties(prefix = "limit")
package com.imooc.luckymoney;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
@Component
@ConfigurationProperties(prefix = "limit")
public class LimitConfig {
private BigDecimal minMoney;
private BigDecimal maxMoney;
private String description;
public BigDecimal getMinMoney() {
return minMoney;
}
public void setMinMoney(BigDecimal minMoney) {
this.minMoney = minMoney;
}
public BigDecimal getMaxMoney() {
return maxMoney;
}
public void setMaxMoney(BigDecimal maxMoney) {
this.maxMoney = maxMoney;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
然后,以前@Value那些就不要了
import java.math.BigDcimal;
@RestController
public class HelloController {
@Autowired
private LimitConfig limitConfig;
@GetMapping("/hello")
pubilc String say() {
return ", 说明:“ + limitConfig.getDescription();
}
}
server:
port: 8081
servlet:
context-path: /luckymoney
minMoney: 2
description: 最少要发${limit.minMoney}元,最多${limit.maxMoney}元
可以根据情况,设定不同的yml配置文件。关于多环境配置,这里先不show,看我github完整代码吧。
若想使用product生成环境的配置,jar包那里得改
在运行时,把参数传进去。
java -jar -Dspring.profiles.active=prod target/luckymoney-0.0.1-SNAPSHOT.jar
5.Controller的使用
pom.xml
增加或者减少依赖,都需要重新import
@Controller + @ResponseBody = @RestController
@RestController
@RequestMapping("/hello")
public class HelloController {
@Autowired
private LimitConfig limitConfig;
@GetMapping("/say")
public String say() {
return "说明:” + limitConfig.getDescription();
}
}
在实际请求中,无论是get还是post,往往都是会携带参数的。
来一段代码,获取url中的id
localhost:8081/luckymoney/hello/say/100
@RestController
@RequestMapping("/hello")
public class HelloController {
@Autowired
private LimitConfig limitConfig;
@GetMapping("/say/{id}")
public String say(@PathVariable("id") Integer id) {
return "id:" + id;
}
}
来,再来一段代码,获取url中的id
localhost:8081/luckymoney/hello/say?id=100
@RestController
@RequestMapping("/hello")
public class HelloController {
@Autowired
private LimitConfig limitConfig;
@GetMapping("/say")
public String say(@RequestParam("id") Integer id) {
return "id:" + id;
}
}
如果url中不传id的话,
@RequestParam(value = "id", required = false, defaultValue = "0")
6.关于红包收发之操作数据库(上)
Spring-Data-Jpa
JPA(Java Persistence API)定义了一系列对象持久化的标准,目前实现这一规范的产品有Hibernate,TopLink等。
接下来,完成4个接口
自主看github
事务
数据库事务,是指作为单个逻辑工作单元执行的一系列操作,要么完全地执行,要么完全地不执行。
事务操作一般都放在Servise中,
import org.springframework.stereotype.Service;
import java.math.BigDecrimal;
@Service
public class LuckymoneyService {
public void createTwo() {
//写到数据库中
@Autowired
private LuckmoneyRepository repository;
Luckymoney luckymoney1 = new Luckymoney();
luckymoney1.setProducer("宝宝");
luckymoney1.setMoney(new BigDecimal("520"));
repository.save(luckymoney1);
Luckymoney luckymoney2 = new Luckymoney();
luckymoney2.setProducer("宝宝");
luckymoney2.setMoney(new BigDecimal("1314"));
repository.save(luckymoney2);
}
}
然后,在controller中调用Service
@Autowired
private LuckymoneyService service;
//调用Service
@PostMapping("/luckymoneys/two")
public void createTwo() {
service.createTwo();
}
要么都成功,要么都失败
加个注解@Transactional
注意,需要把Mysql数据库的引擎改成InnoDB
在实际开发中,事务用的非常广泛。
github:
https://github.com/sunshinezhihuo/myjavaprojects
luckymoney.zip那个是本项目完整的代码