导入jpa及相关模块的依赖
使用Spring Intializr,选中JPA,web,jdbc模块完成项目的创建
并按找自己需要导入其他依赖,这里我导入druid连接池和mysql驱动,具体pom.xml依赖如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- druid连接池与mysql驱动-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
编写配置文件
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db_springboot?characterEncoding=utf8&ServerTimezone=GMT%2B8
jpa:
hibernate:
ddl-auto: create
show-sql: true
编写映射实体类
package com.example.demo.pojo;
import javax.persistence.*;
import java.util.Date;
/**
* @author XiaoXin
* @date 2020/2/21 下午9:46
*/
@Entity
@Table(name = "tb_student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "sid")
private Integer id;
@Column(name = "sname",length = 15,nullable = false)
private String name;
@Column(name = "semail",length = 20)
private String email;
@Column(name = "sage")
private Integer age;
@Column()
private Date registerTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getRegisterTime() {
return registerTime;
}
public void setRegisterTime(Date registerTime) {
this.registerTime = registerTime;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", registerTime=" + registerTime +
'}';
}
}
实现JpaRepository接口
import com.example.demo.pojo.Student;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* @author XiaoXin
* @date 2020/2/21 下午10:08
*/
public interface StudentRepository extends JpaRepository<Student,Integer> {
}
访问测试
mport com.example.demo.pojo.Student;
import com.example.demo.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Optional;
/**
* @author XiaoXin
* @date 2020/2/21 下午11:08
*/
@RestController
public class StudentController {
@Autowired
private StudentRepository repository;
private List<Student> studentList = null;
/**
* 添加
* @param student
* @return 返回新集合
*/
@GetMapping("/stu/add")
private List<Student> addStu(Student student){
Student stu = repository.save(student);
studentList.add(stu);
return studentList;
}
/**
* 按id查
* @param id
* @return
*/
@GetMapping("/stu/{id}")
private Student queryStuById(@PathVariable("id")Integer id){
// Student student = repository.getOne(id);
Optional<Student> optionalStudent = repository.findById(id);
Student student =optionalStudent.get();
return student;
}
/**
* 查询全部
* @return
*/
@GetMapping("/stus")
private List<Student>queryAllStu(){
return studentList = repository.findAll();
}
/**
* 根据id删除
* @param id
* @return 返回删除后的列表集合
*/
@GetMapping("/stu/del/{id}")
private List<Student>delStu(@PathVariable("id")Integer id){
repository.deleteById(id);
return studentList = repository.findAll();
}
/**
* 测试修改
* @param newStu 前段传入具体属性值完成封装后的实体
* @return 返回修改后的列表集合
*/
@GetMapping("/stu/update")
private List<Student>modifyStu(Student newStu){
/**
* 1.传入id值并能找到,newStu会覆盖掉原主键为id的stu,完后数据更新
* 2.不传入id,就插入这条数据;
* 3.传入id但找不到对应主键,还是插入这条数据,
* 并且这条数据的主键会在原基础上自增,因为实体类id配置的主键生成策略是自增,当然前提是数据库支持自增
*/
repository.saveAndFlush(newStu);
return studentList = repository.findAll();
}
}
部分效果展示
插入
修改,但没传id,可以看到新增了这条数据
传入id,修改age,实现了更新
删除
按id 查询
查询全部