写一个 java基于Spring框架的增删改查 教程
时间: 2024-03-24 13:39:40 浏览: 71
Java中SSM框架实现增删改查功能代码详解
好的,下面是一个基于Spring框架的增删改查教程。
1. 配置数据库连接
首先需要在Spring配置文件中配置数据库连接信息,例如以下示例:
```
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
```
2. 创建实体类
创建一个Java Bean作为数据库表的映射,例如以下示例:
```
public class Student {
private int id;
private String name;
private int age;
private String gender;
// 省略getter和setter方法
}
```
3. DAO层
创建一个DAO(Data Access Object)接口,用于定义数据访问的方法,例如以下示例:
```
public interface StudentDAO {
public void addStudent(Student student);
public void updateStudent(Student student);
public void deleteStudent(int id);
public Student getStudentById(int id);
public List<Student> getAllStudents();
}
```
4. DAO实现类
创建一个DAO实现类,实现DAO接口中定义的方法,例如以下示例:
```
@Repository
public class StudentDAOImpl implements StudentDAO {
@Autowired
private JdbcTemplate jdbcTemplate;
public void addStudent(Student student) {
String sql = "INSERT INTO student(name, age, gender) VALUES (?, ?, ?)";
jdbcTemplate.update(sql, student.getName(), student.getAge(), student.getGender());
}
public void updateStudent(Student student) {
String sql = "UPDATE student SET name=?, age=?, gender=? WHERE id=?";
jdbcTemplate.update(sql, student.getName(), student.getAge(), student.getGender(), student.getId());
}
public void deleteStudent(int id) {
String sql = "DELETE FROM student WHERE id=?";
jdbcTemplate.update(sql, id);
}
public Student getStudentById(int id) {
String sql = "SELECT * FROM student WHERE id=?";
return jdbcTemplate.queryForObject(sql, new Object[]{id}, new RowMapper<Student>() {
public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setGender(rs.getString("gender"));
return student;
}
});
}
public List<Student> getAllStudents() {
String sql = "SELECT * FROM student";
return jdbcTemplate.query(sql, new RowMapper<Student>() {
public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setGender(rs.getString("gender"));
return student;
}
});
}
}
```
5. Service层
创建一个Service层,用于定义业务逻辑,例如以下示例:
```
@Service
public class StudentService {
@Autowired
private StudentDAO studentDAO;
public void addStudent(Student student) {
studentDAO.addStudent(student);
}
public void updateStudent(Student student) {
studentDAO.updateStudent(student);
}
public void deleteStudent(int id) {
studentDAO.deleteStudent(id);
}
public Student getStudentById(int id) {
return studentDAO.getStudentById(id);
}
public List<Student> getAllStudents() {
return studentDAO.getAllStudents();
}
}
```
6. 控制器
创建一个控制器,用于处理HTTP请求和响应,例如以下示例:
```
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping(value = "/addStudent", method = RequestMethod.POST)
public String addStudent(@ModelAttribute("student") Student student, Model model) {
studentService.addStudent(student);
model.addAttribute("message", "添加成功");
return "result";
}
@RequestMapping(value = "/updateStudent", method = RequestMethod.POST)
public String updateStudent(@ModelAttribute("student") Student student, Model model) {
studentService.updateStudent(student);
model.addAttribute("message", "更新成功");
return "result";
}
@RequestMapping(value = "/deleteStudent/{id}", method = RequestMethod.GET)
public String deleteStudent(@PathVariable("id") int id, Model model) {
studentService.deleteStudent(id);
model.addAttribute("message", "删除成功");
return "result";
}
@RequestMapping(value = "/getStudent/{id}", method = RequestMethod.GET)
public String getStudent(@PathVariable("id") int id, Model model) {
Student student = studentService.getStudentById(id);
model.addAttribute("student", student);
return "student";
}
@RequestMapping(value = "/getAllStudents", method = RequestMethod.GET)
public String getAllStudents(Model model) {
List<Student> students = studentService.getAllStudents();
model.addAttribute("students", students);
return "students";
}
}
```
7. 视图
创建视图,用于展示数据和接收用户输入,例如以下示例:
student.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息</title>
</head>
<body>
<h1>学生信息</h1>
<p>姓名:${student.name}</p>
<p>年龄:${student.age}</p>
<p>性别:${student.gender}</p>
</body>
</html>
```
students.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生列表</title>
</head>
<body>
<h1>学生列表</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="student" items="${students}">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
<td>${student.gender}</td>
<td><a href="/deleteStudent/${student.id}">删除</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
```
result.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>操作结果</title>
</head>
<body>
<h1>${message}</h1>
<a href="/getAllStudents">返回列表</a>
</body>
</html>
```
以上就是一个简单的基于Spring框架的增删改查教程。希望这份教程能够帮助你入门Spring数据库操作。
阅读全文