实体类的代码:
package example.lucy.com.demo.entity;
import javax.persistence.*;
@Entity
@Table(name = "stu")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "age")
private Integer age;
@Column(name = "class_id")
private Long classId;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Long getClassId() {
return classId;
}
public void setClassId(Long classId) {
this.classId = classId;
}
}
dao层的代码:
package example.lucy.com.demo.dao;
import example.lucy.com.demo.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
public interface StudentDao extends JpaRepository<Student, Long> {
Student findByName(String name);
}
service的代码:
package example.lucy.com.demo.service;
import example.lucy.com.demo.entity.Student;
public interface StudentService {
Student addStudent(Student stu);
Student findById(Long id);
Student findByName(String name) throws Exception;
void updateAge(Student student) throws Exception;
void deleteStudent(String name) throws Exception;
}
serviceImpl的代码:
package example.lucy.com.demo.service.impl;
import example.lucy.com.demo.dao.StudentDao;
import example.lucy.com.demo.entity.Student;
import example.lucy.com.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Override
public Student addStudent(Student stu) {
return studentDao.save(stu);
}
@Override
public Student findById(Long id) {
Optional<Student> exist = studentDao.findById(id);
if (exist.isPresent()) {
return exist.get();
}
return null;
}
@Override
public Student findByName(String name) throws Exception {
Student exist = studentDao.findByName(name);
if (exist != null) {
return exist;
}
throw new Exception(String.format("There is no student named %s.", name));
}
@Override
public void updateAge(Student student) throws Exception {
Student exist = studentDao.findByName(student.getName());
if (exist != null) {
exist.setAge(student.getAge());
studentDao.save(exist);
} else {
throw new Exception("Update student failed.");
}
}
@Override
public void deleteStudent(String name) throws Exception {
Student exist = studentDao.findByName(name);
if (exist != null) {
studentDao.delete(exist);
} else {
throw new Exception("Delete student failed.");
}
}
}
工具类Message的代码:
package example.lucy.com.demo.utils;
public class Message<T> {
public static final String SUCCESS="success";
public static final String FAILED="failed";
private String code;
private String msg;
private T data;
public Message(String code, String msg) {
this.code = code;
this.msg = msg;
}
public Message(String code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
controller的代码:
package example.lucy.com.demo.controller;
import example.lucy.com.demo.entity.Student;
import example.lucy.com.demo.service.StudentService;
import example.lucy.com.demo.utils.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@RestController
@RequestMapping("/student")
@EnableSwagger2
public class StudentController {
@Autowired
private StudentService studentService;
@PostMapping("/add")
public Message<Student> addStudent(@RequestBody Student student){
Student result= studentService.addStudent(student);
return new Message(Message.SUCCESS, "添加学生信息成功!",result);
}
@GetMapping("/findByName")
public Message<Student> findByName(@RequestParam(name = "name")String name){
Student result=null;
try {
result = studentService.findByName(name);
}catch (Exception exception){
return new Message(Message.FAILED, exception.getMessage());
}
return new Message<>(Message.SUCCESS, "成功查询到学生信息", result);
}
@PostMapping("/updateAge")
public Message updateAge(@RequestBody Student student){
try {
studentService.updateAge(student);
}catch (Exception exception){
return new Message(Message.FAILED, exception.getMessage());
}
return new Message(Message.SUCCESS, "成功更新学生年龄信息");
}
@DeleteMapping("/delete")
public Message delete(@RequestParam(name = "name") String name){
try {
studentService.deleteStudent(name);
}catch (Exception exception){
return new Message(Message.FAILED, exception.getMessage());
}
return new Message(Message.SUCCESS, "成功删除学生信息");
}
}
转载请注明原文地址:https://blackberry.8miu.com/read-42372.html