java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > springboot  jpa删除数据

springboot + jpa实现删除数据的操作代码

作者:Dr.Disrespect

这篇文章主要介绍了springboot + jpa实现删除数据的操作代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

Entity层

package coolwen.demo.model;
import lombok.*;
import javax.persistence.*;
@Table(name = "t_stu")
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
@ToString
@Entity
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO,generator = "student_id")
    private int id;
    private String name;
    private String address;
    private int age;
}

dao层方法

package coolwen.demo.dao;
import coolwen.demo.model.Student;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
@Component
public interface StudentDao extends JpaRepository<Student,Integer> {
//    Student findByNameAndAddress(String name,String address);
    void deleteById(int id);
//    Page<Student> findAllByAge(int age,Pageable pageable);
}

​​​​​​​ controller层方法

    @GetMapping("delete")
    public String deleteuser(){
        return "user/DeleteUser";
    }
    @PostMapping("delete")
    @ResponseBody
    public String deleteuser(int id){
        System.out.println(id);
        serviceimp.deleteById(id);
        return "delete suc";
    };

​​​​​​​ thymeleaf前端模板

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form method="post" action="/admin/user/delete">
    <input type="text" name="id">
<!--    <input type="text" name="address">-->
    <input type="submit" value="提交">
</form>
</body>
</html>

​​​​​​​ service层接口

package coolwen.demo.service;
import coolwen.demo.model.Student;
public interface Serviceimp {
    Student findByNameAndAddress(String name,String address);
    void deleteById(int id);
}

service层实现类

package coolwen.demo.service;
import coolwen.demo.dao.StudentDao;
import coolwen.demo.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class Serviceimpl implements Serviceimp {
    @Autowired
    StudentDao studentDao;
    @Override
    public Student findByNameAndAddress(String name, String address) {
        return studentDao.findByNameAndAddress(name,address);
    }
    @Override
    public void deleteById(int id) {
        studentDao.deleteById(id);
    }
}

到此这篇关于springboot + jpa实现删除数据的文章就介绍到这了,更多相关springboot jpa删除数据内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文