• 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 创建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);
        }
    }
  • 相关阅读:
    bzoj2588 Count on a tree
    poco对象生成的几种方式根据你使用不同的ui决定
    airtest本地连接和远程连接
    python音频文件中pcm格式提取
    python提取视频中的音频
    如何理解快速排序的时间复杂度是O(nlogn)
    剑指 Offer 45. 把数组排成最小的数
    剑指 Offer 44. 数字序列中某一位的数字
    剑指 Offer 43. 1~n 整数中 1 出现的次数
    剑指 Offer 41. 数据流中的中位数
  • 原文地址:https://www.cnblogs.com/szhhhh/p/12038202.html
Copyright © 2020-2023  润新知