SpringHibernate项目代码示例

  • 实体类的代码:

    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, "成功删除学生信息");
        }
    }
    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值