package com.zn.entity;
public class Student {
private Integer stu_id;
private String stu_name;
@Override
public String toString() {
return "Student{" +
"stu_id=" + stu_id +
", stu_name='" + stu_name + '/'' +
'}';
}
public Student() {
}
public Student(String stu_name) {
this.stu_name = stu_name;
}
public Student(Integer stu_id, String stu_name) {
this.stu_id = stu_id;
this.stu_name = stu_name;
}
public Integer getStu_id() {
return stu_id;
}
public void setStu_id(Integer stu_id) {
this.stu_id = stu_id;
}
public String getStu_name() {
return stu_name;
}
public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
}
package com.zn.dao;
import com.zn.entity.Student;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@Repository
public class StudentDao {
//导入JDBCTemplate模板
@Resource
JdbcTemplate jdbcTemplate;
public Student selectByName(String stu_name){
String sql="select * from studentinfo where stu_name=?";
RowMapper<Student> rowMapper=new BeanPropertyRowMapper<Student>(Student.class);
Student student=jdbcTemplate.queryForObject(sql,rowMapper,stu_name);
return student;
}
//增加数据
public int insertStudent(Student student) {
return jdbcTemplate.update("insert into studentinfo(stu_name) values(?)",student.getStu_name());
}
//修改数据
public int updateStudent(Student student) {
return jdbcTemplate.update("update studentinfo set stu_name=? where stu_id=?",student.getStu_name(),student.getStu_id());
}
//删除数据
public int deleteStudent(Integer id) {
return jdbcTemplate.update("delete from studentinfo where stu_id=?",id);
}
//查询数据
public List<Student> findAll() {
//封装行数据映射
RowMapper<Student> rowMapper=new RowMapper<Student>() {
@Override
public Student mapRow(ResultSet resultSet, int i) throws SQLException {
Student students=new Student(resultSet.getInt("stu_id"),resultSet.getString("stu_name"));
return students;
}
};
return jdbcTemplate.query("select * from studentinfo", rowMapper);
}
}
package com.zn.service;
import com.zn.dao.StudentDao;
import com.zn.entity.Student;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class StudentService {
@Resource
StudentDao studentDao;
public Student selectByName(String stu_name){
return studentDao.selectByName(stu_name);
}
//增加数据
public int insertStudent(Student student) {
return studentDao.insertStudent(student);
}
//修改数据
public int updateStudent(Student student) {
return studentDao.updateStudent(student);
}
//删除数据
public int deleteStudent(Integer id) {
return studentDao.deleteStudent(id);
}
//查询数据
public List<Student> findAll(){
return studentDao.findAll();
}
}
package com.zn.controller;
import com.zn.entity.Student;
import com.zn.service.StudentService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
public class StudentController {
@Resource
StudentService studentService;
//根据名字查询单条数据
@RequestMapping("/selectByName")
public Student selectByName(String stu_name){
return studentService.selectByName(stu_name);
}
//添加数据
@RequestMapping("/insertStudent")
public int insertStudent(){
return studentService.insertStudent(new Student("刘三姐"));
}
//修改数据
@RequestMapping("/updateStudent")
public int updateStudent(){
return studentService.updateStudent(new Student(5,"小飒"));
}
//删除数据
@RequestMapping("/deleteStudent")
public int deleteStudent(){
return studentService.deleteStudent(3);
}
//查询数据
@RequestMapping("/findAll")
public List<Student> findAll(){
return studentService.findAll();
}
}