依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
controller 层
package com.example.demo.controller;
import com.example.demo.service.StudentService;
import com.example.demo.vo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/getStudent")
public String getStudent() {
return studentService.getAll().toString();
}
@RequestMapping("/addStudent")
public String addStudent(Student student) {
int i = studentService.addStudent(student);
if (i > 0) {
return "添加成功";
} else {
return "添加失败";
}
}
@RequestMapping("/updateStudent")
public String updateStudent(Student student) {
int i = studentService.updateStudent(student);
if (i > 0) {
return "修改成功";
} else {
return "修改失败";
}
}
@RequestMapping("/deleteStudent")
public String delStudent(int id) {
int i = studentService.delStudent(id);
if (i > 0) {
return "删除成功";
} else {
return "删除失败";
}
}
}
实际功能 StudentDAOImpl
package com.example.demo.dao.impl;
import com.example.demo.dao.StudentDAO;
import com.example.demo.vo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
@SuppressWarnings("ALL")
public class StudentDAOImpl implements StudentDAO {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<Student> getAll() {
String sql = "select * from student";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Student.class));
}
@Override
public int addStudent(Student student) {
String sql = "insert into student(username,password) values(?,?)";
return jdbcTemplate.update(sql, student.getUsername(), student.getPassword());
}
@Override
public int delStudent(int id) {
String sql = "delete from student where id=?";
return jdbcTemplate.update(sql, id);
}
@Override
public int updateStudent(Student student) {
String sql = "update student set username=?,password=? where id=?";
return jdbcTemplate.update(sql, student.getUsername(), student.getPassword(), student.getId());
}
}
省略 StudentDAO
省略 StudentService
省略 StudentServiceImpl
实体类
package com.example.demo.vo;
import lombok.Data;
import org.springframework.stereotype.Component;
@Component
@Data
public class Student {
private int id;
private String username;
private String password;
}
test