springboot使用jdbcTemplate案例

1 创建实体类

public class Student {
    private Integer stuid;
    private String stuname;

    public Integer getStuid() {
        return stuid;
    }

    public void setStuid(Integer stuid) {
        this.stuid = stuid;
    }

    public String getStuname() {
        return stuname;
    }

    public void setStuname(String stuname) {
        this.stuname = stuname;
    }

    public Student(Integer stuid, String stuname) {
        this.stuid = stuid;
        this.stuname = stuname;
    }
    public Student(){}

    public Student(String stuname) {
        this.stuname = stuname;
    }

}

 

 

2 创建Dao层(Dao层接口和实现类合并)

@Repository
public class StudentDao {

    @Resource
    private JdbcTemplate jdbcTemplate;

    //查询所有学生信息
    public List<Student> getStudent(){
        RowMapper<Student> rowMapper=new RowMapper<Student>() {
            @Override
            public Student mapRow(ResultSet resultSet, int i) throws SQLException {
                Student student=new Student(resultSet.getInt("stuid"),resultSet.getString("stuname"));
                return student;
            }
        };
        return jdbcTemplate.query("select * from student",rowMapper);
    }

    //删除学生信息
    public int delStudent(Integer stuid){
        return jdbcTemplate.update("delete from student where stuid=?",stuid);
    }
    //添加学生
    public int insertStudent(Student student){
        return jdbcTemplate.update("insert into student(stuname) values (?)",student.getStuname());
    }
    //修改学生信息
    public int updaStudent(Student student){
        return jdbcTemplate.update("update student set stuname=? where stuid=? ",student.getStuname(),student.getStuid());
    }
}

 

3 创建Service层(Service层 接口和实现类合并)

@Service
public class StudentService {
    @Resource
    private StudentDao studentDao;
    //添加学生信息
   public List<Student> getStudent(){
       return studentDao.getStudent();
   }
   //删除学生信息
    public int delStudent(Integer stuid){
       return studentDao.delStudent(stuid);
    }
    //添加学生信息
    public int insertStudent(Student student){
       return studentDao.insertStudent(student);
    }
    //修改学生信息
    public int updaStudent(Student student){
       return studentDao.updaStudent(student);
    }
}

 

 

4 创建Controller层

@RestController
public class StudentController {
    @Resource
    private StudentService studentService;

    //查询所有学生信息
    @RequestMapping("/getStudent")
    public List<Student> getStudent(){
        return studentService.getStudent();
    }
    //删除学生信息
    @RequestMapping("/delStudent")
    public int delStudent(){
        return studentService.delStudent(8);
    }
    //添加学生信息
    @RequestMapping("/insertStudent")
    public int insertStudent(){
        return studentService.insertStudent(new Student("bb"));
    }
    //修改学生信息
    @RequestMapping("/updaStudent")
    public int updaStudent(){
        return studentService.updaStudent(new Student(3,"liuli"));
    }
}
上一篇:dr1- rest & restful规范


下一篇:ajax通过PUT方式调用WEBAPI