使用spring-data-jpa实现一个简单的增删改查

使用spring-data-jpa实现一个简单的增删改查

yml的配置如下

server:
  port: 8081
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: root
  jpa:
    show-sql: true  #显示sql语句
    hibernate:
      ddl-auto: update
      naming:
        implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl  #驼峰命名
    database: mysql #配置现在所使用的数据库
#配置spring-data-jpa

实体类的创建

使用spring-data-jpa实现一个简单的增删改查_第1张图片
使用Lombok插件生成get和set方法

再写个dao层接口继承JpaRepository

使用spring-data-jpa实现一个简单的增删改查_第2张图片
后面的Long是主键id的类型

接着就是接口和实现类

接口


public interface StudentService {
    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    Page findAll(Integer page, Integer pageSize);

    /**
     * 根据id查询学生
     * @param id
     * @return
     */
    Student findById(Long id);

    /**
     * 根据id删除学生
     *
     * @param id
     */
    void delStudent(Long id);

    /**
     * 修改学生信息
     *
     * @param student
     * @return
     */
    Student modifyStudent(Student student);

    /**
     * 新增学生
     * @param student
     * @return
     */
    Student addStudent(Student student);

}

实现类


@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    StudentDao studentDao;

    @Autowired
    IdWorker idWorker;

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page findAll(Integer page, Integer pageSize) {
        PageRequest of = PageRequest.of(page-1,pageSize);
        Page all = studentDao.findAll(of);
        return all;
    }

    /**
     * 根据id查询学生
     *
     * @param id
     * @return
     */
    @Override
    public Student findById(Long id) {
        return studentDao.findStudentById(id);
    }

    /**
     * 根据id删除学生
     *
     * @param id
     */
    @Override
    public void delStudent(Long id) {
        studentDao.deleteById(id);
    }

    /**
     * 修改学生信息
     *
     * @param student
     * @return
     */
    @Override
    public Student modifyStudent(Student student) {
        Student save = studentDao.save(student);
        return save;
    }

    /**
     * 新增学生
     *
     * @param student
     * @return
     */
    @Override
    public Student addStudent(Student student) {
        student.setId(idWorker.nextId());
        Student save = studentDao.save(student);
        return save;
    }

}

最后再写个控制器


@Controller
public class StudentController {

    @Autowired
    StudentService studentService;

    static final Integer pageSize = 2;
    Integer TotalPages;

    /**
     * 刚开始加载时查询第一页的信息
     * @param model
     * @return
     */
    @GetMapping({"/","/index"})
    public String findIndex(Model model) {
        Page all = studentService.findAll(1, this.pageSize);
        this.TotalPages = all.getTotalPages();
        model.addAttribute("list", all);
        return "student";
    }

    /**
     * 分页查询
     * @param page
     * @param model
     * @return
     */
    @GetMapping("/student/{page}")
    public String findAll(@PathVariable("page") Integer page, Model model) {
        if (page <= 0) {
            page = 1;
        }
        if(page>this.TotalPages){
            page = this.TotalPages;
        }
        Page all = studentService.findAll(page, this.pageSize);
        this.TotalPages = all.getTotalPages();
        model.addAttribute("list", all);
        return "student";
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/student/del/{id}")
    public String del(@PathVariable("id") Long id) {
        studentService.delStudent(id);
        return "redirect:/index";
    }

    /**
     * 跳转修改页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/student/edit/{id}")
    public String getUpd(@PathVariable("id") Long id, Model model) {
        Student stu = studentService.findById(id);
        model.addAttribute("flag1", "student/modify");
        model.addAttribute("flag", "修改");
        model.addAttribute("student", stu);
        return "modify";
    }

    /**
     * 修改
     * @param student
     * @param model
     * @return
     */
    @PostMapping("/student/modify")
    public String modify(Student student,Model model) {
        try {
            studentService.modifyStudent(student);
            model.addAttribute("flag1", "student/modify");
            model.addAttribute("flag", "修改");
            Student stu = studentService.findById(student.getId());
            model.addAttribute("student", stu);
            model.addAttribute("msg","修改成功");
        } catch (Exception e) {
            model.addAttribute("msg","修改失败");
        }
        return "modify";
    }

    /**
     * 跳转新增页面
     * @param model
     * @return
     */
    @GetMapping("/student/edit")
    public String getUpd(Model model) {
        model.addAttribute("flag1", "student/add");
        model.addAttribute("flag", "新增");
        return "modify";
    }

    /**
     * 新增页面
     * @param student
     * @param model
     * @return
     */
    @PostMapping("/student/add")
    public String add(Student student,Model model) {
        try {
            model.addAttribute("flag1", "student/add");
            model.addAttribute("flag", "新增");
            studentService.addStudent(student);
            model.addAttribute("msg","新增成功");
        } catch (Exception e) {
            model.addAttribute("msg","新增失败");
        }
        return "modify";
    }


}

下一篇博客要使用vue进行前后端分离,会使用到这次的一些代码。

你可能感兴趣的:(spring-data-jpa)