SpringBoot简介
SpringBoot是SpringMVC的升级版,有如下特点:
- 配置简单
- 新一代框架
- 微服务入门级框架
IDEA创建SpringBoot程序
Spring Initializer –> Write the detail –>Web + Thymeleaf + MySQL + JDBC –> Create Project.
- main/java文件目录:编写的类(控制器)
- main/source文件目录:属性配置
- test文件目录:单元测试文件目录
运行项目
- Step1. idea里面直接运行类
- Step2. 进入项目根目录,执行
|
|
- Step3. 首先编译执行
|
|
然后进入target目录下,执行
|
|
编写Controller
- 新建java类,标签@RestController
- 方法标签@RequestMapping(value = “/test”, methos = RequestMethod.Get)
|
|
编写配置文件
在application.properties文件下配置端口,url等信息
在配置文件中添加一个变量,然后在类文件中使用标签@Value(“${变量名}”)来关联一个变量,将配置为呢间中的属性诸如到变量中.
size=B
|
|
然后可以直接使用
|
|
此时直接获取了size的值为B
同时属性可以嵌套,就是在新的属性里使用之前设置的属性,比如
|
|
对于每一个属性,使用时都需要通过@Value标签来使用,这样带来了很大的冗余性的工作,比较好的方式是编写一个类,然后将属性的值映射到类,然后通过类实例化一个对象,直接使用.
- Step1. 在application.properties下编写响应的配置文件,将所有属性写在同一个前缀下
|
|
- Step3. 使用时添加@AutoWired标签映射,然后后定义一个相应类型的变量,直接通过get方法來获取对应属性的值
|
|
可以新建多个配置文件,然后在默认的配置文件中通过spring.profiles.active=””來设置使用哪个配置文件.
Controller使用
控制器接收请求并处理
- @Controller 处理http请求
- @RestController Spring4新加的注解
- @RequestMapping 配置url映射,通过url可以访问到我们写的方法
这里我们有两种设计方式,方式1.使用Restful风格,则使用@RestController标签,然后只用返回相应的值或者资源
方式2. 使用模板,比如thymeleaf模板,则使用@Controller标签,在resources/templates/下新建html文件然后在方法返回相应的html文件.
url中参数的解析
@PathVariable 获取url中的数据
123(value = "/hello/{id}", method = RequestMethod.GET)public String print(@PathVariable("id") int id){return tp.getName() + id;在url中直接加入要传递的参数值即可,Eg: /test/100
- @RequestParam 获取请求参数的值1234(value = "/hello", method = RequestMethod.GET)public String print(@RequestParam("id") int id){return tp.getName() + id;}
在url中直接加入?id=,Eg: /test?id=100,可以设置默认值
- @GetMapping 组合注解,还包括@PostMapping,@PutMapping等,主要是简化代码,少写代码.
数据库操作-MySQL
JPA: 定义了一系列对象持久化的标准,是一种规范
通过配置文件來配置数据库的驱动和JPA,在配置文件中添加依赖
然后在属性文件中编辑数据库的配置信息
Tips:
create每次运行时都会删除旧的表然后新建一张新表
update每次更新,不会i删除原表
此时我们可以选择自己去数据库创建数据表,也可以采用映射的方式自动添加数据表
Step1. 首先创建一个类,添加空的构造方法以及get和set方法
Step2. 添加注释@Entity,@Id主键,@GeneratedValue生成值
添加(strategy= GenerationType.IDENTITY)来确保自增
然后运行,即可在数据库下生成一张和类名相同的表``
### 编写API
采用Restful风格设计,编写所有的接口,包括创建,获取,查询等接口
主要是编写对应的Controller
首先编写一个接口继承JapRepository
12public interface PersonRepository extends JpaRepository<Person, Integer>{}编写对应的Controller
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455public class PersonController {private PersonRepository personRepository;(value = "/persons")public List<Person> personList(){/*\* @param \* @Description: 查询列表\* @Date: 上午8:46 18-3-26*/ return personRepository.findAll();}(value = "/persons")public Person addPerson(@RequestParam("size") String size,@RequestParam("age") Integer age){/*\* @param size \* @param age \* @Description: Add a person \* @Date: 上午11:14 18-3-26*/ Person person = new Person();person.setSize(size);person.setAge(age);return personRepository.save(person);}(value = "/persons/{id}")public Optional personFindOne(@PathVariable("id") Integer id){/*\* @param id \* @Description: 两种方式可选 注册,返回类是Optional<Girl>, Optional是jdk8自带的 \ \* @GetMapping(value = "/girls/{id}")\ \* public Optional<Girl> girlFindOne(@PathVariable("id") Integer id) {\ \* //直接用findById查找 \ \* //return girlRepository.findById(id); \ \* //构通Example查找 \ \* Girl girl = new Girl(); \ \* girl.setId(id); \ \* Example<Girl> example = Example.of(girl); \ \* return girlRepository.findOne(example); \\* @Date: 下午7:39 18-3-26*/ return personRepository.findById(id);}(value = "/persons/{id}")public Person personUpdate(@PathVariable("id") Integer id,@RequestParam("age") Integer age,@RequestParam("size") String size){/*\* @param id \* @param age \* @param size \* @Description: save接口根据数据库中的主键来更新,使用Put方式时,Postman要使用x-www-form-urlencode的数据格式\* @Date: 下午7:55 18-3-26*/ Person person = new Person();person.setId(id);person.setAge(age);person.setSize(size);return personRepository.save(person);}(value = "/persons/{id}")public void personDelete(@PathVariable("id") Integer id){/*\* @param id \* @Description: \* @Date: 下午8:04 18-3-26*/ personRepository.deleteById(id);}}
如果要扩展查询,比如多种查询参数和方式,则需要扩展personRepository接口,比如通过年龄来查找
首先要扩展personRepository
123public interface PersonRepository extends JpaRepository<Person, Integer>{public List<Person> findByAge(Integer age);}然后编写对应的COntroller
1234(value = "persons/age/{age}")public List<Person> personListByAge(@PathVariable("age") Integer age){return personRepository.findByAge(age);}
### 数据库事务管理
事务管理是对于一些列操作要么都执行,要么都不执行,在电商平台最常见,下单,付款,发货设计对数据库的的操作要一致,主要是维护数据库的安全性.
通过@Transactional注释来进行事务管理,在操作方法前添加此标签,则方法里的事务要么全部执行,要么都不执行.