springboot使用jdbcTemplate案例

时间:2021-09-06 06:23:54

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 创建application.yml文件 

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql:///springbootjpa
    username: root
    password: 123

 

 

 

5 创建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"));
    }
}

 

 

6 启动程序

@SpringBootApplication
public class StartSpringBoot {
    public static void main(String[] args) {
        SpringApplication.run(StartSpringBoot.class,args);
    }
}