新建项目,添加依赖
<!-- Spring Boot 集成jap依赖坐标-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Mysql数据库连接依赖坐标-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
项目配置
在application.properties文件中做以下配置
#通用数据源配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springbootjpa
spring.datasource.username=root
spring.datasource.password=root
# JPA 相关配置
#配置是否显示出响应执行sql语句
spring.jpa.show-sql=true
#配置指明在程序启动的时候要检查数据库没有对应表创建,有表更新
spring.jpa.hibernate.ddl-auto=update
创建实体类,映射
import javax.persistence.*;
@Entity
@Table(name = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "stu_id")
private Long stuId;
@Column(name = "stu_name")
private String stuName;
@Column(name = "stu_age")
private int age;
public Long getStuId() {
return stuId;
}
public void setStuId(Long stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"stuId=" + stuId +
", stuName='" + stuName + '\'' +
", age=" + age +
'}';
}
}
创建持久层接口
public interface StuRepository extends JpaRepository<Student,Long>, JpaSpecificationExecutor<Student> {
}
测试
增
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringdatajpabootApplicationTests {
@Autowired
private StuRepository stuRepository;
/**
* 增
* 创建一个实体类student添加到数据库
*/
@Test
public void addStudent() {
Student student = new Student();
student.setStuName("李白");
student.setAge(22);
stuRepository.save(student);
}
}
数据库执行语句
//因为第一次测试,数据库中并没有我们的表student,所以会创建一个数据表student
Hibernate: create table student (stu_id bigint not null auto_increment, stu_age integer, stu_name varchar(255), primary key (stu_id)) engine=MyISAM
Hibernate: insert into student (stu_age, stu_name) values (?, ?)
我们的数据库中就新增了一条数据
改
/**
* 改
*/
@Test
public void updateStudent() {
Student student = new Student();
student.setStuId(1L);
student.setStuName("兰陵王");
student.setAge(23);
stuRepository.save(student);
Student student2 = new Student();
student2.setStuId(5L);
student2.setStuName("赵云");
student2.setAge(21);
stuRepository.save(student2);
}
我们来看一下数据库执行语句:
Hibernate: select student0_.stu_id as stu_id1_0_0_, student0_.stu_age as stu_age2_0_0_, student0_.stu_name as stu_name3_0_0_ from student student0_ where student0_.stu_id=?
Hibernate: update student set stu_age=?, stu_name=? where stu_id=?
Hibernate: select student0_.stu_id as stu_id1_0_0_, student0_.stu_age as stu_age2_0_0_, student0_.stu_name as stu_name3_0_0_ from student student0_ where student0_.stu_id=?
Hibernate: insert into student (stu_age, stu_name) values (?, ?)
我们会发现数据库竟然执行了一次两次select
,一次update
和一次insert
,这是因为save(Object object)
方法会先执行select
语句检查我们的数据表中是否存在有相应id
的数据,如果存在,则执行update
操作,否则执行insert
操作。
我们执行后的数据表为:
删
/**
* 删
*/
@Test
public void deleteStudent(){
stuRepository.deleteById(1L);
}
数据库执行语句:
Hibernate: select student0_.stu_id as stu_id1_0_0_, student0_.stu_age as stu_age2_0_0_, student0_.stu_name as stu_name3_0_0_ from student student0_ where student0_.stu_id=?
Hibernate: delete from student where stu_id=?
执行后数据库表:
查
在测试之前,我们先向数据库中添加几条数据:
@Test
public void addStudent2() {
Student student = new Student();
student.setStuName("孙悟空");
student.setAge(23);
stuRepository.save(student);
Student student2 = new Student();
student2.setStuName("庄周");
student2.setAge(21);
stuRepository.save(student2);
Student student3 = new Student();
student3.setStuName("夏侯惇");
student3.setAge(21);
stuRepository.save(student3);
}
findById
顾名思义,根据ID
进行查询
/**
* 查
* findById
*/
@Test
public void findOne(){
Optional<Student> student = stuRepository.findById(3L);
System.out.println(student.toString());
}
执行数据库语句:
Hibernate: select student0_.stu_id as stu_id1_0_0_, student0_.stu_age as stu_age2_0_0_, student0_.stu_name as stu_name3_0_0_ from student student0_ where student0_.stu_id=?
执行输出结果:
Optional[Student{stuId=3, stuName='孙悟空', age=23}]
findAll
顾名思义,查询全部
/**
* 查
* findAll
*/
@Test
public void findAll(){
List<Student> students = stuRepository.findAll();
students.forEach(a->System.out.println(a.toString()));
}
数据库执行语句:
Hibernate: select student0_.stu_id as stu_id1_0_, student0_.stu_age as stu_age2_0_, student0_.stu_name as stu_name3_0_ from student student0_
执行输出结果:
Student{stuId=3, stuName='孙悟空', age=23}
Student{stuId=2, stuName='赵云', age=21}
Student{stuId=4, stuName='庄周', age=21}
Student{stuId=5, stuName='夏侯惇', age=21}