时间:2021-07-01 10:21:17 帮助过:8人阅读
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(); } }
SpringBoot整合jdbcTemplate
标签:mapping 文件 str sql osi student org epo oid