随笔 - 323  文章 - 0  评论 - 4  阅读 - 17万

SpringBoot+Spring Data JPA

1. 添加起步依赖

1
2
3
4
5
<!-- spring data jpa起步依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

 

2.配置文件

配置项参考 SpringBoot+JPA初始化数据库表

1
2
3
spring.jpa.hibernate.ddl-auto=update
#spring.jpa.show-sql=true
#spring.jpa.generate-ddl=true

  

3. 建实体类  运行项目 就会在数据库中自动建表t_student

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
 
 
@Entity
@Table(name = "t_student")
public class Student {
 
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
 
    @Column
    private String name;
 
    @Column
    private String homeAddress;
 
    private Long homeTel;
     
 
}

 

4.建repository

1
2
3
4
5
6
7
8
9
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;
 
 
@Repository
public interface StudentRepository extends JpaRepository<Student, Long>,JpaSpecificationExecutor<Student> {
 
}

  

5.建service

1
2
3
4
5
public interface StudentService {
 
    Student save(Student student);
 
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
@Service
public class StudentServiceImpl implements StudentService {
 
    @Autowired
    private StudentRepository studentRepository;
 
    @Override
    public Student save(Student student) {
 
        return studentRepository.save(student);
 
    }
 
}

 

6.建controller  运行项目,调用方法saveStudent就可以实现 持久化student对象到数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
 
@RequestMapping("/api")
@RestController
public class StudentController {
     
    @Autowired
    private StudentService studentService;
 
    @PostMapping("/students")
    @ResponseBody
    public String saveStudent(@RequestBody Student student) {
        Student savestudent = studentService.save(student);
        return savestudent.getName();
    }
}

 

posted on   dreamstar  阅读(58)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示