SpringBoot-springboot data jpa realizes adding, deleting, modifying and checking
Development tools: IDEA
Operating environment: JDK1.8, MySQL5.7, Tomcat8.5
Technology used: SpringBoot+spring data jpa
step
database student table sql
create table student(
id int PRIMARY key auto_increment,
name VARCHAR(100),
age int
)
1. New project
Check dependencies: lombok, spring data jpa, spring web, mysql driver
![Insert picture description here](https://img-blog.csdnimg.cn/20210419140321488.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0JleW9ub2Q= , size_16, color_FFFFFF, t_70
2. Introduce spring data jpa dependency
<!-- Spring Boot JPA 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
3. Configure the application.properties file
## 数据源配置
#设置端口号
server.port=8080
#驱动类
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#设置数据源
#地址url
spring.datasource.url=jdbc:mysql://localhost:3306/jpadb?characterEncoding=utf-8
#用户名
spring.datasource.username=root
#密码
spring.datasource.password=123456
#配置SpringDataJPA
# mysql 5
# spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
#打印sql ture 就是显示 false不显示sql
spring.jpa.show-sql=true
Fourth, create entities (POJO)
Example: Student class
package com.sh.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import javax.persistence.*;
@Entity
@Table(name = "student")
@Data //lombok 自动生成getter\setter方法
public class Student {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY) //设置主键自增
private Integer id;
private String name;
private String age;
}
5. Write dao interface
Create repository package under com.sh and create StudentRepository interface
Inherit JpaRepository<T,D> T: entity class, D: primary key type
public interface StudentRepository extends JpaRepository<Student, Integer>{
//JpaRepository<实体类, 实体类的主键类型> }
Sixth, write the control layer
Create a StudentController class under com.shcontroller
package com.sh.controller;
import com.sh.entity.Student;
import com.sh.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
public class StudentController {
@Autowired
private StudentRepository studentRepository;
/*查询*/
@GetMapping("/select")
public List<Student> getAll(){
List<Student> getAll = studentRepository.findAll();
return getAll;
}
/*添加*/
@PostMapping("/insert")
public Student insert(Student student){
Student add = studentRepository.save(student);
return add;
}
/*修改*/
@PutMapping("/update")
public Student update(Student student){
Student update = studentRepository.save(student);
return update;
}
/*删除*/
@DeleteMapping("/delete")
public String delete(Integer id){
try {
studentRepository.deleteById(id);
return "成功";
}catch (Exception e){
return "失败";
}
}
}
project structure
Conclusion: