用spj+MySQL+servlet+dao实现增删查改的简单代码
时间: 2023-06-29 10:17:39 浏览: 225
jsp+servlet实现最简单的增删改查代码分享.docx
以下是使用SPJ(Spring+JPA)、MySQL数据库、Servlet和DAO实现增删查改的简单代码示例:
Student.java(学生实体类)
```java
@Entity
@Table(name = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private Integer age;
private Float score;
// getter和setter方法
}
```
StudentDao.java(学生DAO接口)
```java
public interface StudentDao {
List<Student> findAll();
Student findById(Long id);
void save(Student student);
void update(Student student);
void delete(Student student);
}
```
StudentDaoImpl.java(学生DAO实现类)
```java
@Repository
public class StudentDaoImpl implements StudentDao {
@PersistenceContext
private EntityManager entityManager;
@Override
public List<Student> findAll() {
return entityManager.createQuery("SELECT s FROM Student s", Student.class).getResultList();
}
@Override
public Student findById(Long id) {
return entityManager.find(Student.class, id);
}
@Override
public void save(Student student) {
entityManager.persist(student);
}
@Override
public void update(Student student) {
entityManager.merge(student);
}
@Override
public void delete(Student student) {
entityManager.remove(student);
}
}
```
StudentServlet.java(学生Servlet)
```java
@WebServlet("/student/*")
public class StudentServlet extends HttpServlet {
private StudentDao studentDao;
@Override
public void init() throws ServletException {
super.init();
studentDao = new StudentDaoImpl();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String pathInfo = request.getPathInfo();
if (pathInfo == null || pathInfo.equals("/")) {
List<Student> students = studentDao.findAll();
request.setAttribute("students", students);
request.getRequestDispatcher("/WEB-INF/views/student/list.jsp").forward(request, response);
} else if (pathInfo.equals("/add")) {
request.getRequestDispatcher("/WEB-INF/views/student/add.jsp").forward(request, response);
} else if (pathInfo.matches("/\\d+")) {
Long id = Long.parseLong(pathInfo.substring(1));
Student student = studentDao.findById(id);
if (student != null) {
request.setAttribute("student", student);
request.getRequestDispatcher("/WEB-INF/views/student/edit.jsp").forward(request, response);
} else {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
} else {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String pathInfo = request.getPathInfo();
if (pathInfo == null || pathInfo.equals("/")) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
} else if (pathInfo.equals("/add")) {
String name = request.getParameter("name");
Integer age = Integer.parseInt(request.getParameter("age"));
Float score = Float.parseFloat(request.getParameter("score"));
Student student = new Student();
student.setName(name);
student.setAge(age);
student.setScore(score);
studentDao.save(student);
response.sendRedirect(request.getContextPath() + "/student/");
} else if (pathInfo.matches("/\\d+")) {
Long id = Long.parseLong(pathInfo.substring(1));
Student student = studentDao.findById(id);
if (student != null) {
String name = request.getParameter("name");
Integer age = Integer.parseInt(request.getParameter("age"));
Float score = Float.parseFloat(request.getParameter("score"));
student.setName(name);
student.setAge(age);
student.setScore(score);
studentDao.update(student);
response.sendRedirect(request.getContextPath() + "/student/");
} else {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
} else {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
}
}
@Override
protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String pathInfo = request.getPathInfo();
if (pathInfo == null || pathInfo.equals("/")) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
} else if (pathInfo.matches("/\\d+")) {
Long id = Long.parseLong(pathInfo.substring(1));
Student student = studentDao.findById(id);
if (student != null) {
studentDao.delete(student);
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
} else {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
} else {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
}
}
}
```
list.jsp(学生列表页面)
```html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>学生管理</title>
</head>
<body>
<h1>学生管理</h1>
<table border="1">
<thead>
<tr>
<th>姓名</th>
<th>
阅读全文