
本文讲解使用Spring-Data-Jpa操作数据库。
JPA定义了一系列对象持久化的标准。
一、在项目中使用Spring-Data-Jpa
1. 配置文件application.properties中配置如下代码:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/dbstudent
username: root
password: ccvdp
jpa:
hibernate:
ddl-auto: update
show-sql: true
注:spring.jpa.ddl-auto的值如下:
a.create:每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表 数据丢失的一个重要原因
b.create-drop:每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除
c.update:最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据model类自动更新表结 构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等应用第一次运行起来后才 会
d.validate:每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值
e.none:没有配置
2. pom.xml添加如下依赖:
<!-- JPA依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- mysql依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
3. 创建数据表实体类
package com.aston.reader.model; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; @Entity
public class Student {
@Id
@GeneratedValue
private Integer id;
private String name;
private Integer age; public Student(){
} 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;
}
}
启动项目,会自动创建数据表。
4. 创建Repository接口StudentRepository
package com.aston.reader.interfaces; import com.aston.reader.model.Student;
import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; public interface StudentRepository extends JpaRepository<Student, Integer>{
/**
* 扩展,按年龄查询
* @param age
* @return
*/
public List<Student> findByAge( Integer age);
}
5. 创建操作数据库类
package com.aston.reader.controller; import com.aston.reader.interfaces.StudentRepository;
import com.aston.reader.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import java.util.List; @RestController
public class StudentController {
@Autowired
private StudentRepository studentRepository; /**
* 查询学生列表
* @return
*/
@GetMapping(value = "/students")
public List<Student> getStudentList(){
return studentRepository.findAll();
} /**
* 添加学生
* @param name
* @param age
* @return
*/
@PostMapping(value = "/addStudent")
public Student addStudent( @RequestParam("name") String name, @RequestParam("age") Integer age){
Student student = new Student(); student.setAge( age);
student.setName( name); return studentRepository.save( student);
} /**
* 根据ID查询学生
* @param id
* @return
*/
@GetMapping(value = "findStudent/{id}")
public Student findById(@PathVariable("id") Integer id){
return studentRepository.findOne(id);
} /**
* 根据ID更新student记录
* @param id
* @param name
* @param age
* @return
*/
@PutMapping(value = "/student/{id}")
public Student updateStudent(@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 studentRepository.save( student);
} /**
* 根据ID删除记录
* @param id
*/
@DeleteMapping(value = "student/{id}")
public void deleteStudent(@PathVariable("id") Integer id){
studentRepository.delete(id);
} /**
* 扩展,按年龄查询记录
* @param age
* @return
*/
@GetMapping(value = "/findByAge")
public List<Student > findByAge(@RequestParam("age") Integer age){
return studentRepository.findByAge( age);
}
}
二、注意
有一个扩展的操作。
三、事务管理
涉及数据库操作就必然会用到事务。事务指访问并可能更新数据库中各种数据项的一个程序执行单元(unit)。
Spring Boot的事务使用比较简单,只需在方法上使用注解 @Transactional。代码实例如下:
import javax.transaction.Transactional; @Transactional
public void insertStudents(){
Student student1 = new Student();
student1.setName("zhangsan");
student1.setAge(17);
studentRepository.save(student1); Student student2 = new Student();
student2.setName("lisi");
//student2.setAge(111);
studentRepository.save(student2);
}
在操作数据库时要认真考虑事务的范围。只有查询的时候可以不需要加事务。