一、项目配置和依赖
1、通过Spring Initializr生成项目,将项目压缩包解压,打开Eclipse,选择import->Existing Maven Projects导入项目;
2、打开pom.xml添加依赖项
<dependencies>
<!-- 添加jpa依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- 添加thymeleaf依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 添加web依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 添加mysql依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 添加test依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
~JPA(Java Persistence Api)是JDK5.0注解或XML描述对象,并将运行期的实体对象持久化到数据库中
~Thymeleaf是一个模板引擎,可以完全替代JSP
二、实体类
创建一个Student实体类(放在Entity包中)
@Entity //声明类为实体 public class Student { @Id //指定的类的属性,用于识别(一个表中的主键) @GeneratedValue //指定如何标识属性可以被初始化 private Integer stuID; private String stuName; private String stuPhone; private String stuAddress; public Integer getStuID() { return stuID; } public void setStuID(Integer stuID) { this.stuID = stuID; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public String getStuPhone() { return stuPhone; } public void setStuPhone(String stuPhone) { this.stuPhone = stuPhone; } public String getStuAddress() { return stuAddress; } public void setStuAddress(String stuAddress) { this.stuAddress = stuAddress; } }
三、Repository
在repository包中编写一个StudentRepository接口,继承JpaRepository
public interface StudentRepository extends JpaRepository<Student, Integer>{ }
四、实体类服务
编写一个接口StudentService(放在service包中),接口中定义学生服务
public interface StudentService { //添加学生信息 Student addStudent(Student student); //返回所有学生信息 List<Student> getAllStudent(); //修改学生信息 Student updateStudent(Student student); //删除学生信息 void deleteStudent(Integer stuId); //查询学生信息 Student getStudentByID(Integer stuID); }
五、实体类服务的实现
编写StudentServiceImpl类实现StudentService接口中的方法(放在serviceImpl包中)
@Service public class StudentServiceImpl implements StudentService{ @Autowired private StudentRepository studentRepository; @Override public Student addStudent(Student student) { return studentRepository.save(student); } @Override public List<Student> getAllStudent(){ return studentRepository.findAll(); } @Override public Student updateStudent(Student student) { return studentRepository.save(student); } @Override public void deleteStudent(Integer stuId) { studentRepository.deleteById(stuId); } @Override public Student getStudentByID(Integer stuID) { return studentRepository.findById(stuID).get(); } }
六、Controller
在StudentController写入以下代码,实现学生服务
@RestController @RequestMapping("/stu") public class StudentController { @Autowired private StudentService studentService; @RequestMapping("/test") private String test() { return "测试"; } @PostMapping("/add") private Student addStudent (Student student) { return studentService.addStudent(student); } @GetMapping("/all") private List<Student> getAllStu(){ return studentService.getAllStudent(); } @GetMapping("/getbyid/{id}") private Student getStuByStuId(@PathVariable("id") Integer stuId) { return studentService.getStudentByID(stuId); } @PutMapping("/updatestu") private Student updateStudent(Student student) { return studentService.updateStudent(student); } @DeleteMapping("/deletestu/{id}") private void deleteStuById(@PathVariable("id") Integer stuId) { studentService.deleteStudent(stuId); } }