数据库操做使用了Spring-Data-Jpa和MySQL,什么是Spring-Data-Jpa呢?java
Spring-Data-Jpa是Spring对Hibernate的整合,关于Spring-Data-Jpa的详细内容,后面会写一系列相关的笔记。mysql
数据库操做的RESTful API设计以下:web
请求类型 | 请求路径 | 功能 |
GET | /student | 获取学生列表 |
POST | /student | 建立一个学生 |
GET | /student/id | 经过id查询一个学生 |
PUT | /student/id | 经过id更新一个学生 |
DELETE | /student/id | 经过id删除一个学生 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
spring: profiles: active: prod datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/testSprigBoot?useSSL=false username: root password: 123456 jpa: hibernate: ddl-auto: create show-sql: true
ddl-auto几个参数值表明的意义:spring
show-sql : 是否能够在控制台看到sql语句sql
(utf8mb4兼容utf8,因此能支持更多的字符集,关于emoji表情的话mysql的utf8是不支持,须要修改设置为utf8mb4,才能支持)数据库
package com.example.demo; import org.springframework.data.annotation.Id; import javax.persistence.Entity; import javax.persistence.GeneratedValue; /** * Created by xzf on 2017/9/17. */ @Entity public class Student { @javax.persistence.Id @GeneratedValue private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
运行程序后,没必要写sql语句,便可自动在数据库中建立student表json
以上实体类代码有一些须要注意的点:app
package com.example.demo; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Component; import java.util.List; public interface StuentRepository extends JpaRepository<Student,Integer> { //经过年龄来查询 public List<Student> findByAge(Integer age); }
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; /** * Created by xzf on 2017/9/17. */ @RestController public class StudentController { @Autowired private StuentRepository stuentRepository ; /** * 查询全部学生列表 * @return */ @GetMapping("/students") public List<Student> studentList(){ return stuentRepository.findAll(); } /** * 添加一个学生 */ @PostMapping("/students") public Student studentAdd(@RequestParam("name") String name, @RequestParam("age") Integer age){ Student student=new Student(); student.setName(name); student.setAge(age); return stuentRepository.save(student); } /** * 经过id查询一个学生 */ @GetMapping("/students/{id}") public Student studentFindOne(@PathVariable("id") Integer id){ return stuentRepository.findOne(id); } /** *经过id更新一个学生 */ @PutMapping("/students/{id}") public Student studentUpdate(@PathVariable("id") Integer id, @RequestParam("name") String name, @RequestParam("age") Integer age){ Student student=new Student(); student.setId(id); student.setName(name); student.setAge(age); return stuentRepository.save(student); } /** *经过id删除一个学生 */ @DeleteMapping("/students/{id}") public void studentDel(@PathVariable("id") Integer id){ stuentRepository.delete(id); } //经过年龄查询学生列表 @GetMapping("/students/age/{age}") public List<Student> studentListByAge(@PathVariable("age") Integer age){ return stuentRepository.findByAge(age); } }
以上代码的测试能够在Postman中进行,Postman能够方便的模拟各类类型的请求操做(GET,POST,PUT,DELETE等),而且能给出直观地返回json结果数据,很是方便。spring-boot
在测试PUT请求时,要注意,Postman中Body应该选择图中选项,而不是默认的form-data!测试
这里主要涉及到一个注解的使用——@Transactional
package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.transaction.Transactional; @Service public class StudentService { @Autowired private StuentRepository stuentRepository; @Transactional public void insertTwo(){ Student studentA=new Student(); studentA.setName("J"); studentA.setAge(10); stuentRepository.save(studentA); Student studentB=new Student(); studentB.setName("laka"); studentB.setAge(20); stuentRepository.save(studentB); } }
如上代码,在insertTwo()方法前加上@Transactional注解,在该方法执行过程当中,若是发生错误,则会自动回滚。具体表现为:insertTwo方法执行的操做为新增两个学生A和B,若执行过程当中有一个学生新增操做出现错误,则会回滚,数据库中不会留下任何一个学生的插入结果。